git ssb

1+

Matt McKegg / mutant



Tree: 6bed2a0570765da36a16c719c30c833cc7994f5d

Files: 6bed2a0570765da36a16c719c30c833cc7994f5d / watch-all.js

900 bytesRaw
1var resolve = require('./resolve')
2var isObservable = require('./is-observable')
3
4module.exports = watchAll
5
6function watchAll (observables, listener, opts) {
7 if (!Array.isArray(observables)) {
8 observables = [ observables ]
9 }
10
11 var broadcasting = false
12 var releases = observables.map(bind, broadcast)
13
14 broadcast()
15
16 return function () {
17 releases.forEach(tryInvoke)
18 releases.length = 0
19 }
20
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
34 listener.apply(this, observables.map(resolve))
35 }
36}
37
38function bind (value) {
39 if (isObservable(value)) {
40 return value(this)
41 }
42}
43
44function tryInvoke (value) {
45 if (typeof value === 'function') {
46 return value()
47 }
48}
49

Built with git-ssb-web