git ssb

1+

Matt McKegg / mutant



Commit 994654fc520c8585e99de99665ac7140ce87a446

watch-all: add nextTick option

Matt McKegg committed on 9/4/2016, 12:56:54 PM
Parent: 2b5c272499d06a199576e602fc62fb998954ad18

Files changed

watch-all.jschanged
watch-all.jsView
@@ -2,22 +2,36 @@
22 var isObservable = require('./is-observable')
33
44 module.exports = watchAll
55
6-function watchAll (observables, listener) {
6+function watchAll (observables, listener, opts) {
77 if (!Array.isArray(observables)) {
88 observables = [ observables ]
99 }
1010
11+ var broadcasting = false
1112 var releases = observables.map(bind, broadcast)
13+
1214 broadcast()
1315
1416 return function () {
1517 releases.forEach(tryInvoke)
1618 releases.length = 0
1719 }
1820
1921 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
2034 listener.apply(this, observables.map(resolve))
2135 }
2236 }
2337

Built with git-ssb-web