Commit 994654fc520c8585e99de99665ac7140ce87a446
watch-all: add nextTick option
Matt McKegg committed on 9/4/2016, 12:56:54 PMParent: 2b5c272499d06a199576e602fc62fb998954ad18
Files changed
watch-all.js | changed |
watch-all.js | ||
---|---|---|
@@ -2,22 +2,36 @@ | ||
2 | 2 | var isObservable = require('./is-observable') |
3 | 3 | |
4 | 4 | module.exports = watchAll |
5 | 5 | |
6 | -function watchAll (observables, listener) { | |
6 | +function watchAll (observables, listener, opts) { | |
7 | 7 | if (!Array.isArray(observables)) { |
8 | 8 | observables = [ observables ] |
9 | 9 | } |
10 | 10 | |
11 | + var broadcasting = false | |
11 | 12 | var releases = observables.map(bind, broadcast) |
13 | + | |
12 | 14 | broadcast() |
13 | 15 | |
14 | 16 | return function () { |
15 | 17 | releases.forEach(tryInvoke) |
16 | 18 | releases.length = 0 |
17 | 19 | } |
18 | 20 | |
19 | 21 | function broadcast () { |
22 | + if (opts && opts.nextTick) { | |
23 | + if (!broadcasting) { | |
24 | + broadcasting = true | |
25 | + setImmediate(broadcastNow) | |
26 | + } | |
27 | + } else { | |
28 | + broadcastNow() | |
29 | + } | |
30 | + } | |
31 | + | |
32 | + function broadcastNow () { | |
33 | + broadcasting = false | |
20 | 34 | listener.apply(this, observables.map(resolve)) |
21 | 35 | } |
22 | 36 | } |
23 | 37 |
Built with git-ssb-web