Files: 784a83eb521d4e284478ac2ee4160e2e85a617bc / value.js
718 bytesRaw
1 | module.exports = Observable |
2 | |
3 | function Observable (value) { |
4 | var listeners = [] |
5 | value = value === undefined ? null : value |
6 | |
7 | observable.set = function (v) { |
8 | value = v |
9 | |
10 | for (var i = 0, len = listeners.length; i < len; i++) { |
11 | listeners[i](v) |
12 | } |
13 | } |
14 | |
15 | return observable |
16 | |
17 | function observable (listener) { |
18 | if (!listener) { |
19 | return value |
20 | } |
21 | |
22 | if (typeof listener !== 'function') { |
23 | throw new Error('Listeners must be functions.') |
24 | } |
25 | |
26 | listeners.push(listener) |
27 | |
28 | return function remove () { |
29 | for (var i = 0, len = listeners.length; i < len; i++) { |
30 | if (listeners[i] === listener) { |
31 | listeners.splice(i, 1) |
32 | break |
33 | } |
34 | } |
35 | } |
36 | } |
37 | } |
38 |
Built with git-ssb-web