Skip to content

Instantly share code, notes, and snippets.

@Azadehkhojandi
Created October 14, 2016 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Azadehkhojandi/757fe10de0a340a5b4af22788a89d6ca to your computer and use it in GitHub Desktop.
Save Azadehkhojandi/757fe10de0a340a5b4af22788a89d6ca to your computer and use it in GitHub Desktop.
React Flux
function Dispatcher() {
this._lastID = 0;
this._callbacks = {};
}
Dispatcher.prototype.register = function(callback) {
var id = 'CID_' + this._lastID++;
this._callbacks[id] = callback;
return id;
};
Dispatcher.prototype.dispatch = function(action) {
for (var id in this._callbacks) {
this._callbacks[id](action);
}
};
function EventEmitter() {
this._events = {};
}
EventEmitter.prototype.on = function(type, listener) {
this._events[type] = this._events[type] || [];
this._events[type].push(listener);
};
EventEmitter.prototype.emit = function(type) {
if (this._events[type]) {
this._events[type].forEach(function(listener) {
listener();
});
}
};
EventEmitter.prototype.removeListener = function(type, listener) {
if (this._events[type]) {
this._events[type].splice(this._events[type].indexOf(listener), 1);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment