git ssb

1+

Matt McKegg / mutant



Tree: be0df157d2e308a5ea7014d2d266d2d48f87699d

Files: be0df157d2e308a5ea7014d2d266d2d48f87699d / value.js

718 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 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