Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80536 views
1
'use strict';
2
3
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj['default'] : obj; };
4
5
var _bind = Function.prototype.bind;
6
7
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } };
8
9
var _inherits = function (subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; };
10
11
exports.__esModule = true;
12
/**
13
* Flux
14
*
15
* The main Flux class.
16
*/
17
18
var _Store = require('./Store');
19
20
var Store = _interopRequire(_Store);
21
22
var _Actions = require('./Actions');
23
24
var Actions = _interopRequire(_Actions);
25
26
var _Dispatcher = require('flux');
27
28
var _EventEmitter2 = require('eventemitter3');
29
30
var EventEmitter = _interopRequire(_EventEmitter2);
31
32
var _assign = require('object-assign');
33
34
var assign = _interopRequire(_assign);
35
36
var Flux = (function (_EventEmitter) {
37
function Flux() {
38
_classCallCheck(this, Flux);
39
40
_EventEmitter.call(this);
41
42
this.dispatcher = new _Dispatcher.Dispatcher();
43
44
this._stores = {};
45
this._actions = {};
46
}
47
48
_inherits(Flux, _EventEmitter);
49
50
Flux.prototype.createStore = function createStore(key, _Store) {
51
for (var _len = arguments.length, constructorArgs = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
52
constructorArgs[_key - 2] = arguments[_key];
53
}
54
55
if (!(_Store.prototype instanceof Store)) {
56
var className = getClassName(_Store);
57
58
throw new Error('You\'ve attempted to create a store from the class ' + className + ', which ' + 'does not have the base Store class in its prototype chain. Make sure ' + ('you\'re using the `extends` keyword: `class ' + className + ' extends ') + 'Store { ... }`');
59
}
60
61
if (this._stores.hasOwnProperty(key) && this._stores[key]) {
62
throw new Error('You\'ve attempted to create multiple stores with key ' + key + '. Keys must ' + 'be unique.');
63
}
64
65
var store = new (_bind.apply(_Store, [null].concat(constructorArgs)))();
66
var token = this.dispatcher.register(store.handler.bind(store));
67
68
store._waitFor = this.waitFor.bind(this);
69
store._token = token;
70
store._getAllActionIds = this.getAllActionIds.bind(this);
71
72
this._stores[key] = store;
73
74
return store;
75
};
76
77
Flux.prototype.getStore = function getStore(key) {
78
return this._stores.hasOwnProperty(key) ? this._stores[key] : undefined;
79
};
80
81
Flux.prototype.removeStore = function removeStore(key) {
82
if (this._stores.hasOwnProperty(key)) {
83
this._stores[key].removeAllListeners();
84
this.dispatcher.unregister(this._stores[key]._token);
85
delete this._stores[key];
86
} else {
87
throw new Error('You\'ve attempted to remove store with key ' + key + ' which does not exist.');
88
}
89
};
90
91
Flux.prototype.createActions = function createActions(key, _Actions) {
92
for (var _len2 = arguments.length, constructorArgs = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
93
constructorArgs[_key2 - 2] = arguments[_key2];
94
}
95
96
if (!(_Actions.prototype instanceof Actions) && _Actions !== Actions) {
97
if (typeof _Actions === 'function') {
98
var className = getClassName(_Actions);
99
100
throw new Error('You\'ve attempted to create actions from the class ' + className + ', which ' + 'does not have the base Actions class in its prototype chain. Make ' + ('sure you\'re using the `extends` keyword: `class ' + className + ' ') + 'extends Actions { ... }`');
101
} else {
102
var properties = _Actions;
103
_Actions = (function (_Actions2) {
104
var _class = function () {
105
_classCallCheck(this, _class);
106
107
if (_Actions2 != null) {
108
_Actions2.apply(this, arguments);
109
}
110
};
111
112
_inherits(_class, _Actions2);
113
114
return _class;
115
})(Actions);
116
assign(_Actions.prototype, properties);
117
}
118
}
119
120
if (this._actions.hasOwnProperty(key) && this._actions[key]) {
121
throw new Error('You\'ve attempted to create multiple actions with key ' + key + '. Keys ' + 'must be unique.');
122
}
123
124
var actions = new (_bind.apply(_Actions, [null].concat(constructorArgs)))();
125
actions.dispatch = this.dispatch.bind(this);
126
actions.dispatchAsync = this.dispatchAsync.bind(this);
127
128
this._actions[key] = actions;
129
130
return actions;
131
};
132
133
Flux.prototype.getActions = function getActions(key) {
134
return this._actions.hasOwnProperty(key) ? this._actions[key] : undefined;
135
};
136
137
Flux.prototype.getActionIds = function getActionIds(key) {
138
var actions = this.getActions(key);
139
140
if (!actions) {
141
return;
142
}return actions.getConstants();
143
};
144
145
Flux.prototype.removeActions = function removeActions(key) {
146
if (this._actions.hasOwnProperty(key)) {
147
delete this._actions[key];
148
} else {
149
throw new Error('You\'ve attempted to remove actions with key ' + key + ' which does not exist.');
150
}
151
};
152
153
Flux.prototype.getAllActionIds = function getAllActionIds() {
154
var actionIds = [];
155
156
for (var key in this._actions) {
157
if (!this._actions.hasOwnProperty(key)) continue;
158
159
var actionConstants = this._actions[key].getConstants();
160
161
actionIds = actionIds.concat(getValues(actionConstants));
162
}
163
164
return actionIds;
165
};
166
167
Flux.prototype.dispatch = function dispatch(actionId, body) {
168
this._dispatch({ actionId: actionId, body: body });
169
};
170
171
Flux.prototype.dispatchAsync = function dispatchAsync(actionId, promise, actionArgs) {
172
var _this = this;
173
174
var payload = {
175
actionId: actionId,
176
async: 'begin'
177
};
178
179
if (actionArgs) payload.actionArgs = actionArgs;
180
181
this._dispatch(payload);
182
183
return promise.then(function (body) {
184
_this._dispatch({
185
actionId: actionId,
186
body: body,
187
async: 'success'
188
});
189
190
return body;
191
}, function (error) {
192
_this._dispatch({
193
actionId: actionId,
194
error: error,
195
async: 'failure'
196
});
197
})['catch'](function (error) {
198
_this.emit('error', error);
199
200
throw error;
201
});
202
};
203
204
Flux.prototype._dispatch = function _dispatch(payload) {
205
this.dispatcher.dispatch(payload);
206
this.emit('dispatch', payload);
207
};
208
209
Flux.prototype.waitFor = function waitFor(tokensOrStores) {
210
211
if (!Array.isArray(tokensOrStores)) tokensOrStores = [tokensOrStores];
212
213
var ensureIsToken = function ensureIsToken(tokenOrStore) {
214
return tokenOrStore instanceof Store ? tokenOrStore._token : tokenOrStore;
215
};
216
217
var tokens = tokensOrStores.map(ensureIsToken);
218
219
this.dispatcher.waitFor(tokens);
220
};
221
222
Flux.prototype.removeAllStoreListeners = function removeAllStoreListeners(event) {
223
for (var key in this._stores) {
224
if (!this._stores.hasOwnProperty(key)) continue;
225
226
var store = this._stores[key];
227
228
store.removeAllListeners(event);
229
}
230
};
231
232
Flux.prototype.serialize = function serialize() {
233
var stateTree = {};
234
235
for (var key in this._stores) {
236
if (!this._stores.hasOwnProperty(key)) continue;
237
238
var store = this._stores[key];
239
240
var serialize = store.constructor.serialize;
241
242
if (typeof serialize !== 'function') continue;
243
244
var serializedStoreState = serialize(store.state);
245
246
if (typeof serializedStoreState !== 'string') {
247
var className = store.constructor.name;
248
249
if (process.env.NODE_ENV !== 'production') {
250
console.warn('The store with key \'' + key + '\' was not serialized because the static ' + ('method `' + className + '.serialize()` returned a non-string with type ') + ('\'' + typeof serializedStoreState + '\'.'));
251
}
252
}
253
254
stateTree[key] = serializedStoreState;
255
256
if (typeof store.constructor.deserialize !== 'function') {
257
var className = store.constructor.name;
258
259
if (process.env.NODE_ENV !== 'production') {
260
console.warn('The class `' + className + '` has a `serialize()` method, but no ' + 'corresponding `deserialize()` method.');
261
}
262
}
263
}
264
265
return JSON.stringify(stateTree);
266
};
267
268
Flux.prototype.deserialize = function deserialize(serializedState) {
269
var stateMap = undefined;
270
271
try {
272
stateMap = JSON.parse(serializedState);
273
} catch (error) {
274
var className = this.constructor.name;
275
276
if (process.env.NODE_ENV !== 'production') {
277
throw new Error('Invalid value passed to `' + className + '#deserialize()`: ' + ('' + serializedState));
278
}
279
}
280
281
for (var key in this._stores) {
282
if (!this._stores.hasOwnProperty(key)) continue;
283
284
var store = this._stores[key];
285
286
var deserialize = store.constructor.deserialize;
287
288
if (typeof deserialize !== 'function') continue;
289
290
var storeStateString = stateMap[key];
291
var storeState = deserialize(storeStateString);
292
293
store.replaceState(storeState);
294
295
if (typeof store.constructor.serialize !== 'function') {
296
var className = store.constructor.name;
297
298
if (process.env.NODE_ENV !== 'production') {
299
console.warn('The class `' + className + '` has a `deserialize()` method, but no ' + 'corresponding `serialize()` method.');
300
}
301
}
302
}
303
};
304
305
return Flux;
306
})(EventEmitter);
307
308
exports['default'] = Flux;
309
310
// Aliases
311
Flux.prototype.getConstants = Flux.prototype.getActionIds;
312
Flux.prototype.getAllConstants = Flux.prototype.getAllActionIds;
313
Flux.prototype.dehydrate = Flux.prototype.serialize;
314
Flux.prototype.hydrate = Flux.prototype.deserialize;
315
316
function getClassName(Class) {
317
return Class.prototype.constructor.name;
318
}
319
320
function getValues(object) {
321
var values = [];
322
323
for (var key in object) {
324
if (!object.hasOwnProperty(key)) continue;
325
326
values.push(object[key]);
327
}
328
329
return values;
330
}
331
332
var Flummox = Flux;
333
334
exports.Flux = Flux;
335
exports.Flummox = Flummox;
336
exports.Store = Store;
337
exports.Actions = Actions;
338