Files: ba938d4692fe73ad9200093bf9c44381ab8373e1 / watch-all.js
636 bytesRaw
1 | var resolve = require('./resolve') |
2 | var isObservable = require('./is-observable') |
3 | |
4 | module.exports = watchAll |
5 | |
6 | function watchAll (observables, listener) { |
7 | if (!Array.isArray(observables)) { |
8 | observables = [ observables ] |
9 | } |
10 | |
11 | var releases = observables.map(bind, broadcast) |
12 | broadcast() |
13 | |
14 | return function () { |
15 | releases.forEach(tryInvoke) |
16 | releases.length = 0 |
17 | } |
18 | |
19 | function broadcast () { |
20 | listener.apply(this, observables.map(resolve)) |
21 | } |
22 | } |
23 | |
24 | function bind (value) { |
25 | if (isObservable(value)) { |
26 | return value(this) |
27 | } |
28 | } |
29 | |
30 | function tryInvoke (value) { |
31 | if (typeof value === 'function') { |
32 | return value() |
33 | } |
34 | } |
35 |
Built with git-ssb-web