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