git ssb

1+

Matt McKegg / mutant



Tree: 7f33edf4e20870b7b514970eda78bcfeb2791a11

Files: 7f33edf4e20870b7b514970eda78bcfeb2791a11 / value.js

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

Built with git-ssb-web