fluxx
Version:
Terse application state management
44 lines (35 loc) • 1.16 kB
JavaScript
;
exports.__esModule = true;
exports.default = Action;
var _Store = require('./Store');
// Unique Action ids.
// This removes the need to provide unique names across the whole application.
var id = 1;
/**
* Creates an unique action for a name.
* The name is only useful for debugging purposes; different actions can have the same name.
* The returned action function can then be used to dispatch one or more payloads.
*
* Ex:
* var clickThread = Action('clickThread'); // Create the action once
* clickThread(id); // Dispatch a payload any number of times
*/
function Action(name) {
// The actual action dispatch function
function action() {
var payloads = [].slice.call(arguments);
Object.keys(_Store.stores).forEach(function (id) {
var store = _Store.stores[id];
// A previous handler may have resulted in another store's disposal
if (!store) return;
store._handleAction(action, payloads);
});
}
action._id = id++;
action._name = name;
// Allows Actions to be used as Object keys with the correct behavior
action.toString = function () {
return action._id;
};
return action;
}