Skip to content

Commit 98b24fa

Browse files
Merge pull request #797 from pattern-lab/fix-eventemitterleak
Fix watches and event listener leaks
2 parents bf4e0e8 + 192f9d0 commit 98b24fa

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

core/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,14 @@ const patternlab_module = function(config) {
372372
copier()
373373
.copyAndWatch(patternlab.config.paths, patternlab, options)
374374
.then(() => {
375-
this.events.on('patternlab-pattern-change', () => {
375+
this.events.once(events.PATTERNLAB_PATTERN_CHANGE, () => {
376376
if (!patternlab.isBusy) {
377377
return this.build(options);
378378
}
379379
return Promise.resolve();
380380
});
381381

382-
this.events.on('patternlab-global-change', () => {
382+
this.events.once(events.PATTERNLAB_GLOBAL_CHANGE, () => {
383383
if (!patternlab.isBusy) {
384384
return this.build(
385385
Object.assign({}, options, { cleanPublic: true }) // rebuild everything

core/lib/copier.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ const copier = () => {
8989
return Promise.all(copyPromises).then(() => {
9090
//we need to special case patterns/**/*.md|.json|.pattern-extensions as well as the global structures
9191
if (options.watch) {
92-
watchPatternLabFiles(patternlab, assetDirectories, basePath);
92+
return watchPatternLabFiles(patternlab, assetDirectories, basePath);
9393
}
94+
return Promise.resolve();
9495
});
9596
};
9697

core/lib/serve.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22
const path = require('path');
33
const liveServer = require('@pattern-lab/live-server');
4+
5+
const events = require('./events');
46
const logger = require('./log');
57

68
const serve = patternlab => {
@@ -25,7 +27,7 @@ const serve = patternlab => {
2527
);
2628

2729
// watch for asset changes, and reload appropriately
28-
patternlab.events.on('patternlab-asset-change', data => {
30+
patternlab.events.on(events.PATTERNLAB_PATTERN_ASSET_CHANGE, data => {
2931
if (serverReady) {
3032
if (data.file.indexOf('css') > -1) {
3133
liveServer.refreshCSS();
@@ -36,7 +38,7 @@ const serve = patternlab => {
3638
});
3739

3840
//watch for pattern changes, and reload
39-
patternlab.events.on('patternlab-pattern-change', () => {
41+
patternlab.events.on(events.PATTERNLAB_PATTERN_CHANGE, () => {
4042
if (serverReady) {
4143
liveServer.reload();
4244
}

core/lib/watchPatternLabFiles.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,18 @@ const watchPatternLabFiles = (
7474
_.each(patternWatches, patternWatchPath => {
7575
logger.debug(`Pattern Lab is watching ${patternWatchPath} for changes`);
7676

77+
if (patternlab.watchers[patternWatchPath]) {
78+
patternlab.watchers[patternWatchPath].close();
79+
}
80+
7781
const patternWatcher = chokidar.watch(path.resolve(patternWatchPath), {
7882
ignored: /(^|[\/\\])\../,
7983
ignoreInitial: true,
8084
awaitWriteFinish: {
8185
stabilityThreshold: 200,
8286
pollInterval: 100,
8387
},
88+
persistent: !watchOnce,
8489
});
8590

8691
//watch for changes and rebuild
@@ -100,13 +105,16 @@ const watchPatternLabFiles = (
100105
file: p,
101106
});
102107
});
108+
109+
patternlab.watchers[patternWatchPath] = patternWatcher;
103110
});
104111

105112
logger.info(
106113
`Pattern Lab is watching for changes to files under ${
107114
assetDirectories.source.root
108115
}`
109116
);
117+
return Promise.resolve();
110118
};
111119

112120
module.exports = watchPatternLabFiles;

test/watchPatternLabFiles_tests.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const _ = require('lodash');
4+
const tap = require('tap');
5+
const rewire = require('rewire');
6+
const path = require('path');
7+
8+
const util = require('./util/test_utils.js');
9+
const watchPatternLabFiles = rewire('../core/lib/watchPatternLabFiles');
10+
11+
const patterns_dir = './test/files/_patterns';
12+
13+
tap.test(
14+
'watchPatternLabFiles - adds watcher to patternlab.watchers for given patternWatchPath',
15+
test => {
16+
const pl = util.fakePatternLab(patterns_dir, {
17+
watchers: [],
18+
engines: {},
19+
});
20+
21+
pl.engines.getSupportedFileExtensions = () => {
22+
return ['.mustache'];
23+
};
24+
25+
watchPatternLabFiles(
26+
pl,
27+
{
28+
source: {
29+
data: '_data',
30+
meta: '_meta',
31+
patterns: 'patterns',
32+
},
33+
},
34+
'/foo',
35+
true
36+
);
37+
38+
// should have two for _data and _meta
39+
// should have five for '.json', '.yml', '.yaml', '.md' and '.mustache'
40+
test.equals(Object.keys(pl.watchers).length, 7);
41+
42+
test.end();
43+
}
44+
);

0 commit comments

Comments
 (0)