Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Observable
function Observable(init_value) {
var _value = init_value;
var subscriber = [];
function obs() {
if (arguments.length) {
_value = arguments[0];
obs.trigger();
return this;
} else {
return _value;
}
}
obs.on = function (fn) {
subscriber.push(fn);
return this;
};
obs.off = function (fn) {
subscriber.forEach(function (f, i) {
if (fn === f) subscriber.splice(i, 1);
})
};
obs.trigger = function () {
subscriber.map(function (fn) {
fn(_value);
});
}
return obs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment