UNPKG

sententiaregum-flux-container

Version:

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

138 lines (114 loc) 4.19 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 _events = require('events'); var _events2 = _interopRequireDefault(_events); 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"); } } var list = []; /** * Connection API which creates view listeners that can react on a refreshed store. * * @param {Object} store The store which should be handled by the view. * * @returns {{subscribe:Function,unsubscribe:Function,register:Function}} The internal connection API. */ exports.default = function (store) { /** * Manual modification of the emitter. * This function handles how the emitter is managed in the connection API. * * @param {Function} handler The handler to be called in case of flushing changes. * @param {String} type The type (e.g. addListener/removeListener). * * @returns {void} */ function modifyEmitter(handler, type) { (0, _invariant2.default)(isStoreKnown(store), 'The store is not registered! Please use the `store()` function to assemble a store properly and register it automatically!'); (0, _invariant2.default)(typeof handler === 'function', 'The store handler must be a function!'); getEmitter(store)[type]('change', handler); } /** * Checks whether the list is aware of the store passed as argument. * * @param {Object} store The store to check. * * @returns {boolean} */ function isStoreKnown(store) { return list.length > 0 && list.some(function (item) { return item[0] === store; }); } /** * Checks for the emitter. * * @param {Object} store The store associated to the emitter. * * @returns {EventEmitter} */ function getEmitter(store) { return list.find(function (item) { return item[0] === store ? item : false; })[1]; } return new (function () { function _class() { _classCallCheck(this, _class); } _createClass(_class, [{ key: 'subscribe', /** * Associates the handler with the given store. * * @param {Function} handler The handler to check. * * @returns {void} */ value: function subscribe(handler) { modifyEmitter(handler, 'addListener'); } /** * Unsubscribes the handler with the given store. * * @param {Function} handler The handler to check. * * @returns {void} */ }, { key: 'unsubscribe', value: function unsubscribe(handler) { modifyEmitter(handler, 'removeListener'); } /** * API for the registration of the store. * * @param {EventEmitter} emitter Emitter. * * @returns {void} */ }, { key: 'register', value: function register(emitter) { (0, _invariant2.default)(emitter instanceof _events2.default, 'Emitter must be node\'s core emitter!'); if (!isStoreKnown(store)) { list.push([store, emitter]); } } }]); return _class; }())(); };