UNPKG

4.71 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import { EventEmitter } from "events";
4import Watcher = require("./Watcher");
5
6interface Entry {
7 /** A point in time at which is it safe to say all changes happened before that */
8 safeTime: number;
9 /** Only for file entries: the last modified timestamp of the file */
10 timestamp: number;
11}
12
13declare class Watchpack extends EventEmitter {
14 aggregatedChanges: Set<string>;
15 aggregatedRemovals: Set<string>;
16
17 aggregateTimeout: NodeJS.Timer;
18 dirWatchers: Watcher[];
19 fileWatchers: Watcher[];
20 /** Last modified times for files by path */
21 mtimes: {
22 [path: string]: number;
23 };
24 options: Watchpack.WatchOptions;
25 paused: boolean;
26 watcherOptions: Watchpack.WatcherOptions;
27
28 constructor(options: Watchpack.WatchOptions);
29
30 /**
31 * Starts watching these files and directories
32 * Calling this again will override the files and directories
33 */
34 watch(options: {
35 /**
36 * Can be files or directories
37 * For files: content and existence changes are tracked
38 * For directories: only existence and timestamp changes are tracked
39 */
40 files?: Iterable<string>;
41 /**
42 * Can only be directories
43 * Directory content (and content of children, ...) and existence changes are tracked.
44 * For files: content and existence changes are tracked
45 * Assumed to exist, when directory is not found without further information a remove event is emitted
46 */
47 directories?: Iterable<string>;
48 /**
49 * Can be files or directories
50 * Only existence changes are tracked
51 * Assued to not exist, no remove event is emitted when not found initially
52 */
53 missing?: Iterable<string>;
54 startTime?: number;
55 }): void;
56
57 on(
58 eventName: "change",
59 listener: (
60 /** The changed file or directory */
61 filePath: string,
62 /** The last modified time of the changed file */
63 modifiedTime: number,
64 /** Textual information how this change was detected */
65 explanation: string,
66 ) => void,
67 ): this;
68
69 on(
70 eventName: "remove",
71 listener: (
72 /** The removed file or directory */
73 filePath: string,
74 /** Textual information how this change was detected */
75 explanation: string,
76 ) => void,
77 ): this;
78
79 on(
80 eventName: "aggregated",
81 listener: (
82 /** Set of all changed files */
83 changes: Set<string>,
84 /** Set of all removed files */
85 removals: Set<string>,
86 ) => void,
87 ): this;
88
89 /**
90 * Stops emitting events, but keeps watchers open
91 * The next "watch" call can reuse the watchers
92 * The watcher will keep aggregating events which can be received with `getAggregated()`
93 */
94 pause(): void;
95
96 /**
97 * Stops emitting events and closes all watchers
98 */
99 close(): void;
100
101 /**
102 * Returns the current aggregated info and removes that from the watcher
103 * The next aggregated event won't include that info and will only emitted when futher changes happen
104 * Can be used when paused
105 */
106 getAggregated(): {
107 changes: Set<string>;
108 removals: Set<string>;
109 };
110
111 /**
112 * Collects time info objects for all known files and directories
113 * This includes info from files not directly watched
114 */
115 collectTimeInfoEntries(fileInfoEntries: Map<string, Entry>, directoryInfoEntries: Map<string, Entry>): void;
116
117 /**
118 * Returns a `Map` with all known time info objects for files and directories
119 * Similar to `collectTimeInfoEntries()` but returns a single map with all entries
120 */
121 getTimeInfoEntries(): Map<string, Entry>;
122
123 /**
124 * Returns an object with all known change times for files
125 * This include timestamps from files not directly watched
126 * Key: absolute path, value: timestamp as number
127 * @deprecated
128 */
129 getTimes(): {
130 [path: string]: number;
131 };
132
133 _fileWatcher(file: string, watcher: Watcher): Watcher;
134
135 _dirWatcher(item: string, watcher: Watcher): Watcher;
136
137 _onChange(item: string, mtime: number, file?: string): void;
138
139 _onTimeout(): void;
140}
141
142declare namespace Watchpack {
143 interface WatcherOptions {
144 ignored?: string[] | string | RegExp | ((path: string) => boolean) | undefined;
145 poll?: boolean | number | undefined;
146 followSymlinks?: boolean;
147 }
148 interface WatchOptions extends WatcherOptions {
149 aggregateTimeout?: number | undefined;
150 }
151}
152
153export = Watchpack;
154
\No newline at end of file