diff options
Diffstat (limited to 'web/src/js/dispatcher.js')
-rw-r--r-- | web/src/js/dispatcher.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/web/src/js/dispatcher.js b/web/src/js/dispatcher.js new file mode 100644 index 00000000..4fe23447 --- /dev/null +++ b/web/src/js/dispatcher.js @@ -0,0 +1,35 @@ +const PayloadSources = { + VIEW: "view", + SERVER: "server" +}; + + +function Dispatcher() { + this.callbacks = []; +} +Dispatcher.prototype.register = function (callback) { + this.callbacks.push(callback); +}; +Dispatcher.prototype.unregister = function (callback) { + var index = this.callbacks.indexOf(f); + if (index >= 0) { + this.callbacks.splice(this.callbacks.indexOf(f), 1); + } +}; +Dispatcher.prototype.dispatch = function (payload) { + console.debug("dispatch", payload); + for(var i = 0; i < this.callbacks.length; i++){ + this.callbacks[i](payload); + } +}; + + +AppDispatcher = new Dispatcher(); +AppDispatcher.dispatchViewAction = function (action) { + action.source = PayloadSources.VIEW; + this.dispatch(action); +}; +AppDispatcher.dispatchServerAction = function (action) { + action.source = PayloadSources.SERVER; + this.dispatch(action); +}; |