1 | import isPlainObject from 'lodash-es/isPlainObject';
|
2 | import $$observable from 'symbol-observable';
|
3 |
|
4 | /**
|
5 | * These are private action types reserved by Redux.
|
6 | * For any unknown actions, you must return the current state.
|
7 | * If the current state is undefined, you must return the initial state.
|
8 | * Do not reference these action types directly in your code.
|
9 | */
|
10 | export var ActionTypes = {
|
11 | INIT: '@@redux/INIT'
|
12 | };
|
13 |
|
14 | /**
|
15 | * Creates a Redux store that holds the state tree.
|
16 | * The only way to change the data in the store is to call `dispatch()` on it.
|
17 | *
|
18 | * There should only be a single store in your app. To specify how different
|
19 | * parts of the state tree respond to actions, you may combine several reducers
|
20 | * into a single reducer function by using `combineReducers`.
|
21 | *
|
22 | * @param {Function} reducer A function that returns the next state tree, given
|
23 | * the current state tree and the action to handle.
|
24 | *
|
25 | * @param {any} [preloadedState] The initial state. You may optionally specify it
|
26 | * to hydrate the state from the server in universal apps, or to restore a
|
27 | * previously serialized user session.
|
28 | * If you use `combineReducers` to produce the root reducer function, this must be
|
29 | * an object with the same shape as `combineReducers` keys.
|
30 | *
|
31 | * @param {Function} enhancer The store enhancer. You may optionally specify it
|
32 | * to enhance the store with third-party capabilities such as middleware,
|
33 | * time travel, persistence, etc. The only store enhancer that ships with Redux
|
34 | * is `applyMiddleware()`.
|
35 | *
|
36 | * @returns {Store} A Redux store that lets you read the state, dispatch actions
|
37 | * and subscribe to changes.
|
38 | */
|
39 | export default function createStore(reducer, preloadedState, enhancer) {
|
40 | var _ref2;
|
41 |
|
42 | if (typeof preloadedState === 'function' && typeof enhancer === 'undefined') {
|
43 | enhancer = preloadedState;
|
44 | preloadedState = undefined;
|
45 | }
|
46 |
|
47 | if (typeof enhancer !== 'undefined') {
|
48 | if (typeof enhancer !== 'function') {
|
49 | throw new Error('Expected the enhancer to be a function.');
|
50 | }
|
51 |
|
52 | return enhancer(createStore)(reducer, preloadedState);
|
53 | }
|
54 |
|
55 | if (typeof reducer !== 'function') {
|
56 | throw new Error('Expected the reducer to be a function.');
|
57 | }
|
58 |
|
59 | var currentReducer = reducer;
|
60 | var currentState = preloadedState;
|
61 | var currentListeners = [];
|
62 | var nextListeners = currentListeners;
|
63 | var isDispatching = false;
|
64 |
|
65 | function ensureCanMutateNextListeners() {
|
66 | if (nextListeners === currentListeners) {
|
67 | nextListeners = currentListeners.slice();
|
68 | }
|
69 | }
|
70 |
|
71 | /**
|
72 | * Reads the state tree managed by the store.
|
73 | *
|
74 | * @returns {any} The current state tree of your application.
|
75 | */
|
76 | function getState() {
|
77 | return currentState;
|
78 | }
|
79 |
|
80 | /**
|
81 | * Adds a change listener. It will be called any time an action is dispatched,
|
82 | * and some part of the state tree may potentially have changed. You may then
|
83 | * call `getState()` to read the current state tree inside the callback.
|
84 | *
|
85 | * You may call `dispatch()` from a change listener, with the following
|
86 | * caveats:
|
87 | *
|
88 | * 1. The subscriptions are snapshotted just before every `dispatch()` call.
|
89 | * If you subscribe or unsubscribe while the listeners are being invoked, this
|
90 | * will not have any effect on the `dispatch()` that is currently in progress.
|
91 | * However, the next `dispatch()` call, whether nested or not, will use a more
|
92 | * recent snapshot of the subscription list.
|
93 | *
|
94 | * 2. The listener should not expect to see all state changes, as the state
|
95 | * might have been updated multiple times during a nested `dispatch()` before
|
96 | * the listener is called. It is, however, guaranteed that all subscribers
|
97 | * registered before the `dispatch()` started will be called with the latest
|
98 | * state by the time it exits.
|
99 | *
|
100 | * @param {Function} listener A callback to be invoked on every dispatch.
|
101 | * @returns {Function} A function to remove this change listener.
|
102 | */
|
103 | function subscribe(listener) {
|
104 | if (typeof listener !== 'function') {
|
105 | throw new Error('Expected listener to be a function.');
|
106 | }
|
107 |
|
108 | var isSubscribed = true;
|
109 |
|
110 | ensureCanMutateNextListeners();
|
111 | nextListeners.push(listener);
|
112 |
|
113 | return function unsubscribe() {
|
114 | if (!isSubscribed) {
|
115 | return;
|
116 | }
|
117 |
|
118 | isSubscribed = false;
|
119 |
|
120 | ensureCanMutateNextListeners();
|
121 | var index = nextListeners.indexOf(listener);
|
122 | nextListeners.splice(index, 1);
|
123 | };
|
124 | }
|
125 |
|
126 | /**
|
127 | * Dispatches an action. It is the only way to trigger a state change.
|
128 | *
|
129 | * The `reducer` function, used to create the store, will be called with the
|
130 | * current state tree and the given `action`. Its return value will
|
131 | * be considered the **next** state of the tree, and the change listeners
|
132 | * will be notified.
|
133 | *
|
134 | * The base implementation only supports plain object actions. If you want to
|
135 | * dispatch a Promise, an Observable, a thunk, or something else, you need to
|
136 | * wrap your store creating function into the corresponding middleware. For
|
137 | * example, see the documentation for the `redux-thunk` package. Even the
|
138 | * middleware will eventually dispatch plain object actions using this method.
|
139 | *
|
140 | * @param {Object} action A plain object representing “what changed”. It is
|
141 | * a good idea to keep actions serializable so you can record and replay user
|
142 | * sessions, or use the time travelling `redux-devtools`. An action must have
|
143 | * a `type` property which may not be `undefined`. It is a good idea to use
|
144 | * string constants for action types.
|
145 | *
|
146 | * @returns {Object} For convenience, the same action object you dispatched.
|
147 | *
|
148 | * Note that, if you use a custom middleware, it may wrap `dispatch()` to
|
149 | * return something else (for example, a Promise you can await).
|
150 | */
|
151 | function dispatch(action) {
|
152 | if (!isPlainObject(action)) {
|
153 | throw new Error('Actions must be plain objects. ' + 'Use custom middleware for async actions.');
|
154 | }
|
155 |
|
156 | if (typeof action.type === 'undefined') {
|
157 | throw new Error('Actions may not have an undefined "type" property. ' + 'Have you misspelled a constant?');
|
158 | }
|
159 |
|
160 | if (isDispatching) {
|
161 | throw new Error('Reducers may not dispatch actions.');
|
162 | }
|
163 |
|
164 | try {
|
165 | isDispatching = true;
|
166 | currentState = currentReducer(currentState, action);
|
167 | } finally {
|
168 | isDispatching = false;
|
169 | }
|
170 |
|
171 | var listeners = currentListeners = nextListeners;
|
172 | for (var i = 0; i < listeners.length; i++) {
|
173 | listeners[i]();
|
174 | }
|
175 |
|
176 | return action;
|
177 | }
|
178 |
|
179 | /**
|
180 | * Replaces the reducer currently used by the store to calculate the state.
|
181 | *
|
182 | * You might need this if your app implements code splitting and you want to
|
183 | * load some of the reducers dynamically. You might also need this if you
|
184 | * implement a hot reloading mechanism for Redux.
|
185 | *
|
186 | * @param {Function} nextReducer The reducer for the store to use instead.
|
187 | * @returns {void}
|
188 | */
|
189 | function replaceReducer(nextReducer) {
|
190 | if (typeof nextReducer !== 'function') {
|
191 | throw new Error('Expected the nextReducer to be a function.');
|
192 | }
|
193 |
|
194 | currentReducer = nextReducer;
|
195 | dispatch({ type: ActionTypes.INIT });
|
196 | }
|
197 |
|
198 | /**
|
199 | * Interoperability point for observable/reactive libraries.
|
200 | * @returns {observable} A minimal observable of state changes.
|
201 | * For more information, see the observable proposal:
|
202 | * https://github.com/zenparsing/es-observable
|
203 | */
|
204 | function observable() {
|
205 | var _ref;
|
206 |
|
207 | var outerSubscribe = subscribe;
|
208 | return _ref = {
|
209 | /**
|
210 | * The minimal observable subscription method.
|
211 | * @param {Object} observer Any object that can be used as an observer.
|
212 | * The observer object should have a `next` method.
|
213 | * @returns {subscription} An object with an `unsubscribe` method that can
|
214 | * be used to unsubscribe the observable from the store, and prevent further
|
215 | * emission of values from the observable.
|
216 | */
|
217 | subscribe: function subscribe(observer) {
|
218 | if (typeof observer !== 'object') {
|
219 | throw new TypeError('Expected the observer to be an object.');
|
220 | }
|
221 |
|
222 | function observeState() {
|
223 | if (observer.next) {
|
224 | observer.next(getState());
|
225 | }
|
226 | }
|
227 |
|
228 | observeState();
|
229 | var unsubscribe = outerSubscribe(observeState);
|
230 | return { unsubscribe: unsubscribe };
|
231 | }
|
232 | }, _ref[$$observable] = function () {
|
233 | return this;
|
234 | }, _ref;
|
235 | }
|
236 |
|
237 | // When a store is created, an "INIT" action is dispatched so that every
|
238 | // reducer returns their initial state. This effectively populates
|
239 | // the initial state tree.
|
240 | dispatch({ type: ActionTypes.INIT });
|
241 |
|
242 | return _ref2 = {
|
243 | dispatch: dispatch,
|
244 | subscribe: subscribe,
|
245 | getState: getState,
|
246 | replaceReducer: replaceReducer
|
247 | }, _ref2[$$observable] = observable, _ref2;
|
248 | } |
\ | No newline at end of file |