Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Path: react / flux / dist / Flux.js
Views: 51016
1
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Flux = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2
/**
3
* Copyright (c) 2014-2015, Facebook, Inc.
4
* All rights reserved.
5
*
6
* This source code is licensed under the BSD-style license found in the
7
* LICENSE file in the root directory of this source tree. An additional grant
8
* of patent rights can be found in the PATENTS file in the same directory.
9
*/
10
11
module.exports.Dispatcher = require('./lib/Dispatcher')
12
13
},{"./lib/Dispatcher":2}],2:[function(require,module,exports){
14
/**
15
* Copyright (c) 2014-2015, Facebook, Inc.
16
* All rights reserved.
17
*
18
* This source code is licensed under the BSD-style license found in the
19
* LICENSE file in the root directory of this source tree. An additional grant
20
* of patent rights can be found in the PATENTS file in the same directory.
21
*
22
* @providesModule Dispatcher
23
* @typechecks
24
* @preventMunge
25
*/
26
27
"use strict";
28
29
var invariant = require('./invariant');
30
31
var _prefix = 'ID_';
32
33
/**
34
* Dispatcher is used to broadcast payloads to registered callbacks. This is
35
* different from generic pub-sub systems in two ways:
36
*
37
* 1) Callbacks are not subscribed to particular events. Every payload is
38
* dispatched to every registered callback.
39
* 2) Callbacks can be deferred in whole or part until other callbacks have
40
* been executed.
41
*
42
* For example, consider this hypothetical flight destination form, which
43
* selects a default city when a country is selected:
44
*
45
* var flightDispatcher = new Dispatcher();
46
*
47
* // Keeps track of which country is selected
48
* var CountryStore = {country: null};
49
*
50
* // Keeps track of which city is selected
51
* var CityStore = {city: null};
52
*
53
* // Keeps track of the base flight price of the selected city
54
* var FlightPriceStore = {price: null}
55
*
56
* When a user changes the selected city, we dispatch the payload:
57
*
58
* flightDispatcher.dispatch({
59
* actionType: 'city-update',
60
* selectedCity: 'paris'
61
* });
62
*
63
* This payload is digested by `CityStore`:
64
*
65
* flightDispatcher.register(function(payload) {
66
* if (payload.actionType === 'city-update') {
67
* CityStore.city = payload.selectedCity;
68
* }
69
* });
70
*
71
* When the user selects a country, we dispatch the payload:
72
*
73
* flightDispatcher.dispatch({
74
* actionType: 'country-update',
75
* selectedCountry: 'australia'
76
* });
77
*
78
* This payload is digested by both stores:
79
*
80
* CountryStore.dispatchToken = flightDispatcher.register(function(payload) {
81
* if (payload.actionType === 'country-update') {
82
* CountryStore.country = payload.selectedCountry;
83
* }
84
* });
85
*
86
* When the callback to update `CountryStore` is registered, we save a reference
87
* to the returned token. Using this token with `waitFor()`, we can guarantee
88
* that `CountryStore` is updated before the callback that updates `CityStore`
89
* needs to query its data.
90
*
91
* CityStore.dispatchToken = flightDispatcher.register(function(payload) {
92
* if (payload.actionType === 'country-update') {
93
* // `CountryStore.country` may not be updated.
94
* flightDispatcher.waitFor([CountryStore.dispatchToken]);
95
* // `CountryStore.country` is now guaranteed to be updated.
96
*
97
* // Select the default city for the new country
98
* CityStore.city = getDefaultCityForCountry(CountryStore.country);
99
* }
100
* });
101
*
102
* The usage of `waitFor()` can be chained, for example:
103
*
104
* FlightPriceStore.dispatchToken =
105
* flightDispatcher.register(function(payload) {
106
* switch (payload.actionType) {
107
* case 'country-update':
108
* case 'city-update':
109
* flightDispatcher.waitFor([CityStore.dispatchToken]);
110
* FlightPriceStore.price =
111
* getFlightPriceStore(CountryStore.country, CityStore.city);
112
* break;
113
* }
114
* });
115
*
116
* The `country-update` payload will be guaranteed to invoke the stores'
117
* registered callbacks in order: `CountryStore`, `CityStore`, then
118
* `FlightPriceStore`.
119
*/
120
121
function Dispatcher() {
122
this._lastID = 1;
123
this._callbacks = {};
124
this._isPending = {};
125
this._isHandled = {};
126
this._isDispatching = false;
127
this._pendingPayload = null;
128
}
129
130
/**
131
* Registers a callback to be invoked with every dispatched payload. Returns
132
* a token that can be used with `waitFor()`.
133
*
134
* @param {function} callback
135
* @return {string}
136
*/
137
Dispatcher.prototype.register=function(callback) {
138
var id = _prefix + this._lastID++;
139
this._callbacks[id] = callback;
140
return id;
141
};
142
143
/**
144
* Removes a callback based on its token.
145
*
146
* @param {string} id
147
*/
148
Dispatcher.prototype.unregister=function(id) {
149
invariant(
150
this._callbacks[id],
151
'Dispatcher.unregister(...): `%s` does not map to a registered callback.',
152
id
153
);
154
delete this._callbacks[id];
155
};
156
157
/**
158
* Waits for the callbacks specified to be invoked before continuing execution
159
* of the current callback. This method should only be used by a callback in
160
* response to a dispatched payload.
161
*
162
* @param {array<string>} ids
163
*/
164
Dispatcher.prototype.waitFor=function(ids) {
165
invariant(
166
this._isDispatching,
167
'Dispatcher.waitFor(...): Must be invoked while dispatching.'
168
);
169
for (var ii = 0; ii < ids.length; ii++) {
170
var id = ids[ii];
171
if (this._isPending[id]) {
172
invariant(
173
this._isHandled[id],
174
'Dispatcher.waitFor(...): Circular dependency detected while ' +
175
'waiting for `%s`.',
176
id
177
);
178
continue;
179
}
180
invariant(
181
this._callbacks[id],
182
'Dispatcher.waitFor(...): `%s` does not map to a registered callback.',
183
id
184
);
185
this._invokeCallback(id);
186
}
187
};
188
189
/**
190
* Dispatches a payload to all registered callbacks.
191
*
192
* @param {object} payload
193
*/
194
Dispatcher.prototype.dispatch=function(payload) {
195
invariant(
196
!this._isDispatching,
197
'Dispatch.dispatch(...): Cannot dispatch in the middle of a dispatch.'
198
);
199
this._startDispatching(payload);
200
try {
201
for (var id in this._callbacks) {
202
if (this._isPending[id]) {
203
continue;
204
}
205
this._invokeCallback(id);
206
}
207
} finally {
208
this._stopDispatching();
209
}
210
};
211
212
/**
213
* Is this Dispatcher currently dispatching.
214
*
215
* @return {boolean}
216
*/
217
Dispatcher.prototype.isDispatching=function() {
218
return this._isDispatching;
219
};
220
221
/**
222
* Call the callback stored with the given id. Also do some internal
223
* bookkeeping.
224
*
225
* @param {string} id
226
* @internal
227
*/
228
Dispatcher.prototype._invokeCallback=function(id) {
229
this._isPending[id] = true;
230
this._callbacks[id](this._pendingPayload);
231
this._isHandled[id] = true;
232
};
233
234
/**
235
* Set up bookkeeping needed when dispatching.
236
*
237
* @param {object} payload
238
* @internal
239
*/
240
Dispatcher.prototype._startDispatching=function(payload) {
241
for (var id in this._callbacks) {
242
this._isPending[id] = false;
243
this._isHandled[id] = false;
244
}
245
this._pendingPayload = payload;
246
this._isDispatching = true;
247
};
248
249
/**
250
* Clear bookkeeping used for dispatching.
251
*
252
* @internal
253
*/
254
Dispatcher.prototype._stopDispatching=function() {
255
this._pendingPayload = null;
256
this._isDispatching = false;
257
};
258
259
260
module.exports = Dispatcher;
261
262
},{"./invariant":3}],3:[function(require,module,exports){
263
/**
264
* Copyright (c) 2014-2015, Facebook, Inc.
265
* All rights reserved.
266
*
267
* This source code is licensed under the BSD-style license found in the
268
* LICENSE file in the root directory of this source tree. An additional grant
269
* of patent rights can be found in the PATENTS file in the same directory.
270
*
271
* @providesModule invariant
272
*/
273
274
"use strict";
275
276
/**
277
* Use invariant() to assert state which your program assumes to be true.
278
*
279
* Provide sprintf-style format (only %s is supported) and arguments
280
* to provide information about what broke and what you were
281
* expecting.
282
*
283
* The invariant message will be stripped in production, but the invariant
284
* will remain to ensure logic does not differ in production.
285
*/
286
287
var invariant = function(condition, format, a, b, c, d, e, f) {
288
if (false) {
289
if (format === undefined) {
290
throw new Error('invariant requires an error message argument');
291
}
292
}
293
294
if (!condition) {
295
var error;
296
if (format === undefined) {
297
error = new Error(
298
'Minified exception occurred; use the non-minified dev environment ' +
299
'for the full error message and additional helpful warnings.'
300
);
301
} else {
302
var args = [a, b, c, d, e, f];
303
var argIndex = 0;
304
error = new Error(
305
'Invariant Violation: ' +
306
format.replace(/%s/g, function() { return args[argIndex++]; })
307
);
308
}
309
310
error.framesToPop = 1; // we don't care about invariant's own frame
311
throw error;
312
}
313
};
314
315
module.exports = invariant;
316
317
},{}]},{},[1])(1)
318
});
319