Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80508 views
1
'use strict';
2
3
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj['default'] : obj; };
4
5
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } };
6
7
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; };
8
9
/**
10
* Store
11
*
12
* Stores hold application state. They respond to actions sent by the dispatcher
13
* and broadcast change events to listeners, so they can grab the latest data.
14
* The key thing to remember is that the only way stores receive information
15
* from the outside world is via the dispatcher.
16
*/
17
18
var _EventEmitter2 = require('eventemitter3');
19
20
var EventEmitter = _interopRequire(_EventEmitter2);
21
22
var _assign = require('object-assign');
23
24
var assign = _interopRequire(_assign);
25
26
var Store = (function (_EventEmitter) {
27
28
/**
29
* Stores are initialized with a reference
30
* @type {Object}
31
*/
32
33
function Store() {
34
_classCallCheck(this, Store);
35
36
_EventEmitter.call(this);
37
38
this.state = null;
39
40
this._handlers = {};
41
this._asyncHandlers = {};
42
this._catchAllHandlers = [];
43
this._catchAllAsyncHandlers = {
44
begin: [],
45
success: [],
46
failure: [] };
47
}
48
49
_inherits(Store, _EventEmitter);
50
51
Store.prototype.setState = function setState(newState) {
52
// Do a transactional state update if a function is passed
53
if (typeof newState === 'function') {
54
var prevState = this._isHandlingDispatch ? this._pendingState : this.state;
55
56
newState = newState(prevState);
57
}
58
59
if (this._isHandlingDispatch) {
60
this._pendingState = this._assignState(this._pendingState, newState);
61
this._emitChangeAfterHandlingDispatch = true;
62
} else {
63
this.state = this._assignState(this.state, newState);
64
this.emit('change');
65
}
66
};
67
68
Store.prototype.replaceState = function replaceState(newState) {
69
if (this._isHandlingDispatch) {
70
this._pendingState = this._assignState(undefined, newState);
71
this._emitChangeAfterHandlingDispatch = true;
72
} else {
73
this.state = this._assignState(undefined, newState);
74
this.emit('change');
75
}
76
};
77
78
Store.prototype.getStateAsObject = function getStateAsObject() {
79
return this.state;
80
};
81
82
Store.assignState = function assignState(oldState, newState) {
83
return assign({}, oldState, newState);
84
};
85
86
Store.prototype._assignState = function _assignState() {
87
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
88
args[_key] = arguments[_key];
89
}
90
91
return (this.constructor.assignState || Store.assignState).apply(undefined, args);
92
};
93
94
Store.prototype.forceUpdate = function forceUpdate() {
95
if (this._isHandlingDispatch) {
96
this._emitChangeAfterHandlingDispatch = true;
97
} else {
98
this.emit('change');
99
}
100
};
101
102
Store.prototype.register = function register(actionId, handler) {
103
actionId = ensureActionId(actionId);
104
105
if (typeof handler !== 'function') {
106
return;
107
}this._handlers[actionId] = handler.bind(this);
108
};
109
110
Store.prototype.registerAsync = function registerAsync(actionId, beginHandler, successHandler, failureHandler) {
111
actionId = ensureActionId(actionId);
112
113
var asyncHandlers = this._bindAsyncHandlers({
114
begin: beginHandler,
115
success: successHandler,
116
failure: failureHandler });
117
118
this._asyncHandlers[actionId] = asyncHandlers;
119
};
120
121
Store.prototype.registerAll = function registerAll(handler) {
122
if (typeof handler !== 'function') {
123
return;
124
}this._catchAllHandlers.push(handler.bind(this));
125
};
126
127
Store.prototype.registerAllAsync = function registerAllAsync(beginHandler, successHandler, failureHandler) {
128
var _this = this;
129
130
var asyncHandlers = this._bindAsyncHandlers({
131
begin: beginHandler,
132
success: successHandler,
133
failure: failureHandler });
134
135
Object.keys(asyncHandlers).forEach(function (key) {
136
_this._catchAllAsyncHandlers[key].push(asyncHandlers[key]);
137
});
138
};
139
140
Store.prototype._bindAsyncHandlers = function _bindAsyncHandlers(asyncHandlers) {
141
for (var key in asyncHandlers) {
142
if (!asyncHandlers.hasOwnProperty(key)) continue;
143
144
var handler = asyncHandlers[key];
145
146
if (typeof handler === 'function') {
147
asyncHandlers[key] = handler.bind(this);
148
} else {
149
delete asyncHandlers[key];
150
}
151
}
152
153
return asyncHandlers;
154
};
155
156
Store.prototype.waitFor = function waitFor(tokensOrStores) {
157
this._waitFor(tokensOrStores);
158
};
159
160
Store.prototype.handler = function handler(payload) {
161
var body = payload.body;
162
var actionId = payload.actionId;
163
var _async = payload.async;
164
var actionArgs = payload.actionArgs;
165
var error = payload.error;
166
167
var _allHandlers = this._catchAllHandlers;
168
var _handler = this._handlers[actionId];
169
170
var _allAsyncHandlers = this._catchAllAsyncHandlers[_async];
171
var _asyncHandler = this._asyncHandlers[actionId] && this._asyncHandlers[actionId][_async];
172
173
if (_async) {
174
var beginOrFailureHandlers = _allAsyncHandlers.concat([_asyncHandler]);
175
176
switch (_async) {
177
case 'begin':
178
this._performHandler(beginOrFailureHandlers, actionArgs);
179
return;
180
case 'failure':
181
this._performHandler(beginOrFailureHandlers, [error]);
182
return;
183
case 'success':
184
this._performHandler(_allAsyncHandlers.concat([_asyncHandler || _handler].concat(_asyncHandler && [] || _allHandlers)), [body]);
185
return;
186
default:
187
return;
188
}
189
}
190
191
this._performHandler(_allHandlers.concat([_handler]), [body]);
192
};
193
194
Store.prototype._performHandler = function _performHandler(_handlers, args) {
195
this._isHandlingDispatch = true;
196
this._pendingState = this._assignState(undefined, this.state);
197
this._emitChangeAfterHandlingDispatch = false;
198
199
try {
200
this._performHandlers(_handlers, args);
201
} finally {
202
if (this._emitChangeAfterHandlingDispatch) {
203
this.state = this._pendingState;
204
this.emit('change');
205
}
206
207
this._isHandlingDispatch = false;
208
this._pendingState = undefined;
209
this._emitChangeAfterHandlingDispatch = false;
210
}
211
};
212
213
Store.prototype._performHandlers = function _performHandlers(_handlers, args) {
214
var _this2 = this;
215
216
_handlers.forEach(function (_handler) {
217
return typeof _handler === 'function' && _handler.apply(_this2, args);
218
});
219
};
220
221
return Store;
222
})(EventEmitter);
223
224
module.exports = Store;
225
226
function ensureActionId(actionOrActionId) {
227
return typeof actionOrActionId === 'function' ? actionOrActionId._id : actionOrActionId;
228
}
229