Files: bc685ac97855accf26bcdf7168da942ffcdccd1a / watch-all.js
900 bytesRaw
1 | var resolve = require('./resolve') |
2 | var isObservable = require('./is-observable') |
3 | |
4 | module.exports = watchAll |
5 | |
6 | function 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 | |
38 | function bind (value) { |
39 | if (isObservable(value)) { |
40 | return value(this) |
41 | } |
42 | } |
43 | |
44 | function tryInvoke (value) { |
45 | if (typeof value === 'function') { |
46 | return value() |
47 | } |
48 | } |
49 |
Built with git-ssb-web