Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created August 29, 2013 21:54
Show Gist options
  • Save Raynos/6383916 to your computer and use it in GitHub Desktop.
Save Raynos/6383916 to your computer and use it in GitHub Desktop.
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