UNPKG

16.8 kBJavaScriptView Raw
1// src/utils/formatProdErrorMessage.ts
2function formatProdErrorMessage(code) {
3 return `Minified Redux error #${code}; visit https://redux.js.org/Errors?code=${code} for the full message or use the non-minified dev environment for full errors. `;
4}
5
6// src/utils/symbol-observable.ts
7var $$observable = /* @__PURE__ */ (() => typeof Symbol === "function" && Symbol.observable || "@@observable")();
8var symbol_observable_default = $$observable;
9
10// src/utils/actionTypes.ts
11var randomString = () => Math.random().toString(36).substring(7).split("").join(".");
12var ActionTypes = {
13 INIT: `@@redux/INIT${/* @__PURE__ */ randomString()}`,
14 REPLACE: `@@redux/REPLACE${/* @__PURE__ */ randomString()}`,
15 PROBE_UNKNOWN_ACTION: () => `@@redux/PROBE_UNKNOWN_ACTION${randomString()}`
16};
17var actionTypes_default = ActionTypes;
18
19// src/utils/isPlainObject.ts
20function isPlainObject(obj) {
21 if (typeof obj !== "object" || obj === null)
22 return false;
23 let proto = obj;
24 while (Object.getPrototypeOf(proto) !== null) {
25 proto = Object.getPrototypeOf(proto);
26 }
27 return Object.getPrototypeOf(obj) === proto || Object.getPrototypeOf(obj) === null;
28}
29
30// src/utils/kindOf.ts
31function miniKindOf(val) {
32 if (val === void 0)
33 return "undefined";
34 if (val === null)
35 return "null";
36 const type = typeof val;
37 switch (type) {
38 case "boolean":
39 case "string":
40 case "number":
41 case "symbol":
42 case "function": {
43 return type;
44 }
45 }
46 if (Array.isArray(val))
47 return "array";
48 if (isDate(val))
49 return "date";
50 if (isError(val))
51 return "error";
52 const constructorName = ctorName(val);
53 switch (constructorName) {
54 case "Symbol":
55 case "Promise":
56 case "WeakMap":
57 case "WeakSet":
58 case "Map":
59 case "Set":
60 return constructorName;
61 }
62 return Object.prototype.toString.call(val).slice(8, -1).toLowerCase().replace(/\s/g, "");
63}
64function ctorName(val) {
65 return typeof val.constructor === "function" ? val.constructor.name : null;
66}
67function isError(val) {
68 return val instanceof Error || typeof val.message === "string" && val.constructor && typeof val.constructor.stackTraceLimit === "number";
69}
70function isDate(val) {
71 if (val instanceof Date)
72 return true;
73 return typeof val.toDateString === "function" && typeof val.getDate === "function" && typeof val.setDate === "function";
74}
75function kindOf(val) {
76 let typeOfVal = typeof val;
77 if (process.env.NODE_ENV !== "production") {
78 typeOfVal = miniKindOf(val);
79 }
80 return typeOfVal;
81}
82
83// src/createStore.ts
84function createStore(reducer, preloadedState, enhancer) {
85 if (typeof reducer !== "function") {
86 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(2) : `Expected the root reducer to be a function. Instead, received: '${kindOf(reducer)}'`);
87 }
88 if (typeof preloadedState === "function" && typeof enhancer === "function" || typeof enhancer === "function" && typeof arguments[3] === "function") {
89 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(0) : "It looks like you are passing several store enhancers to createStore(). This is not supported. Instead, compose them together to a single function. See https://redux.js.org/tutorials/fundamentals/part-4-store#creating-a-store-with-enhancers for an example.");
90 }
91 if (typeof preloadedState === "function" && typeof enhancer === "undefined") {
92 enhancer = preloadedState;
93 preloadedState = void 0;
94 }
95 if (typeof enhancer !== "undefined") {
96 if (typeof enhancer !== "function") {
97 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(1) : `Expected the enhancer to be a function. Instead, received: '${kindOf(enhancer)}'`);
98 }
99 return enhancer(createStore)(reducer, preloadedState);
100 }
101 let currentReducer = reducer;
102 let currentState = preloadedState;
103 let currentListeners = /* @__PURE__ */ new Map();
104 let nextListeners = currentListeners;
105 let listenerIdCounter = 0;
106 let isDispatching = false;
107 function ensureCanMutateNextListeners() {
108 if (nextListeners === currentListeners) {
109 nextListeners = /* @__PURE__ */ new Map();
110 currentListeners.forEach((listener, key) => {
111 nextListeners.set(key, listener);
112 });
113 }
114 }
115 function getState() {
116 if (isDispatching) {
117 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(3) : "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.");
118 }
119 return currentState;
120 }
121 function subscribe(listener) {
122 if (typeof listener !== "function") {
123 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(4) : `Expected the listener to be a function. Instead, received: '${kindOf(listener)}'`);
124 }
125 if (isDispatching) {
126 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(5) : "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/store#subscribelistener for more details.");
127 }
128 let isSubscribed = true;
129 ensureCanMutateNextListeners();
130 const listenerId = listenerIdCounter++;
131 nextListeners.set(listenerId, listener);
132 return function unsubscribe() {
133 if (!isSubscribed) {
134 return;
135 }
136 if (isDispatching) {
137 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(6) : "You may not unsubscribe from a store listener while the reducer is executing. See https://redux.js.org/api/store#subscribelistener for more details.");
138 }
139 isSubscribed = false;
140 ensureCanMutateNextListeners();
141 nextListeners.delete(listenerId);
142 currentListeners = null;
143 };
144 }
145 function dispatch(action) {
146 if (!isPlainObject(action)) {
147 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(7) : `Actions must be plain objects. Instead, the actual type was: '${kindOf(action)}'. You may need to add middleware to your store setup to handle dispatching other values, such as 'redux-thunk' to handle dispatching functions. See https://redux.js.org/tutorials/fundamentals/part-4-store#middleware and https://redux.js.org/tutorials/fundamentals/part-6-async-logic#using-the-redux-thunk-middleware for examples.`);
148 }
149 if (typeof action.type === "undefined") {
150 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(8) : 'Actions may not have an undefined "type" property. You may have misspelled an action type string constant.');
151 }
152 if (typeof action.type !== "string") {
153 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(17) : `Action "type" property must be a string. Instead, the actual type was: '${kindOf(action.type)}'. Value was: '${action.type}' (stringified)`);
154 }
155 if (isDispatching) {
156 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(9) : "Reducers may not dispatch actions.");
157 }
158 try {
159 isDispatching = true;
160 currentState = currentReducer(currentState, action);
161 } finally {
162 isDispatching = false;
163 }
164 const listeners = currentListeners = nextListeners;
165 listeners.forEach((listener) => {
166 listener();
167 });
168 return action;
169 }
170 function replaceReducer(nextReducer) {
171 if (typeof nextReducer !== "function") {
172 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(10) : `Expected the nextReducer to be a function. Instead, received: '${kindOf(nextReducer)}`);
173 }
174 currentReducer = nextReducer;
175 dispatch({
176 type: actionTypes_default.REPLACE
177 });
178 }
179 function observable() {
180 const outerSubscribe = subscribe;
181 return {
182 /**
183 * The minimal observable subscription method.
184 * @param observer Any object that can be used as an observer.
185 * The observer object should have a `next` method.
186 * @returns An object with an `unsubscribe` method that can
187 * be used to unsubscribe the observable from the store, and prevent further
188 * emission of values from the observable.
189 */
190 subscribe(observer) {
191 if (typeof observer !== "object" || observer === null) {
192 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(11) : `Expected the observer to be an object. Instead, received: '${kindOf(observer)}'`);
193 }
194 function observeState() {
195 const observerAsObserver = observer;
196 if (observerAsObserver.next) {
197 observerAsObserver.next(getState());
198 }
199 }
200 observeState();
201 const unsubscribe = outerSubscribe(observeState);
202 return {
203 unsubscribe
204 };
205 },
206 [symbol_observable_default]() {
207 return this;
208 }
209 };
210 }
211 dispatch({
212 type: actionTypes_default.INIT
213 });
214 const store = {
215 dispatch,
216 subscribe,
217 getState,
218 replaceReducer,
219 [symbol_observable_default]: observable
220 };
221 return store;
222}
223function legacy_createStore(reducer, preloadedState, enhancer) {
224 return createStore(reducer, preloadedState, enhancer);
225}
226
227// src/utils/warning.ts
228function warning(message) {
229 if (typeof console !== "undefined" && typeof console.error === "function") {
230 console.error(message);
231 }
232 try {
233 throw new Error(message);
234 } catch (e) {
235 }
236}
237
238// src/combineReducers.ts
239function getUnexpectedStateShapeWarningMessage(inputState, reducers, action, unexpectedKeyCache) {
240 const reducerKeys = Object.keys(reducers);
241 const argumentName = action && action.type === actionTypes_default.INIT ? "preloadedState argument passed to createStore" : "previous state received by the reducer";
242 if (reducerKeys.length === 0) {
243 return "Store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose values are reducers.";
244 }
245 if (!isPlainObject(inputState)) {
246 return `The ${argumentName} has unexpected type of "${kindOf(inputState)}". Expected argument to be an object with the following keys: "${reducerKeys.join('", "')}"`;
247 }
248 const unexpectedKeys = Object.keys(inputState).filter((key) => !reducers.hasOwnProperty(key) && !unexpectedKeyCache[key]);
249 unexpectedKeys.forEach((key) => {
250 unexpectedKeyCache[key] = true;
251 });
252 if (action && action.type === actionTypes_default.REPLACE)
253 return;
254 if (unexpectedKeys.length > 0) {
255 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.`;
256 }
257}
258function assertReducerShape(reducers) {
259 Object.keys(reducers).forEach((key) => {
260 const reducer = reducers[key];
261 const initialState = reducer(void 0, {
262 type: actionTypes_default.INIT
263 });
264 if (typeof initialState === "undefined") {
265 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(12) : `The slice reducer for key "${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.`);
266 }
267 if (typeof reducer(void 0, {
268 type: actionTypes_default.PROBE_UNKNOWN_ACTION()
269 }) === "undefined") {
270 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(13) : `The slice reducer for key "${key}" returned undefined when probed with a random type. Don't try to handle '${actionTypes_default.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.`);
271 }
272 });
273}
274function combineReducers(reducers) {
275 const reducerKeys = Object.keys(reducers);
276 const finalReducers = {};
277 for (let i = 0; i < reducerKeys.length; i++) {
278 const key = reducerKeys[i];
279 if (process.env.NODE_ENV !== "production") {
280 if (typeof reducers[key] === "undefined") {
281 warning(`No reducer provided for key "${key}"`);
282 }
283 }
284 if (typeof reducers[key] === "function") {
285 finalReducers[key] = reducers[key];
286 }
287 }
288 const finalReducerKeys = Object.keys(finalReducers);
289 let unexpectedKeyCache;
290 if (process.env.NODE_ENV !== "production") {
291 unexpectedKeyCache = {};
292 }
293 let shapeAssertionError;
294 try {
295 assertReducerShape(finalReducers);
296 } catch (e) {
297 shapeAssertionError = e;
298 }
299 return function combination(state = {}, action) {
300 if (shapeAssertionError) {
301 throw shapeAssertionError;
302 }
303 if (process.env.NODE_ENV !== "production") {
304 const warningMessage = getUnexpectedStateShapeWarningMessage(state, finalReducers, action, unexpectedKeyCache);
305 if (warningMessage) {
306 warning(warningMessage);
307 }
308 }
309 let hasChanged = false;
310 const nextState = {};
311 for (let i = 0; i < finalReducerKeys.length; i++) {
312 const key = finalReducerKeys[i];
313 const reducer = finalReducers[key];
314 const previousStateForKey = state[key];
315 const nextStateForKey = reducer(previousStateForKey, action);
316 if (typeof nextStateForKey === "undefined") {
317 const actionType = action && action.type;
318 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(14) : `When called with an action of type ${actionType ? `"${String(actionType)}"` : "(unknown type)"}, the slice reducer for key "${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.`);
319 }
320 nextState[key] = nextStateForKey;
321 hasChanged = hasChanged || nextStateForKey !== previousStateForKey;
322 }
323 hasChanged = hasChanged || finalReducerKeys.length !== Object.keys(state).length;
324 return hasChanged ? nextState : state;
325 };
326}
327
328// src/bindActionCreators.ts
329function bindActionCreator(actionCreator, dispatch) {
330 return function(...args) {
331 return dispatch(actionCreator.apply(this, args));
332 };
333}
334function bindActionCreators(actionCreators, dispatch) {
335 if (typeof actionCreators === "function") {
336 return bindActionCreator(actionCreators, dispatch);
337 }
338 if (typeof actionCreators !== "object" || actionCreators === null) {
339 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(16) : `bindActionCreators expected an object or a function, but instead received: '${kindOf(actionCreators)}'. Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`);
340 }
341 const boundActionCreators = {};
342 for (const key in actionCreators) {
343 const actionCreator = actionCreators[key];
344 if (typeof actionCreator === "function") {
345 boundActionCreators[key] = bindActionCreator(actionCreator, dispatch);
346 }
347 }
348 return boundActionCreators;
349}
350
351// src/compose.ts
352function compose(...funcs) {
353 if (funcs.length === 0) {
354 return (arg) => arg;
355 }
356 if (funcs.length === 1) {
357 return funcs[0];
358 }
359 return funcs.reduce((a, b) => (...args) => a(b(...args)));
360}
361
362// src/applyMiddleware.ts
363function applyMiddleware(...middlewares) {
364 return (createStore2) => (reducer, preloadedState) => {
365 const store = createStore2(reducer, preloadedState);
366 let dispatch = () => {
367 throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(15) : "Dispatching while constructing your middleware is not allowed. Other middleware would not be applied to this dispatch.");
368 };
369 const middlewareAPI = {
370 getState: store.getState,
371 dispatch: (action, ...args) => dispatch(action, ...args)
372 };
373 const chain = middlewares.map((middleware) => middleware(middlewareAPI));
374 dispatch = compose(...chain)(store.dispatch);
375 return {
376 ...store,
377 dispatch
378 };
379 };
380}
381
382// src/utils/isAction.ts
383function isAction(action) {
384 return isPlainObject(action) && "type" in action && typeof action.type === "string";
385}
386export {
387 actionTypes_default as __DO_NOT_USE__ActionTypes,
388 applyMiddleware,
389 bindActionCreators,
390 combineReducers,
391 compose,
392 createStore,
393 isAction,
394 isPlainObject,
395 legacy_createStore
396};
397//# sourceMappingURL=redux.mjs.map
\No newline at end of file