Last active
August 29, 2015 14:09
-
-
Save benqus/205a2305850bedae9a43 to your computer and use it in GitHub Desktop.
Observable - Pub/Sub
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
/** | |
* Class inheritance helper | |
* @param {function} Class - sub class | |
* @param {function} Super - super class | |
*/ | |
function extend(Class, Super) { | |
if (typeof Class !== 'function' && typeof Super !== 'function') { | |
return; | |
} | |
Class.prototype = Object.create(Super.prototype); | |
Class.prototype.constructor = Class; | |
}; |
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
/** | |
* Provides basic Pub/Sub interface and functionality | |
* | |
* @class Observable | |
*/ | |
function Observable() { | |
this.subscribers = []; | |
} | |
/** | |
* Publishes the provided arguments to the subscribers. | |
*/ | |
Observable.prototype.publish = function () { | |
var args = Array.prototype.slice.call(arguments); | |
this.subscribers.forEach(function (subscriber) { | |
subscriber.onChange.apply(subscriber, args); | |
}); | |
}; | |
/** | |
* Ensures that the subscriber will be notified when publishing. | |
* @param {object} subscriber | |
* @property {function} subscriber.onChange - callback to be executed when publishing | |
*/ | |
Observable.prototype.subscribe = function (subscriber) { | |
var index = this.subscribers.indexOf(subscriber); | |
if (index === -1 && typeof subscriber.onChange === 'function') { | |
this.subscribers.push(subscriber); | |
} | |
}; | |
Observable.prototype.unsubscribe = function (subscriber) { | |
var index = this.subscribers.indexOf(subscriber); | |
if (index > -1) { | |
this.subscribers.splice(index, 1); | |
} | |
}; |
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
/** | |
* Stores a value and provides Pub/Sub features for value changes. | |
* | |
* @class Property | |
* @extends {Observable} | |
* | |
* @param {*} value - initial value | |
*/ | |
function Property(value) { | |
Observable.apply(this, arguments); | |
this.value = value; | |
} | |
extend(Property, Observable); | |
Property.prototype.get = function () { | |
return this.value; | |
}; | |
Property.prototype.set = function (value) { | |
this.value = value; | |
this.publish(this); | |
}; |
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
/** | |
* Base Subscriber class to provide a noop Subscriber#onChange method | |
*/ | |
function Subscriber() {} | |
Subscriber.prototype.onChange = function () {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment