watcher - set a lower debounce delay

上级 16720eb3
......@@ -204,13 +204,7 @@ export class NsfwWatcherService extends Disposable implements IWatcherService {
const normalizedEvents = normalizeFileChanges(this.normalizeEvents(events, request, realBasePathDiffers, realBasePathLength));
this.emitEvents(normalizedEvents);
});
}, {
errorCallback: error => {
if (!watcher.token.isCancellationRequested) {
this.onError(error, watcher); // error handling only if we are not disposed yet
}
}
}).then(async nsfwWatcher => {
}, this.getOptions(watcher)).then(async nsfwWatcher => {
// Begin watching unless disposed already
if (!watcher.token.isCancellationRequested) {
......@@ -225,6 +219,23 @@ export class NsfwWatcherService extends Disposable implements IWatcherService {
});
}
protected getOptions(watcher: IWatcher): nsfw.Options {
return {
// We must install an error callback, otherwise any error
// that is thrown from the watcher will result in process exit
errorCallback: error => {
if (!watcher.token.isCancellationRequested) {
this.onError(error, watcher); // error handling only if we are not disposed yet
}
},
// The default delay of NSFW is 500 but we already do some
// debouncing in our code so we reduce the delay slightly
debounceMS: 100
};
}
private emitEvents(events: IDiskFileChange[]): void {
// Send outside
......
......@@ -50,7 +50,7 @@ export function normalizeFileChanges(changes: IDiskFileChange[]): IDiskFileChang
class EventNormalizer {
private readonly normalized: IDiskFileChange[] = [];
private readonly normalized = new Set<IDiskFileChange>();
private readonly mapPathToChange = new Map<string, IDiskFileChange>();
processEvent(event: IDiskFileChange): void {
......@@ -64,7 +64,7 @@ class EventNormalizer {
// ignore CREATE followed by DELETE in one go
if (currentChangeType === FileChangeType.ADDED && newChangeType === FileChangeType.DELETED) {
this.mapPathToChange.delete(event.path);
this.normalized.splice(this.normalized.indexOf(existingEvent), 1);
this.normalized.delete(existingEvent);
}
// flatten DELETE followed by CREATE into CHANGE
......@@ -83,7 +83,7 @@ class EventNormalizer {
// Otherwise store new
else {
this.normalized.push(event);
this.normalized.add(event);
this.mapPathToChange.set(event.path, event);
}
}
......@@ -99,7 +99,7 @@ class EventNormalizer {
// 1.) split ADD/CHANGE and DELETED events
// 2.) sort short deleted paths to the top
// 3.) for each DELETE, check if there is a deleted parent and ignore the event in that case
return this.normalized.filter(e => {
return Array.from(this.normalized).filter(e => {
if (e.type !== FileChangeType.DELETED) {
addOrChangeEvents.push(e);
......
......@@ -34,6 +34,17 @@ flakySuite('Recursive Watcher', () => {
await watcher.instance;
}
}
protected override getOptions(watcher: any) {
return {
...super.getOptions(watcher),
// required to let tests pass properly. with a smaller value
// somehow `fsevents` on macOS seems to report events from
// the past so that the event listening will assume wrongly
debounceMS: 500
};
}
}
let testDir: string;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册