UNPKG

@bscotch/debounce-watch

Version:

Monitor files for changes, debounce events, and finally trigger consequences.

84 lines 3.02 kB
/** * @file General watcher utility for re-running functions after debounced * file-change events */ /// <reference types="node" /> /// <reference types="node" /> import type { FSWatcher, WatchOptions } from 'chokidar'; import type { Stats } from 'fs'; import path from 'path'; type Logger = { [level in 'info' | 'warn' | 'debug' | 'error']: (...args: any[]) => void; }; declare const watcherEventNames: readonly ["add", "change", "unlink", "addDir", "unlinkDir"]; export type WatcherEventName = typeof watcherEventNames[number]; export interface WatcherEvent { event: WatcherEventName; relativePath: string; absolutePath: string; parsedPath: ReturnType<typeof path['parse']>; stats: Stats; } export type DebouncedEventsProcessor = ( /** All events that occurred during debouncing. */ events: WatcherEvent[]) => any | Promise<any>; export interface DebounceWatchOptions { /** * Restrict file-related watching and events to * a subset of extensions. If folder-related events * are listed in `options.events`, this setting * will not prevent those events from occurring. * * E.g. 'png' or ['png','jpg'] */ onlyFileExtensions?: string | string[]; /** * By default, only ['add','change'] events trigger `onChange()`. * You can override this with your preferred list of events. */ events?: WatcherEventName[]; /** * Seconds to wait for additional changes before running * the target function. Default is subject to change. */ debounceWaitSeconds?: number; /** * By default, if the last onChange invokation has not completed * then further invokations will have to wait until it is complete. * If set to `true`, then this will not be enforced. */ allowOverlappingRuns?: boolean; /** * Custom logging based on severity level. * Defaults to console.log/error, with high * verbosity when process.env.DEBUG is "true" */ logger?: Logger; /** * This function uses Chokidar watcher options that are * robust and predictable (if resource-costly): polling, no glob patterns, etc. * If you want to overwrite the defaults, you should set * *all* parameters since the defaults this function uses * are not the same as what Chokidar uses. */ chokidarWatchOptions?: WatchOptions; } /** * Watch for file system events and debounce them. Collect * events during debouncing, and once events have stopped * call a target function while providing the list of events. */ export declare function debounceWatch( /** * Function to call after debounced change events. * Currently no arguments are passed to the function, * but that could change. */ eventProcessor: DebouncedEventsProcessor, watchFolder: string, options?: DebounceWatchOptions): Promise<FSWatcher>; /** * @alias debounceWatch * @deprecated */ export declare const runAfterDebouncedFileSystemEvents: typeof debounceWatch; export {}; //# sourceMappingURL=runner.d.ts.map