Skip to content

Commit

Permalink
Do not accumulate change listeners in watch mode (#2441)
Browse files Browse the repository at this point in the history
* Do not accumulate change listeners in watch mode

* preserve change listeners (#2443)
  • Loading branch information
lukastaegert authored and Rich-Harris committed Sep 5, 2018
1 parent 0258c70 commit 5c86099
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/Graph.ts
Expand Up @@ -137,7 +137,13 @@ export default class Graph {

this.pluginDriver = createPluginDriver(this, options, this.pluginCache, watcher);

if (watcher) watcher.on('change', id => this.pluginDriver.hookSeqSync('watchChange', [id]));
if (watcher) {
const handleChange = (id: string) => this.pluginDriver.hookSeqSync('watchChange', [id]);
watcher.on('change', handleChange);
watcher.once('restart', () => {
watcher.removeListener('change', handleChange);
});
}

if (typeof options.external === 'function') {
const external = options.external;
Expand Down
12 changes: 9 additions & 3 deletions src/watch/index.ts
Expand Up @@ -24,6 +24,7 @@ export class Watcher extends EventEmitter {
private rerun: boolean = false;
private tasks: Task[];
private succeeded: boolean = false;
private invalidatedIds: Set<string> = new Set();

constructor(configs: RollupWatchOptions[] = []) {
super();
Expand All @@ -42,7 +43,10 @@ export class Watcher extends EventEmitter {
this.removeAllListeners();
}

invalidate() {
invalidate(id?: string) {
if (id) {
this.invalidatedIds.add(id);
}
if (this.running) {
this.rerun = true;
return;
Expand All @@ -52,6 +56,9 @@ export class Watcher extends EventEmitter {

this.buildTimeout = setTimeout(() => {
this.buildTimeout = undefined;
this.invalidatedIds.forEach(id => this.emit('change', id));
this.invalidatedIds.clear();
this.emit('restart');
this.run();
}, DELAY);
}
Expand Down Expand Up @@ -156,7 +163,6 @@ export class Task {
}

invalidate(id: string, isTransformDependency: boolean) {
this.watcher.emit('change', id);
this.invalidated = true;
if (isTransformDependency) {
this.cache.modules.forEach(module => {
Expand All @@ -166,7 +172,7 @@ export class Task {
module.originalCode = null;
});
}
this.watcher.invalidate();
this.watcher.invalidate(id);
}

run() {
Expand Down
22 changes: 20 additions & 2 deletions test/watch/index.js
Expand Up @@ -105,7 +105,7 @@ describe('rollup.watch', () => {
});
});

it('passes file events to the watchChange plugin hook', () => {
it('passes file events to the watchChange plugin hook once for each change', () => {
let watchChangeCnt = 0;
return sander
.copydir('test/watch/samples/basic')
Expand Down Expand Up @@ -144,7 +144,25 @@ describe('rollup.watch', () => {
'END',
() => {
assert.equal(run('../_tmp/output/bundle.js'), 43);
assert.ok(watchChangeCnt >= 1);
assert.equal(watchChangeCnt, 1);
sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;');
},
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
assert.equal(run('../_tmp/output/bundle.js'), 43);
assert.equal(watchChangeCnt, 2);
sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;');
},
'START',
'BUNDLE_START',
'BUNDLE_END',
'END',
() => {
assert.equal(run('../_tmp/output/bundle.js'), 43);
assert.equal(watchChangeCnt, 3);
}
]);
});
Expand Down

0 comments on commit 5c86099

Please sign in to comment.