From a0f727e22115b801da1160d544dcde567e555f23 Mon Sep 17 00:00:00 2001 From: Randall Randall Date: Tue, 30 Sep 2014 19:44:47 -0400 Subject: [PATCH] Add new methods on Flux: addAction, addActions, addStore, addStores Add new method on Dispatcher: addStore to support Flux#addStore Add tests for new api functionality --- lib/dispatcher.js | 9 +++- lib/flux.js | 92 ++++++++++++++++++++++++++++++++------- test/unit/test_flux.js | 99 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 179 insertions(+), 21 deletions(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index 3aefb90..d51b27c 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -10,17 +10,22 @@ var _clone = require("lodash-node/modern/objects/clone"), _uniq = require("lodash-node/modern/arrays/uniq"); var Dispatcher = function(stores) { - this.stores = stores; + this.stores = {}; this.currentDispatch = null; this.waitingToDispatch = []; for (var key in stores) { if (stores.hasOwnProperty(key)) { - stores[key].dispatcher = this; + this.addStore(key, stores[key]); } } }; +Dispatcher.prototype.addStore = function(name, store) { + store.dispatcher = this; + this.stores[name] = store; +}; + Dispatcher.prototype.dispatch = function(action) { if (this.currentDispatch) { throw new Error("Cannot dispatch an action while another action is being dispatched"); diff --git a/lib/flux.js b/lib/flux.js index c4918da..c73369a 100644 --- a/lib/flux.js +++ b/lib/flux.js @@ -4,34 +4,79 @@ function bindActions(target, actions, dispatchBinder) { for (var key in actions) { if (actions.hasOwnProperty(key)) { if (typeof actions[key] === "function") { - target[key] = actions[key].bind(dispatchBinder); + if (target[key] === undefined) { + target[key] = actions[key].bind(dispatchBinder); + } else { + // TODO: pass in the sequence of keys by which we arrived here to provide in the error + throw new Error("An action by the name of '" + key + "' already exists here"); + } } else if (typeof actions[key] === "object") { - target[key] = {}; + if (typeof target[key] === "function") { + throw new Error("A namespace by the name of '" + key + "' already exists here"); + } else if (target[key] === undefined) { + target[key] = {}; + } bindActions(target[key], actions[key], dispatchBinder); - } - } + } + } } } var Flux = function(stores, actions) { - var dispatcher = new Dispatcher(stores), - dispatchBinder = { + this.dispatcher = new Dispatcher(stores); + this.actions = {}; + this.stores = {}; + + this.addActions(actions); + this.addStores(stores); + +}; + +Flux.prototype.addActions = function(actions) { + var dispatcher = this.dispatcher, + dispatchBinder = { flux: this, dispatch: function(type, payload) { dispatcher.dispatch({type: type, payload: payload}); } - }; - - this.dispatcher = dispatcher; - this.actions = {}; - this.stores = stores; - + }; + bindActions(this.actions, actions, dispatchBinder); +}; - for (var key in stores) { - if (stores.hasOwnProperty(key)) { - stores[key].flux = this; +// addAction has two signatures: +// 1: string[, string, string, string...], actionFunction +// 2: arrayOfStrings, actionFunction +Flux.prototype.addAction = function() { + if (arguments.length < 2) { + throw new Error("addAction requires at least two arguments, a string (or array of strings), and a function"); + } + if (typeof arguments[0] === "string") { + // convert signature 2 into 1 + var args = []; + for (var i = 0; i