forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_watcher.ts
More file actions
115 lines (91 loc) · 3.46 KB
/
file_watcher.ts
File metadata and controls
115 lines (91 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Typing info only.
import * as _fs from 'fs';
import Stats from './node_fs_stats';
const EventEmitter = require('events');
interface IWatchEntry {
filename: string;
watcher: any;
curr?: Stats;
recursive?: boolean;
persistent?: boolean;
callback?: ((event: string, filename: string) => any) | undefined;
fileCallback?: (curr: Stats, prev: Stats) => void;
}
export class FileWatcher {
public triggerWatch(filename: string, event: 'change' | 'rename', newStats?: Stats) {
const validEntries = this.watchEntries.filter(entry => {
if (entry.filename === filename) {
return true;
}
if (entry.recursive && filename.startsWith(entry.filename)) {
return true;
}
return false;
});
validEntries.forEach(entry => {
if (entry.callback) {
entry.callback(event, filename);
}
const newStatsArg = newStats || entry.curr;
const oldStatsArg = entry.curr || newStats;
if (newStatsArg && oldStatsArg && entry.fileCallback) {
entry.fileCallback(newStatsArg, oldStatsArg);
entry.curr = newStatsArg;
}
entry.watcher.emit(event);
if (!entry.persistent) {
this.removeEntry(entry);
}
});
}
public watch(filename: string, listener?: (event: string, filename: string) => any): _fs.FSWatcher;
public watch(filename: string, options: { recursive?: boolean; persistent?: boolean; }, listener?: (event: string, filename: string) => any): _fs.FSWatcher;
public watch(filename: string, arg2: any, listener: (event: string, filename: string) => any = (() => {})): _fs.FSWatcher {
const watcher = new EventEmitter();
const watchEntry: IWatchEntry = {
filename,
watcher,
};
watcher.close = () => {
this.removeEntry(watchEntry);
};
if (typeof arg2 === 'object') {
watchEntry.recursive = arg2.recursive;
watchEntry.persistent = arg2.persistent === undefined ? true : arg2.persistent;
watchEntry.callback = listener;
} else if (typeof arg2 === 'function') {
watchEntry.callback = arg2;
}
this.watchEntries.push(watchEntry);
return watchEntry.watcher;
}
public watchFile(curr: Stats, filename: string, listener: (curr: Stats, prev: Stats) => void): void;
public watchFile(curr: Stats, filename: string, options: { persistent?: boolean; interval?: number; }, listener: (curr: Stats, prev: Stats) => void): void;
public watchFile(curr: Stats, filename: string, arg2: any, listener: (curr: Stats, prev: Stats) => void = (() => {})): void {
const watcher = new EventEmitter();
const watchEntry: IWatchEntry = {
filename,
watcher,
curr,
};
watcher.close = () => {
this.removeEntry(watchEntry);
};
if (typeof arg2 === 'object') {
watchEntry.recursive = arg2.recursive;
watchEntry.persistent = arg2.persistent === undefined ? true : arg2.persistent;
watchEntry.fileCallback = listener;
} else if (typeof arg2 === 'function') {
watchEntry.fileCallback = arg2;
}
this.watchEntries.push(watchEntry);
return watchEntry.watcher;
}
unwatchFile(filename: string, listener: (curr: Stats, prev: Stats) => void): any {
this.watchEntries = this.watchEntries.filter(entry => entry.filename !== filename && entry.fileCallback !== listener);
}
private watchEntries: IWatchEntry[] = [];
private removeEntry(watchEntry: IWatchEntry) {
this.watchEntries = this.watchEntries.filter(en => en !== watchEntry);
}
}