Created
October 14, 2016 03:47
-
-
Save Azadehkhojandi/757fe10de0a340a5b4af22788a89d6ca to your computer and use it in GitHub Desktop.
React Flux
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
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); | |
} | |
}; |
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
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