function watchLoop
watchLoop(
directories: string[],
run: () => Promise<void>,
watch?: (
directory: string,
listener: () => void
) => void
,
debounceMs?: number
): Promise<never>

Runs run once, then again on every change to a first-party file in the bundle. Never resolves — watch mode ends when the process is signalled — except to REJECT if the very first run throws, which is a script that never started rather than a bad edit to recover from.

Runs are serialised, and the FIRST run is serialised with them. It is the slowest of the session (bundle, browser launch, navigate), so it is the likeliest one to receive a save; left outside the guard, that save began a second run against the same page while the first was still navigating, and the two wedged each other until the watch timed out. A change arriving mid-run is remembered rather than dropped — the run in flight is already executing stale code.

Watches DIRECTORIES rather than the files themselves: editors save by writing a temp file and renaming it over the original, which severs a per-file watch on the very first save.

watch is injected so the serialisation is testable without a filesystem.

import { watchLoop } from './run.ts';

// Defined, not invoked: the returned promise settles only on a first-run failure.
function example() {
  return watchLoop([], () => Promise.resolve(), () => {});
}

Parameters

directories: string[]
run: () => Promise<void>
optional
watch: (
directory: string,
listener: () => void
) => void
optional
debounceMs: number = WATCH_DEBOUNCE_MS

Return Type

Promise<never>

Usage

import { watchLoop } from "lib/commands/run.ts";