Created
August 29, 2013 21:54
-
-
Save Raynos/6383916 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = Observable | |
function Observable(value) { | |
var listeners = [] | |
value = value === undefined ? null : value | |
observable.set = function (v) { | |
value = v | |
listeners.forEach(function (f) { | |
return f(v) | |
}) | |
} | |
observable.listen = function (listener) { | |
listeners.push(listener) | |
return function remove() { | |
listeners.splice(listeners.indexOf(listener), 1) | |
} | |
} | |
return observable | |
function observable(listener) { | |
if (!listener) { | |
return value | |
} | |
listeners.push(listener) | |
listener(value) | |
return function remove() { | |
listeners.splice(listeners.indexOf(listener), 1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment