functiondispatch(action) { /** 判断action是不是一个普通对象 */ if (!isPlainObject(action)) { thrownewError( "Actions must be plain objects. " + "Use custom middleware for async actions." ); }
/** 判断action是否存在 type 属性 */ if (typeof action.type === "undefined") { thrownewError( 'Actions may not have an undefined "type" property. ' + "Have you misspelled a constant?" ); }
/** 判断是否在 dispatch 中 */ if (isDispatching) { thrownewError("Reducers may not dispatch actions."); }
functionsubscribe(listener) { if (typeof listener !== "function") { thrownewError("Expected the listener to be a function."); }
if (isDispatching) { thrownewError( "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#subscribe(listener) for more details." ); }
returnfunctionunsubscribe() { // 之后返回一个函数用于卸载/取消监听器 if (!isSubscribed) { return; }
if (isDispatching) { thrownewError( "You may not unsubscribe from a store listener while the reducer is executing. " + "See https://redux.js.org/api-reference/store#subscribe(listener) for more details." ); }
isSubscribed = false;
ensureCanMutateNextListeners(); const index = nextListeners.indexOf(listener); nextListeners.splice(index, 1); }; }
functiongetState() { if (isDispatching) { thrownewError( "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." ); }
exportdefaultfunctionapplyMiddleware(...middlewares) { returncreateStore =>(...args) => { const store = createStore(...args); letdispatch = () => { thrownewError( `Dispatching while constructing your middleware is not allowed. ` + `Other middleware would not be applied to this dispatch.` ); };