1 | import $$observable from 'symbol-observable';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var randomString = function randomString() {
|
10 | return Math.random().toString(36).substring(7).split('').join('.');
|
11 | };
|
12 |
|
13 | var ActionTypes = {
|
14 | INIT: "@@redux/INIT" + randomString(),
|
15 | REPLACE: "@@redux/REPLACE" + randomString(),
|
16 | PROBE_UNKNOWN_ACTION: function PROBE_UNKNOWN_ACTION() {
|
17 | return "@@redux/PROBE_UNKNOWN_ACTION" + randomString();
|
18 | }
|
19 | };
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function isPlainObject(obj) {
|
26 | if (typeof obj !== 'object' || obj === null) return false;
|
27 | var proto = obj;
|
28 |
|
29 | while (Object.getPrototypeOf(proto) !== null) {
|
30 | proto = Object.getPrototypeOf(proto);
|
31 | }
|
32 |
|
33 | return Object.getPrototypeOf(obj) === proto;
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | function createStore(reducer, preloadedState, enhancer) {
|
63 | var _ref2;
|
64 |
|
65 | if (typeof preloadedState === 'function' && typeof enhancer === 'function' || typeof enhancer === 'function' && typeof arguments[3] === 'function') {
|
66 | throw new Error('It looks like you are passing several store enhancers to ' + 'createStore(). This is not supported. Instead, compose them ' + 'together to a single function.');
|
67 | }
|
68 |
|
69 | if (typeof preloadedState === 'function' && typeof enhancer === 'undefined') {
|
70 | enhancer = preloadedState;
|
71 | preloadedState = undefined;
|
72 | }
|
73 |
|
74 | if (typeof enhancer !== 'undefined') {
|
75 | if (typeof enhancer !== 'function') {
|
76 | throw new Error('Expected the enhancer to be a function.');
|
77 | }
|
78 |
|
79 | return enhancer(createStore)(reducer, preloadedState);
|
80 | }
|
81 |
|
82 | if (typeof reducer !== 'function') {
|
83 | throw new Error('Expected the reducer to be a function.');
|
84 | }
|
85 |
|
86 | var currentReducer = reducer;
|
87 | var currentState = preloadedState;
|
88 | var currentListeners = [];
|
89 | var nextListeners = currentListeners;
|
90 | var isDispatching = false;
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | function ensureCanMutateNextListeners() {
|
100 | if (nextListeners === currentListeners) {
|
101 | nextListeners = currentListeners.slice();
|
102 | }
|
103 | }
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | function getState() {
|
112 | if (isDispatching) {
|
113 | throw new Error('You may not call store.getState() while the reducer is executing. ' + 'The reducer has already received the state as an argument. ' + 'Pass it down from the top reducer instead of reading it from the store.');
|
114 | }
|
115 |
|
116 | return currentState;
|
117 | }
|
118 | |
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | function subscribe(listener) {
|
144 | if (typeof listener !== 'function') {
|
145 | throw new Error('Expected the listener to be a function.');
|
146 | }
|
147 |
|
148 | if (isDispatching) {
|
149 | throw new Error('You may not call store.subscribe() while the reducer is executing. ' + 'If you would like to be notified after the store has been updated, subscribe from a ' + 'component and invoke store.getState() in the callback to access the latest state. ' + 'See https://redux.js.org/api-reference/store#subscribelistener for more details.');
|
150 | }
|
151 |
|
152 | var isSubscribed = true;
|
153 | ensureCanMutateNextListeners();
|
154 | nextListeners.push(listener);
|
155 | return function unsubscribe() {
|
156 | if (!isSubscribed) {
|
157 | return;
|
158 | }
|
159 |
|
160 | if (isDispatching) {
|
161 | throw new Error('You may not unsubscribe from a store listener while the reducer is executing. ' + 'See https://redux.js.org/api-reference/store#subscribelistener for more details.');
|
162 | }
|
163 |
|
164 | isSubscribed = false;
|
165 | ensureCanMutateNextListeners();
|
166 | var index = nextListeners.indexOf(listener);
|
167 | nextListeners.splice(index, 1);
|
168 | currentListeners = null;
|
169 | };
|
170 | }
|
171 | |
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | function dispatch(action) {
|
199 | if (!isPlainObject(action)) {
|
200 | throw new Error('Actions must be plain objects. ' + 'Use custom middleware for async actions.');
|
201 | }
|
202 |
|
203 | if (typeof action.type === 'undefined') {
|
204 | throw new Error('Actions may not have an undefined "type" property. ' + 'Have you misspelled a constant?');
|
205 | }
|
206 |
|
207 | if (isDispatching) {
|
208 | throw new Error('Reducers may not dispatch actions.');
|
209 | }
|
210 |
|
211 | try {
|
212 | isDispatching = true;
|
213 | currentState = currentReducer(currentState, action);
|
214 | } finally {
|
215 | isDispatching = false;
|
216 | }
|
217 |
|
218 | var listeners = currentListeners = nextListeners;
|
219 |
|
220 | for (var i = 0; i < listeners.length; i++) {
|
221 | var listener = listeners[i];
|
222 | listener();
|
223 | }
|
224 |
|
225 | return action;
|
226 | }
|
227 | |
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | function replaceReducer(nextReducer) {
|
240 | if (typeof nextReducer !== 'function') {
|
241 | throw new Error('Expected the nextReducer to be a function.');
|
242 | }
|
243 |
|
244 | currentReducer = nextReducer;
|
245 |
|
246 |
|
247 |
|
248 |
|
249 | dispatch({
|
250 | type: ActionTypes.REPLACE
|
251 | });
|
252 | }
|
253 | |
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | function observable() {
|
262 | var _ref;
|
263 |
|
264 | var outerSubscribe = subscribe;
|
265 | return _ref = {
|
266 | |
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | subscribe: function subscribe(observer) {
|
275 | if (typeof observer !== 'object' || observer === null) {
|
276 | throw new TypeError('Expected the observer to be an object.');
|
277 | }
|
278 |
|
279 | function observeState() {
|
280 | if (observer.next) {
|
281 | observer.next(getState());
|
282 | }
|
283 | }
|
284 |
|
285 | observeState();
|
286 | var unsubscribe = outerSubscribe(observeState);
|
287 | return {
|
288 | unsubscribe: unsubscribe
|
289 | };
|
290 | }
|
291 | }, _ref[$$observable] = function () {
|
292 | return this;
|
293 | }, _ref;
|
294 | }
|
295 |
|
296 |
|
297 |
|
298 |
|
299 | dispatch({
|
300 | type: ActionTypes.INIT
|
301 | });
|
302 | return _ref2 = {
|
303 | dispatch: dispatch,
|
304 | subscribe: subscribe,
|
305 | getState: getState,
|
306 | replaceReducer: replaceReducer
|
307 | }, _ref2[$$observable] = observable, _ref2;
|
308 | }
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | function warning(message) {
|
317 |
|
318 | if (typeof console !== 'undefined' && typeof console.error === 'function') {
|
319 | console.error(message);
|
320 | }
|
321 |
|
322 |
|
323 |
|
324 | try {
|
325 |
|
326 |
|
327 |
|
328 | throw new Error(message);
|
329 | } catch (e) {}
|
330 |
|
331 | }
|
332 |
|
333 | function getUndefinedStateErrorMessage(key, action) {
|
334 | var actionType = action && action.type;
|
335 | var actionDescription = actionType && "action \"" + String(actionType) + "\"" || 'an action';
|
336 | return "Given " + actionDescription + ", reducer \"" + key + "\" returned undefined. " + "To ignore an action, you must explicitly return the previous state. " + "If you want this reducer to hold no value, you can return null instead of undefined.";
|
337 | }
|
338 |
|
339 | function getUnexpectedStateShapeWarningMessage(inputState, reducers, action, unexpectedKeyCache) {
|
340 | var reducerKeys = Object.keys(reducers);
|
341 | var argumentName = action && action.type === ActionTypes.INIT ? 'preloadedState argument passed to createStore' : 'previous state received by the reducer';
|
342 |
|
343 | if (reducerKeys.length === 0) {
|
344 | return 'Store does not have a valid reducer. Make sure the argument passed ' + 'to combineReducers is an object whose values are reducers.';
|
345 | }
|
346 |
|
347 | if (!isPlainObject(inputState)) {
|
348 | return "The " + argumentName + " has unexpected type of \"" + {}.toString.call(inputState).match(/\s([a-z|A-Z]+)/)[1] + "\". Expected argument to be an object with the following " + ("keys: \"" + reducerKeys.join('", "') + "\"");
|
349 | }
|
350 |
|
351 | var unexpectedKeys = Object.keys(inputState).filter(function (key) {
|
352 | return !reducers.hasOwnProperty(key) && !unexpectedKeyCache[key];
|
353 | });
|
354 | unexpectedKeys.forEach(function (key) {
|
355 | unexpectedKeyCache[key] = true;
|
356 | });
|
357 | if (action && action.type === ActionTypes.REPLACE) return;
|
358 |
|
359 | if (unexpectedKeys.length > 0) {
|
360 | return "Unexpected " + (unexpectedKeys.length > 1 ? 'keys' : 'key') + " " + ("\"" + unexpectedKeys.join('", "') + "\" found in " + argumentName + ". ") + "Expected to find one of the known reducer keys instead: " + ("\"" + reducerKeys.join('", "') + "\". Unexpected keys will be ignored.");
|
361 | }
|
362 | }
|
363 |
|
364 | function assertReducerShape(reducers) {
|
365 | Object.keys(reducers).forEach(function (key) {
|
366 | var reducer = reducers[key];
|
367 | var initialState = reducer(undefined, {
|
368 | type: ActionTypes.INIT
|
369 | });
|
370 |
|
371 | if (typeof initialState === 'undefined') {
|
372 | throw new Error("Reducer \"" + key + "\" returned undefined during initialization. " + "If the state passed to the reducer is undefined, you must " + "explicitly return the initial state. The initial state may " + "not be undefined. If you don't want to set a value for this reducer, " + "you can use null instead of undefined.");
|
373 | }
|
374 |
|
375 | if (typeof reducer(undefined, {
|
376 | type: ActionTypes.PROBE_UNKNOWN_ACTION()
|
377 | }) === 'undefined') {
|
378 | throw new Error("Reducer \"" + key + "\" returned undefined when probed with a random type. " + ("Don't try to handle " + ActionTypes.INIT + " or other actions in \"redux/*\" ") + "namespace. They are considered private. Instead, you must return the " + "current state for any unknown actions, unless it is undefined, " + "in which case you must return the initial state, regardless of the " + "action type. The initial state may not be undefined, but can be null.");
|
379 | }
|
380 | });
|
381 | }
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 | function combineReducers(reducers) {
|
401 | var reducerKeys = Object.keys(reducers);
|
402 | var finalReducers = {};
|
403 |
|
404 | for (var i = 0; i < reducerKeys.length; i++) {
|
405 | var key = reducerKeys[i];
|
406 |
|
407 | if (process.env.NODE_ENV !== 'production') {
|
408 | if (typeof reducers[key] === 'undefined') {
|
409 | warning("No reducer provided for key \"" + key + "\"");
|
410 | }
|
411 | }
|
412 |
|
413 | if (typeof reducers[key] === 'function') {
|
414 | finalReducers[key] = reducers[key];
|
415 | }
|
416 | }
|
417 |
|
418 | var finalReducerKeys = Object.keys(finalReducers);
|
419 |
|
420 |
|
421 | var unexpectedKeyCache;
|
422 |
|
423 | if (process.env.NODE_ENV !== 'production') {
|
424 | unexpectedKeyCache = {};
|
425 | }
|
426 |
|
427 | var shapeAssertionError;
|
428 |
|
429 | try {
|
430 | assertReducerShape(finalReducers);
|
431 | } catch (e) {
|
432 | shapeAssertionError = e;
|
433 | }
|
434 |
|
435 | return function combination(state, action) {
|
436 | if (state === void 0) {
|
437 | state = {};
|
438 | }
|
439 |
|
440 | if (shapeAssertionError) {
|
441 | throw shapeAssertionError;
|
442 | }
|
443 |
|
444 | if (process.env.NODE_ENV !== 'production') {
|
445 | var warningMessage = getUnexpectedStateShapeWarningMessage(state, finalReducers, action, unexpectedKeyCache);
|
446 |
|
447 | if (warningMessage) {
|
448 | warning(warningMessage);
|
449 | }
|
450 | }
|
451 |
|
452 | var hasChanged = false;
|
453 | var nextState = {};
|
454 |
|
455 | for (var _i = 0; _i < finalReducerKeys.length; _i++) {
|
456 | var _key = finalReducerKeys[_i];
|
457 | var reducer = finalReducers[_key];
|
458 | var previousStateForKey = state[_key];
|
459 | var nextStateForKey = reducer(previousStateForKey, action);
|
460 |
|
461 | if (typeof nextStateForKey === 'undefined') {
|
462 | var errorMessage = getUndefinedStateErrorMessage(_key, action);
|
463 | throw new Error(errorMessage);
|
464 | }
|
465 |
|
466 | nextState[_key] = nextStateForKey;
|
467 | hasChanged = hasChanged || nextStateForKey !== previousStateForKey;
|
468 | }
|
469 |
|
470 | hasChanged = hasChanged || finalReducerKeys.length !== Object.keys(state).length;
|
471 | return hasChanged ? nextState : state;
|
472 | };
|
473 | }
|
474 |
|
475 | function bindActionCreator(actionCreator, dispatch) {
|
476 | return function () {
|
477 | return dispatch(actionCreator.apply(this, arguments));
|
478 | };
|
479 | }
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 | function bindActionCreators(actionCreators, dispatch) {
|
504 | if (typeof actionCreators === 'function') {
|
505 | return bindActionCreator(actionCreators, dispatch);
|
506 | }
|
507 |
|
508 | if (typeof actionCreators !== 'object' || actionCreators === null) {
|
509 | throw new Error("bindActionCreators expected an object or a function, instead received " + (actionCreators === null ? 'null' : typeof actionCreators) + ". " + "Did you write \"import ActionCreators from\" instead of \"import * as ActionCreators from\"?");
|
510 | }
|
511 |
|
512 | var boundActionCreators = {};
|
513 |
|
514 | for (var key in actionCreators) {
|
515 | var actionCreator = actionCreators[key];
|
516 |
|
517 | if (typeof actionCreator === 'function') {
|
518 | boundActionCreators[key] = bindActionCreator(actionCreator, dispatch);
|
519 | }
|
520 | }
|
521 |
|
522 | return boundActionCreators;
|
523 | }
|
524 |
|
525 | function _defineProperty(obj, key, value) {
|
526 | if (key in obj) {
|
527 | Object.defineProperty(obj, key, {
|
528 | value: value,
|
529 | enumerable: true,
|
530 | configurable: true,
|
531 | writable: true
|
532 | });
|
533 | } else {
|
534 | obj[key] = value;
|
535 | }
|
536 |
|
537 | return obj;
|
538 | }
|
539 |
|
540 | function ownKeys(object, enumerableOnly) {
|
541 | var keys = Object.keys(object);
|
542 |
|
543 | if (Object.getOwnPropertySymbols) {
|
544 | keys.push.apply(keys, Object.getOwnPropertySymbols(object));
|
545 | }
|
546 |
|
547 | if (enumerableOnly) keys = keys.filter(function (sym) {
|
548 | return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
549 | });
|
550 | return keys;
|
551 | }
|
552 |
|
553 | function _objectSpread2(target) {
|
554 | for (var i = 1; i < arguments.length; i++) {
|
555 | var source = arguments[i] != null ? arguments[i] : {};
|
556 |
|
557 | if (i % 2) {
|
558 | ownKeys(source, true).forEach(function (key) {
|
559 | _defineProperty(target, key, source[key]);
|
560 | });
|
561 | } else if (Object.getOwnPropertyDescriptors) {
|
562 | Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
563 | } else {
|
564 | ownKeys(source).forEach(function (key) {
|
565 | Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
566 | });
|
567 | }
|
568 | }
|
569 |
|
570 | return target;
|
571 | }
|
572 |
|
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 |
|
580 |
|
581 |
|
582 |
|
583 | function compose() {
|
584 | for (var _len = arguments.length, funcs = new Array(_len), _key = 0; _key < _len; _key++) {
|
585 | funcs[_key] = arguments[_key];
|
586 | }
|
587 |
|
588 | if (funcs.length === 0) {
|
589 | return function (arg) {
|
590 | return arg;
|
591 | };
|
592 | }
|
593 |
|
594 | if (funcs.length === 1) {
|
595 | return funcs[0];
|
596 | }
|
597 |
|
598 | return funcs.reduce(function (a, b) {
|
599 | return function () {
|
600 | return a(b.apply(void 0, arguments));
|
601 | };
|
602 | });
|
603 | }
|
604 |
|
605 |
|
606 |
|
607 |
|
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 |
|
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 | function applyMiddleware() {
|
623 | for (var _len = arguments.length, middlewares = new Array(_len), _key = 0; _key < _len; _key++) {
|
624 | middlewares[_key] = arguments[_key];
|
625 | }
|
626 |
|
627 | return function (createStore) {
|
628 | return function () {
|
629 | var store = createStore.apply(void 0, arguments);
|
630 |
|
631 | var _dispatch = function dispatch() {
|
632 | throw new Error('Dispatching while constructing your middleware is not allowed. ' + 'Other middleware would not be applied to this dispatch.');
|
633 | };
|
634 |
|
635 | var middlewareAPI = {
|
636 | getState: store.getState,
|
637 | dispatch: function dispatch() {
|
638 | return _dispatch.apply(void 0, arguments);
|
639 | }
|
640 | };
|
641 | var chain = middlewares.map(function (middleware) {
|
642 | return middleware(middlewareAPI);
|
643 | });
|
644 | _dispatch = compose.apply(void 0, chain)(store.dispatch);
|
645 | return _objectSpread2({}, store, {
|
646 | dispatch: _dispatch
|
647 | });
|
648 | };
|
649 | };
|
650 | }
|
651 |
|
652 |
|
653 |
|
654 |
|
655 |
|
656 |
|
657 | function isCrushed() {}
|
658 |
|
659 | if (process.env.NODE_ENV !== 'production' && typeof isCrushed.name === 'string' && isCrushed.name !== 'isCrushed') {
|
660 | warning('You are currently using minified code outside of NODE_ENV === "production". ' + 'This means that you are running a slower development build of Redux. ' + 'You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify ' + 'or setting mode to production in webpack (https://webpack.js.org/concepts/mode/) ' + 'to ensure you have the correct code for your production build.');
|
661 | }
|
662 |
|
663 | export { ActionTypes as __DO_NOT_USE__ActionTypes, applyMiddleware, bindActionCreators, combineReducers, compose, createStore };
|