UNPKG

sententiaregum-flux-container

Version:

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

84 lines (68 loc) 3.16 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 }); exports.default = createStoreRefreshStateHandler; var _events = require('events'); var _events2 = _interopRequireDefault(_events); var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * Handler which creates a callback that handles the state refresh of a store from a dispatcher * and flushes it into the view. * * @param {Function} saveHandler The handler provided by the store to save all the data. * @param {EventEmitter} emitter The event emitter. * @param {Object.<String>} eventConfig The configuration of the event to handle. * @param {*} oldState The old state of the store. * * @returns {Function} The refreshing handler. * @private This is part of the internal API and should not be used directly! */ function createStoreRefreshStateHandler(saveHandler, emitter, eventConfig, oldState) { (0, _invariant2.default)(emitter instanceof _events2.default, 'The emitter must be an instance of node\'s core event emitter!'); var modifySubsection = function modifySubsection(subsection, state, oldState) { return Object.assign({}, oldState, _defineProperty({}, subsection, Array.isArray(oldState[subsection]) && Array.isArray(state) ? oldState[subsection].concat(state) : state)); }; /** * Lightweight handler which evaluates the new state for a store based on the configuration for a store * and the dispatched payload. * * This function covers the following cases: * - an array with strings (modify a subsection of the current state snapshot) and handlers (modify the state with a function). * - no function (the payload will be interpreted as state). * * @param {*} state The newly dispatched payload. * * @returns {*} The evaluated result. */ function handleState(state) { if (!eventConfig.function) { return state; } if (typeof eventConfig.function === 'function') { return eventConfig.function(state, oldState); } (0, _invariant2.default)(Array.isArray(eventConfig.function) && eventConfig.function.length > 0, 'The `function` value must be a non-empty array!'); return eventConfig.function.reduce(function (prevState, handler) { if (typeof handler === 'function') { return handler(state, oldState); } return modifySubsection(handler, state, oldState); }, state); } return function (state) { return saveHandler(handleState(state), emitter); }; }