Fix live operator source watcher
This commit is contained in:
parent
b53abf52f5
commit
510cdf1d2c
2 changed files with 25 additions and 2 deletions
|
|
@ -129,7 +129,7 @@ async function readSwarmOperatorInputs(paths) {
|
|||
}
|
||||
|
||||
function watchSwarmOperatorInputs(paths, onChange, { debounceMs = 75 } = {}) {
|
||||
const watched = [...new Set(Object.values(paths).map(pathModule.resolve))];
|
||||
const watched = [...new Set(Object.values(paths).map((value) => pathModule.resolve(value)))];
|
||||
const byDirectory = new Map();
|
||||
for (const path of watched) {
|
||||
const directory = pathModule.dirname(path);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ const fs = require("node:fs/promises");
|
|||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
const test = require("node:test");
|
||||
const { operatorViewPublication, projectSwarmOperatorView, readSwarmOperatorInputs } = require("./voidbot-swarm-operator-view.cjs");
|
||||
const { operatorViewPublication, projectSwarmOperatorView, readSwarmOperatorInputs, watchSwarmOperatorInputs } = require("./voidbot-swarm-operator-view.cjs");
|
||||
|
||||
test("projects bounded operator state and lets control own heat", () => {
|
||||
const view = projectSwarmOperatorView({
|
||||
|
|
@ -70,3 +70,26 @@ test("reads source files independently and reports a missing owner", async () =>
|
|||
await fs.rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("watches atomic source replacement without polling", async () => {
|
||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), "voidbot-operator-watch-"));
|
||||
const target = path.join(root, "heartbeat.json");
|
||||
await fs.writeFile(target, "{}");
|
||||
let stop = () => {};
|
||||
try {
|
||||
const changed = new Promise((resolve, reject) => {
|
||||
const timeout = setTimeout(() => reject(new Error("source watcher did not observe replacement")), 2000);
|
||||
stop = watchSwarmOperatorInputs({ heartbeat: target }, () => {
|
||||
clearTimeout(timeout);
|
||||
resolve();
|
||||
}, { debounceMs: 10 });
|
||||
});
|
||||
const replacement = path.join(root, "heartbeat.next.json");
|
||||
await fs.writeFile(replacement, '{"state":"ready"}');
|
||||
await fs.rename(replacement, target);
|
||||
await changed;
|
||||
} finally {
|
||||
stop();
|
||||
await fs.rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue