Skip to content

Commit 573d201

Browse files
c-vetterphated
authored andcommitted
New: Add events option
1 parent 4fca0fb commit 573d201

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ Type: `Boolean`
9696

9797
Default: `true`
9898

99+
##### `options.events`
100+
101+
An event name or array of event names to listen for. Useful if you only need to watch specific events.
102+
103+
Type: `String | Array<String>`
104+
105+
Default: `[ 'add', 'change', 'unlink' ]`
106+
99107
##### other
100108

101109
Options are passed directly to [lodash.debounce][lodash-debounce] and [chokidar][chokidar], so all their options are supported. Any debounce-related options are documented in [lodash.debounce][lodash-debounce]. Any chokidar-related options are documented in [chokidar][chokidar].

index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ function assignNullish(objValue, srcValue) {
1010
}
1111

1212
var defaults = {
13-
ignoreInitial: true,
1413
delay: 200,
14+
events: ['add', 'change', 'unlink'],
15+
ignoreInitial: true,
1516
queue: true,
1617
};
1718

@@ -23,6 +24,10 @@ function watch(glob, options, cb) {
2324

2425
var opt = assignWith({}, defaults, options, assignNullish);
2526

27+
if (!Array.isArray(opt.events)) {
28+
opt.events = [opt.events];
29+
}
30+
2631
var queued = false;
2732
var running = false;
2833

@@ -54,12 +59,17 @@ function watch(glob, options, cb) {
5459
asyncDone(cb, runComplete);
5560
}
5661

62+
var fn;
5763
if (typeof cb === 'function') {
58-
var fn = debounce(onChange, opt.delay, opt);
59-
watcher
60-
.on('change', fn)
61-
.on('unlink', fn)
62-
.on('add', fn);
64+
fn = debounce(onChange, opt.delay, opt);
65+
}
66+
67+
function watchEvent(eventName) {
68+
watcher.on(eventName, fn);
69+
}
70+
71+
if (fn) {
72+
opt.events.forEach(watchEvent);
6373
}
6474

6575
return watcher;

test/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ describe('glob-watcher', function() {
4242
return del(outDir);
4343
});
4444

45+
after(function() {
46+
return del(outDir);
47+
});
48+
4549
it('only requires a glob and returns watcher', function(done) {
4650
watcher = watch(outGlob);
4751

@@ -248,4 +252,30 @@ describe('glob-watcher', function() {
248252
// So wait for `on('ready')`
249253
watcher.on('ready', changeFile);
250254
});
255+
256+
it('watches exactly the given event', function(done) {
257+
var spy = expect.createSpy()
258+
.andCall(function(cb) {
259+
cb();
260+
spy.andThrow(new Error('`Add` handler called for `change` event'));
261+
setTimeout(done, 500);
262+
changeFile();
263+
});
264+
265+
watcher = watch(outGlob, { events: 'add' }, spy);
266+
267+
watcher.on('ready', addFile);
268+
});
269+
270+
it('accepts multiple events to watch', function(done) {
271+
var spy = expect.createSpy()
272+
.andThrow(new Error('`Add`/`Unlink` handler called for `change` event'));
273+
274+
watcher = watch(outGlob, { events: ['add', 'unlink'] }, spy);
275+
276+
watcher.on('ready', function() {
277+
changeFile();
278+
setTimeout(done, 500);
279+
});
280+
});
251281
});

0 commit comments

Comments
 (0)