// subscribe订阅方法,它将会定义dispatch最后执行的listeners数组的内容 functionsubscribe(listener){ // 校验listener的类型 if(typeof listener !== 'function') { thrownewError('Listener must be a function'); } // 禁止在reducer中调用subscribe if(isDispatching) { thrownewError('You may not call store.subscribe() while the reducer is executing. The reducer has already received the state as an argument. Pass it down from the top reducer instead of subscribing.'); }
// 定义dispatch方法,用于派发action functiondispatch(action) { // 校验action的类型 if(!isPlainObject(action)) { thrownewError('Actions must be plain objects. Use custom middleware for async actions.'); }
// 约束action中必须有type属性作为action的唯一标识 if(typeof action.type === 'undefined') { thrownewError('Actions may not have an undefined "type" property. Have you misspelled a constant?'); }
// 若当前已经位于dispatch的流程中,则不允许再度发起dispatch(禁止套娃) if(isDispatching) { thrownewError('Reducers may not dispatch actions.'); }