|
| 1 | +import { createSignal, ensure, race, resource, spawn, until, withResolvers } from "effection"; |
| 2 | +import type { Operation, Subscription } from "effection"; |
| 3 | +import { access } from "node:fs/promises"; |
| 4 | +import { dirname, join, resolve } from "node:path"; |
| 5 | +import { watch } from "chokidar"; |
| 6 | + |
| 7 | +/** |
| 8 | + * How long after a notification a second read still catches an edit Chokidar |
| 9 | + * folded into it: it drops a change within 50 ms of the last one it reported, |
| 10 | + * and holds an unlink up to 100 ms waiting for a replacing add. |
| 11 | + */ |
| 12 | +export const TODO_WATCH_SETTLE_MS = 150; |
| 13 | + |
| 14 | +export class TodoWatchError extends Error { |
| 15 | + override name = "TodoWatchError"; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Notifications that `todo.md` in one workspace may have changed. |
| 20 | + * |
| 21 | + * The file may be missing when the watch starts and appear, be replaced |
| 22 | + * atomically, disappear and reappear later. The workspace directory itself may |
| 23 | + * be missing too: a legacy execution has no paired directory until its first |
| 24 | + * explicit Sidekick message publishes one, and creating it here would turn that |
| 25 | + * execution into a broken pair. Until it exists, the nearest existing ancestor |
| 26 | + * is watched for the next missing component, one level at a time. |
| 27 | + * The subscription exists before the watcher reports ready, so a read taken |
| 28 | + * after this returns sees every edit that no later notification announces. |
| 29 | + */ |
| 30 | +export function* useTodoWatch(workspace: string): Operation<Subscription<void, void>> { |
| 31 | + return yield* resource<Subscription<void, void>>(function* (provide) { |
| 32 | + const changes = createSignal<void, void>(); |
| 33 | + const subscription = yield* changes; |
| 34 | + const arrivals = createSignal<void, void>(); |
| 35 | + const arrived = yield* arrivals; |
| 36 | + const failed = withResolvers<never>(); |
| 37 | + const directory = resolve(workspace); |
| 38 | + const target = join(directory, "todo.md"); |
| 39 | + let current: { readonly final: boolean; close(): Operation<void> } | undefined; |
| 40 | + // Registered before any watcher or transition exists, so it runs after |
| 41 | + // all of them have stopped: closing a watcher removes its listeners at |
| 42 | + // once, and only then do both streams end, with nothing left to send. |
| 43 | + yield* ensure(function* () { |
| 44 | + if (current !== undefined) { |
| 45 | + yield* current.close(); |
| 46 | + } |
| 47 | + arrivals.close(); |
| 48 | + changes.close(); |
| 49 | + }); |
| 50 | + |
| 51 | + // One non-recursive watch of the nearest existing directory on the way to |
| 52 | + // the workspace, passing only the one path that moves this watch forward: |
| 53 | + // todo.md once the workspace exists, the next missing component before. |
| 54 | + function* arm(): Operation<{ readonly final: boolean; close(): Operation<void> }> { |
| 55 | + let base = directory; |
| 56 | + let next: string | undefined; |
| 57 | + while (!(yield* present(base))) { |
| 58 | + next = base; |
| 59 | + base = dirname(base); |
| 60 | + if (base === next) { |
| 61 | + throw new TodoWatchError(`no ancestor of ${directory} exists to watch.`); |
| 62 | + } |
| 63 | + } |
| 64 | + const awaited = next ?? target; |
| 65 | + const watcher = watch(base, { |
| 66 | + depth: 0, |
| 67 | + ignored: (path) => path !== base && path !== awaited, |
| 68 | + ignoreInitial: true, |
| 69 | + persistent: true, |
| 70 | + usePolling: false, |
| 71 | + atomic: true, |
| 72 | + }); |
| 73 | + const close = () => until(watcher.close()); |
| 74 | + // Closed by the scope that armed it unless it is handed back, because a |
| 75 | + // failure or a halt before then leaves it with no other owner. |
| 76 | + let armed = false; |
| 77 | + yield* ensure(function* () { |
| 78 | + if (!armed) { |
| 79 | + yield* close(); |
| 80 | + } |
| 81 | + }); |
| 82 | + // Chokidar lets CHOKIDAR_USEPOLLING override the option above for every |
| 83 | + // watcher in the process; polling is what this watch exists to replace. |
| 84 | + if (watcher.options.usePolling) { |
| 85 | + throw new TodoWatchError( |
| 86 | + "CHOKIDAR_USEPOLLING enables polling, which the TODO watch does not use. " + |
| 87 | + "Unset it or set it to false and start the REPL again.", |
| 88 | + ); |
| 89 | + } |
| 90 | + const ready = withResolvers<void>(); |
| 91 | + watcher.on("error", (error) => { |
| 92 | + failed.reject( |
| 93 | + new TodoWatchError("todo.md can no longer be watched for changes.", { cause: error }), |
| 94 | + ); |
| 95 | + }); |
| 96 | + watcher.on("ready", () => ready.resolve()); |
| 97 | + watcher.on("all", (_event, path) => { |
| 98 | + if (path !== awaited) { |
| 99 | + return; |
| 100 | + } |
| 101 | + if (next === undefined) { |
| 102 | + changes.send(); |
| 103 | + } else { |
| 104 | + arrivals.send(); |
| 105 | + } |
| 106 | + }); |
| 107 | + yield* race([ready.operation, failed.operation]); |
| 108 | + // Initial entries are not reported, so a component created between the |
| 109 | + // existence check and readiness would otherwise never arrive. |
| 110 | + if (next !== undefined && (yield* present(next))) { |
| 111 | + arrivals.send(); |
| 112 | + } |
| 113 | + armed = true; |
| 114 | + return { final: next === undefined, close }; |
| 115 | + } |
| 116 | + |
| 117 | + current = yield* arm(); |
| 118 | + if (!current.final) { |
| 119 | + yield* spawn(function* () { |
| 120 | + while (current !== undefined && !current.final) { |
| 121 | + const arrival = yield* race([arrived.next(), failed.operation]); |
| 122 | + if (arrival.done) { |
| 123 | + return; |
| 124 | + } |
| 125 | + // The deeper watch is ready before the shallower one closes, and |
| 126 | + // the notification after it covers whatever the workspace gained |
| 127 | + // while neither watch was looking at it. |
| 128 | + const previous = current; |
| 129 | + current = yield* arm(); |
| 130 | + yield* previous.close(); |
| 131 | + if (current.final) { |
| 132 | + changes.send(); |
| 133 | + } |
| 134 | + } |
| 135 | + }); |
| 136 | + } |
| 137 | + yield* provide({ |
| 138 | + next: () => race([subscription.next(), failed.operation]), |
| 139 | + }); |
| 140 | + }); |
| 141 | +} |
| 142 | + |
| 143 | +function present(path: string): Operation<boolean> { |
| 144 | + return until( |
| 145 | + access(path).then( |
| 146 | + () => true, |
| 147 | + () => false, |
| 148 | + ), |
| 149 | + ); |
| 150 | +} |
0 commit comments