Watch a directory
Watch a directory when your application must react to files created or changed inside a running sandbox.
mjs
import { FilesystemEventType, Sandbox } from "@abox-dev/sdk";
const sandbox = await Sandbox.create();
try {
let receiveEvent;
const nextWrite = new Promise((resolve) => {
receiveEvent = resolve;
});
const watcher = await sandbox.files.watchDir(
"/tmp",
(event) => {
if (event.type === FilesystemEventType.WRITE) receiveEvent(event);
},
{ includeEntry: true },
);
await sandbox.files.write("/tmp/message.txt", "Hello");
const event = await nextWrite;
console.log(event.type, event.entry?.path);
await watcher.stop();
} finally {
await sandbox.kill();
}JavaScript delivers events to the callback passed to watchDir. Python stores them until get_new_events() is called. Stop the watcher when it is no longer needed.
Set recursive to watch existing subdirectories. Set includeEntry in JavaScript or include_entry in Python to receive file information with an event. Entry information can be absent after a file is removed.
Watching a network filesystem requires the explicit allowNetworkMounts / allow_network_mounts option. Such events are best effort: changes made by a different client are not delivered to the sandbox watcher.