Skip to content

Commit

Permalink
Refactor and simplify watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jan 17, 2020
1 parent c70cd01 commit a29affd
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 245 deletions.
16 changes: 8 additions & 8 deletions LICENSE.md
Expand Up @@ -112,7 +112,7 @@ Repository: https://github.com/acornjs/acorn.git
## anymatch
License: ISC
By: Elan Shanker
Repository: git+https://github.com/micromatch/anymatch.git
Repository: https://github.com/micromatch/anymatch

> The ISC License
>
Expand All @@ -135,14 +135,14 @@ Repository: git+https://github.com/micromatch/anymatch.git
## binary-extensions
License: MIT
By: Sindre Sorhus
Repository: git+https://github.com/sindresorhus/binary-extensions.git
Repository: sindresorhus/binary-extensions

---------------------------------------

## braces
License: MIT
By: Jon Schlinkert, Brian Woodward, Elan Shanker, Eugene Sharygin, hemanth.hm
Repository: git+https://github.com/micromatch/braces.git
Repository: micromatch/braces

> The MIT License (MIT)
>
Expand Down Expand Up @@ -207,7 +207,7 @@ Repository: sindresorhus/date-time
## fill-range
License: MIT
By: Jon Schlinkert, Edo Rivai, Paul Miller, Rouven Weßling
Repository: git+https://github.com/jonschlinkert/fill-range.git
Repository: jonschlinkert/fill-range

> The MIT License (MIT)
>
Expand Down Expand Up @@ -236,7 +236,7 @@ Repository: git+https://github.com/jonschlinkert/fill-range.git
## glob-parent
License: ISC
By: Gulp Team, Elan Shanker, Blaine Bublitz
Repository: git+https://github.com/gulpjs/glob-parent.git
Repository: gulpjs/glob-parent

> The ISC License
>
Expand Down Expand Up @@ -288,7 +288,7 @@ Repository: git://github.com/isaacs/inherits
## is-binary-path
License: MIT
By: Sindre Sorhus
Repository: git+https://github.com/sindresorhus/is-binary-path.git
Repository: sindresorhus/is-binary-path

---------------------------------------

Expand Down Expand Up @@ -353,7 +353,7 @@ Repository: micromatch/is-glob
## is-number
License: MIT
By: Jon Schlinkert, Olsten Larck, Rouven Weßling
Repository: git+https://github.com/jonschlinkert/is-number.git
Repository: jonschlinkert/is-number

> The MIT License (MIT)
>
Expand Down Expand Up @@ -668,7 +668,7 @@ Repository: sindresorhus/time-zone
## to-regex-range
License: MIT
By: Jon Schlinkert, Rouven Weßling
Repository: git+https://github.com/micromatch/to-regex-range.git
Repository: micromatch/to-regex-range

> The MIT License (MIT)
>
Expand Down
64 changes: 64 additions & 0 deletions src/watch/fileWatcher.ts
@@ -0,0 +1,64 @@
import chokidar, { FSWatcher } from 'chokidar';
import { ChokidarOptions } from '../rollup/types';
import { Task } from './watch';

export class FileWatcher {
private chokidarOptions: ChokidarOptions;
private task: Task;
private transformWatchers = new Map<string, FSWatcher>();
private watcher: FSWatcher;

constructor(task: Task, chokidarOptions: ChokidarOptions) {
this.chokidarOptions = chokidarOptions;
this.task = task;
this.watcher = this.createWatcher(null);
}

close() {
this.watcher.close();
for (const watcher of this.transformWatchers.values()) {
watcher.close();
}
}

unwatch(id: string) {
this.watcher.unwatch(id);
const transformWatcher = this.transformWatchers.get(id);
if (transformWatcher) {
this.transformWatchers.delete(id);
transformWatcher.close();
}
}

watch(id: string, isTransformDependency: boolean) {
if (isTransformDependency) {
const watcher = this.transformWatchers.get(id) || this.createWatcher(id);
watcher.add(id);
this.transformWatchers.set(id, watcher);
} else {
this.watcher.add(id);
}
}

private createWatcher(transformWatcherId: string | null): FSWatcher {
const handleChange = transformWatcherId
? () => {
// unwatching and watching fixes an issue with chokidar where on certain systems,
// a file that was unlinked and immediately recreated would create a change event
// but then no longer any further events
watcher.unwatch(transformWatcherId);
watcher.add(transformWatcherId);
this.task.invalidate(transformWatcherId, true);
}
: (id: string) => {
watcher.unwatch(id);
watcher.add(id);
this.task.invalidate(id, false);
};
const watcher = chokidar
.watch([], this.chokidarOptions)
.on('change', handleChange)
.on('unlink', handleChange);
return watcher;
}
}
113 changes: 0 additions & 113 deletions src/watch/fileWatchers.ts

This file was deleted.

0 comments on commit a29affd

Please sign in to comment.