File_Watcher module

init

procedure · 1 overloadSource

Sets up watcher and returns whether that worked. Call it on an empty watcher, once; afterwards add folders with add_directories.

It returns false, and leaves the watcher empty, for a null callback, a negative or non-finite window, a buffer size of 0 or less, or when the system refuses. Calling it on a watcher that is already set up fails an assertion. Pair each successful call with deinit.

Example
on_change :: (watcher: *File_Watcher(void), change: *File_Change, data: *void) {}

watcher: File_Watcher;
assert(!init(*watcher, on_change, merge_window_seconds = -1));
assert(!init(*watcher, on_change, event_buffer_size = 0));
deinit(*watcher); // nothing was set up, so nothing to release

Doesn't run in the browser (skipped on wasm).

init :: (
    watcher: *$T/File_Watcher,
    callback: T.File_Watcher_Callback,
    user_data: *T.User_Data = null,
    events_to_watch: File_Change_Event = .ALL_EVENTS,
    merge_window_seconds: float = 0.1,
    watch_recursively := true,
    verbose := false,
    event_buffer_size: s32 = DEFAULT_BUFFER_SIZE
) -> success: bool

Sets up watcher and returns whether that worked. Call it on an empty watcher, once; afterwards add folders with add_directories.

  • callback is called by process_changes for each change, as callback(watcher, change, user_data). It may not be null.
  • user_data is passed on to the callback.
  • events_to_watch selects the events worth reporting (default .ALL_EVENTS); others are dropped.
  • merge_window_seconds (default 0.1) is how long a path must be quiet before its changes are reported, so a burst of writes to one file gives one report. 0 reports at the next process_changes.
  • watch_recursively (default true) includes the sub-folders of watched folders, also ones made later.
  • event_buffer_size sizes the system's event buffer for each watched folder (default 8000); on Linux a burst of changes that overflows it is reported as SCAN_CHILDREN.
  • verbose logs each change just before its callback, through context.logger: the path and the events.

It returns false, and leaves the watcher empty, for a null callback, a negative or non-finite window, a buffer size of 0 or less, or when the system refuses. Calling it on a watcher that is already set up fails an assertion. Pair each successful call with deinit.

Parameters

watcher: *$T/File_Watcher
callback: T.File_Watcher_Callback
user_data: *T.User_Data
events_to_watch: File_Change_Event
merge_window_seconds: float
watch_recursively := true
verbose := false
event_buffer_size: s32

Returns

success: bool
Example
on_change :: (watcher: *File_Watcher(void), change: *File_Change, data: *void) {}

watcher: File_Watcher;
assert(!init(*watcher, on_change, merge_window_seconds = -1));
assert(!init(*watcher, on_change, event_buffer_size = 0));
deinit(*watcher); // nothing was set up, so nothing to release

Doesn't run in the browser (skipped on wasm).

See also