UNPKG

thundercats

Version:
87 lines (63 loc) 2.45 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); exports.Register = Register; exports['default'] = Cat; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } var _stampit = require('stampit'); var _stampit2 = _interopRequireDefault(_stampit); var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); var _warning = require('warning'); var _warning2 = _interopRequireDefault(_warning); var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var _utils = require('./utils'); var debug = (0, _debug2['default'])('thundercats:cat'); function Register(map, Factory, constructorArgs) { var name = (0, _utils.getName)(Factory); if (map.has(name.toLowerCase())) { return (0, _warning2['default'])(false, 'Attempted to add a class, %s, that already exists', name); } var instance = Factory.apply(null, constructorArgs); debug('registering %s %s', (0, _utils.isStore)(Factory) ? 'store' : 'action', name); map.set(name.toLowerCase(), instance); return map; } var methods = { register: function register(StoreOrActions) { var name = (0, _utils.getNameOrNull)(StoreOrActions); (0, _invariant2['default'])(typeof name === 'string', 'Attempted to add a Store/Actions that does not have a displayName'); for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } return Register((0, _utils.isStore)(StoreOrActions) ? this.stores : this.actions, StoreOrActions, args); }, getStore: function getStore(store) { return this.stores.get(('' + store).toLowerCase()); }, getActions: function getActions(action) { return this.actions.get(('' + action).toLowerCase()); }, get: function get(storeOrActions) { var possibleStore = this.getStore(storeOrActions); return possibleStore ? possibleStore : this.getActions(storeOrActions); }, dispose: function dispose() { debug('dispose all the things'); this.stores.forEach(function (store) { store.dispose(); }); this.actions.forEach(function (action) { action.dispose(); }); } }; function Cat() { return (0, _stampit2['default'])().init(function (_ref) { var instance = _ref.instance; instance.stores = new Map(); instance.actions = new Map(); }).methods(methods); }