UNPKG

2.45 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.Register = Register;
7exports['default'] = Cat;
8
9function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
10
11var _stampit = require('stampit');
12
13var _stampit2 = _interopRequireDefault(_stampit);
14
15var _invariant = require('invariant');
16
17var _invariant2 = _interopRequireDefault(_invariant);
18
19var _warning = require('warning');
20
21var _warning2 = _interopRequireDefault(_warning);
22
23var _debug = require('debug');
24
25var _debug2 = _interopRequireDefault(_debug);
26
27var _utils = require('./utils');
28
29var debug = (0, _debug2['default'])('thundercats:cat');
30
31function Register(map, Factory, constructorArgs) {
32 var name = (0, _utils.getName)(Factory);
33 if (map.has(name.toLowerCase())) {
34 return (0, _warning2['default'])(false, 'Attempted to add a class, %s, that already exists', name);
35 }
36 var instance = Factory.apply(null, constructorArgs);
37 debug('registering %s %s', (0, _utils.isStore)(Factory) ? 'store' : 'action', name);
38 map.set(name.toLowerCase(), instance);
39 return map;
40}
41
42var methods = {
43 register: function register(StoreOrActions) {
44 var name = (0, _utils.getNameOrNull)(StoreOrActions);
45
46 (0, _invariant2['default'])(typeof name === 'string', 'Attempted to add a Store/Actions that does not have a displayName');
47
48 for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
49 args[_key - 1] = arguments[_key];
50 }
51
52 return Register((0, _utils.isStore)(StoreOrActions) ? this.stores : this.actions, StoreOrActions, args);
53 },
54
55 getStore: function getStore(store) {
56 return this.stores.get(('' + store).toLowerCase());
57 },
58
59 getActions: function getActions(action) {
60 return this.actions.get(('' + action).toLowerCase());
61 },
62
63 get: function get(storeOrActions) {
64 var possibleStore = this.getStore(storeOrActions);
65 return possibleStore ? possibleStore : this.getActions(storeOrActions);
66 },
67
68 dispose: function dispose() {
69 debug('dispose all the things');
70 this.stores.forEach(function (store) {
71 store.dispose();
72 });
73
74 this.actions.forEach(function (action) {
75 action.dispose();
76 });
77 }
78};
79
80function Cat() {
81 return (0, _stampit2['default'])().init(function (_ref) {
82 var instance = _ref.instance;
83
84 instance.stores = new Map();
85 instance.actions = new Map();
86 }).methods(methods);
87}
\No newline at end of file