UNPKG

sententiaregum-flux-container

Version:

The flux implementation of the sententiaregum project based on facebook/flux

170 lines (140 loc) 5.04 kB
/* * This file is part of the Sententiaregum project. * * (c) Maximilian Bosch <maximilian.bosch.27@gmail.com> * (c) Ben Bieler <benjaminbieler2014@gmail.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); var _computeEventListenerOrder = require('./../util/computeEventListenerOrder'); var _computeEventListenerOrder2 = _interopRequireDefault(_computeEventListenerOrder); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Extended dispatcher which supports a better handling with data flows. * * This dispatcher attaches callbacks directly to events and calls them if the given * event is provided in Dispatcher#dispatch(). The dependencies * are a refactored approach of `waitFor()` which computes the proper order before executing * the callbacks, so a clear structure is provided and nothing will be changed during the actual dispatch. * As the dispatcher is stateless and stores callbacks only, parallel and recursive dispatches can * be done. * * @author Maximilian Bosch <maximilian.bosch.27@gmail.com> */ exports.default = new (function () { /** * Constructor. * * @returns {void} */ function _class() { _classCallCheck(this, _class); this.reset(); } /** * Adds a new listener for a certain event. * * @param {String} eventName Name of the event to listen on. * @param {Function} callback The actual callback hook. * @param {Array.<String>} dependencies Dependencies of the current listener. * * @returns {String} the ID of the callback. */ _createClass(_class, [{ key: 'addListener', value: function addListener(eventName, callback) { var dependencies = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; var id = this._generateDispatchID(), deps = typeof dependencies === 'undefined' ? [] : dependencies; this.store[id] = { eventName: eventName, callback: callback, dependencies: deps }; return id; } /** * Removes the event listener by its ID. * * @param {String} id ID of the hook. * * @returns {void} */ }, { key: 'removeListener', value: function removeListener(id) { (0, _invariant2.default)(typeof this.store[id] !== 'undefined', 'The ID "' + id + '" must be present in the event store!'); delete this.store[id]; } /** * Dispatches the payload to every callback which is subscribed to the given event name. * * @param {String} eventName The event name. * @param {Object} payload The payload that every callback receives. * * @returns {void} */ }, { key: 'dispatch', value: function dispatch(eventName, payload) { var _this = this; this._executeCallbackChain((0, _computeEventListenerOrder2.default)(Object.keys(this.store).reduce(function (list, id) { var config = _this.store[id]; if (eventName === config.eventName) { list[id] = config; } return list; }, {})), payload); } /** * Resets the dispatcher. * * @returns {void} */ }, { key: 'reset', value: function reset() { this.counter = 1; this.store = {}; } /** * Executes a chain of callbacks. * * @param {Array} chain The chain of callbacks to execute. * @param {Object} payload The event payload. * * @returns {void} * @private */ }, { key: '_executeCallbackChain', value: function _executeCallbackChain(chain, payload) { chain.forEach(function (listener) { return listener(payload); }); } /** * Generates the ID of the next event hook. * * @returns {String} The hook ID. * @private */ }, { key: '_generateDispatchID', value: function _generateDispatchID() { var id = 'ID_' + this.counter; this.counter++; return id; } }]); return _class; }())();