File-watching primitives for NetScript: a pipeline-based FileWatcher, composable filters and
failure tracking for local and network filesystems. Use createWatcher when a tool or service must
react to file changes.
With no explicit strategy, the watcher selects native OS notifications with polling fallback for
local paths, or polling for network paths. Set strategy when the choice must be explicit.
Factory function to create a FileWatcher with the given options.
Watcher
Symbol
Kind
Member
Signature
Description
FileWatcher
class
constructor
newFileWatcher(options: WatcherOptions)
Composable file watcher that combines a strategy with a filter pipeline; auto-selects native vs. polling.
watch
async *watch(): AsyncGenerator<WatchEvent>
Start watching and yield file events through the filter pipeline.
stop
stop(): void
Stop the watcher by signaling abort.
running
getrunning(): boolean
Whether the watcher is currently running.
Strategy selection and failures
Options
Behavior
strategy: 'native'
Native OS notifications only. A setup or runtime failure emits an onError diagnostic and rejects the iterator; it never starts polling.
strategy: 'polling'
Polling without a native attempt.
strategy: 'hybrid'
Native first, with polling fallback for native failures other than a missing/removed root.
No strategy, forcePolling: true
Polling, preserving the legacy option.
No strategy, network path
Polling selected automatically.
No strategy, local path
Hybrid selected automatically.
An explicit strategy takes precedence over forcePolling and network-path heuristics. Unknown
strategy identifiers fail with TypeError; there is no implicit fallback for them.
WATCH_STRATEGIES exports the accepted built-in values.
onReady runs after the strategy acquires resources, independently of matching file events.
Polling retains its consecutive-access-failure grace period during startup, but does not announce
readiness until every root has been scanned successfully. A share that returns within that budget
continues in the same polling session; persistent startup failures reject for supervisor recovery.
Files present when that first successful polling scan completes establish its initial snapshot;
changes during the startup access outage are not replayed.
onError receives a WatcherErrorEvent containing type: 'error', configured paths, the original
error, and a Date timestamp. This diagnostic is separate from the WatchEvent stream: file
filters and file handlers never receive an error as a file change. The original error still rejects
watch(), even if the diagnostic hook throws. Cancellation completes without an error diagnostic.
Removing a watched root invalidates the native subscription even when only create events were
requested. That failure propagates in native and hybrid modes so a supervisor can reopen the root;
it is not hidden by polling. A standalone watcher does not retry automatically. Create a fresh
watcher for each session or recovery attempt, and call stop() to release it. File-watch triggers
use a supervising adapter that retries with bounded backoff and exposes
per-trigger availability.
Filters consume an async iterable of events and yield only the events that pass. They run in
configured order inside the FileWatcher pipeline, and each implements the WatchFilter contract.
Symbol
Kind
Member
Signature
Description
GlobFilter
class
constructor
newGlobFilter(patterns: readonlystring[])
Yields only events whose filenames match at least one glob pattern.
The strategy classes (NativeStrategy, PollingStrategy, HybridStrategy) and their option
interfaces (NativeStrategyOptions, PollingStrategyOptions, HybridStrategyOptions) are
@internal and not part of the public contract. FileWatcher selects one from the configured
paths; construct watchers with createWatcher or new FileWatcher(...).