Files: fd48051c9a0ccc1e5fd254db048758d8eff91383 / watch-all.js
1068 bytesRaw
1 | var resolve = require('./resolve') |
2 | var isObservable = require('./is-observable') |
3 | var onceIdle = require('./once-idle') |
4 | |
5 | module.exports = watchAll |
6 | |
7 | function watchAll (observables, listener, opts) { |
8 | if (!Array.isArray(observables)) { |
9 | observables = [ observables ] |
10 | } |
11 | |
12 | var broadcasting = false |
13 | var releases = observables.map(bind, broadcast) |
14 | |
15 | broadcast() |
16 | |
17 | return function () { |
18 | releases.forEach(tryInvoke) |
19 | releases.length = 0 |
20 | } |
21 | |
22 | function broadcast () { |
23 | if (opts && opts.idle) { |
24 | if (!broadcasting) { |
25 | broadcasting = true |
26 | onceIdle(broadcastNow) |
27 | } |
28 | } else if (opts && opts.nextTick) { |
29 | if (!broadcasting) { |
30 | broadcasting = true |
31 | setImmediate(broadcastNow) |
32 | } |
33 | } else { |
34 | broadcastNow() |
35 | } |
36 | } |
37 | |
38 | function broadcastNow () { |
39 | broadcasting = false |
40 | listener.apply(this, observables.map(resolve)) |
41 | } |
42 | } |
43 | |
44 | function bind (value) { |
45 | if (isObservable(value)) { |
46 | return value(this) |
47 | } |
48 | } |
49 | |
50 | function tryInvoke (value) { |
51 | if (typeof value === 'function') { |
52 | return value() |
53 | } |
54 | } |
55 |
Built with git-ssb-web