license import { Action } from "./Action" ; import { Reducer } from "./Reducer" ; import { ReducerMap } from "./ReducerMap" ; const hasOwnProperty = Object . prototype . hasOwnProperty ; function reduce < S > ( state , action , reducerMap , reducerKeys ) { let newState = undefined ; for ( let i = 0 ; i < reducerKeys . length ; i ++ ) { const key = reducerKeys [ i ] ; const statePart = state [ key ] ; const newStatePart = reducerMap [ key ] ( statePart , action ) ; if ( newStatePart !== statePart ) { if ( ! newState ) { newState = { ... state } ; } newState [ key ] = newStatePart ; } } return newState !== undefined ? newState : state ; } function verifySameShape < S > ( state , reducerMap ) { for ( const key in state ) { if ( hasOwnProperty . call ( state , key ) && ! hasOwnProperty . call ( reducerMap , key ) ) { throw new Error ( `mismatched shapes in combineReducers(): reducers missing '${ key }'` ) ; } } for ( const key in reducerMap ) { if ( hasOwnProperty . call ( reducerMap , key ) && ! hasOwnProperty . call ( state , key ) ) { throw new Error ( `mismatched shapes in combineReducers(): state missing '${ key }'` ) ; } } } template S param reducerMap returns export function combineReducers < S > ( reducerMap ) { let hasVerifiedShape = false ; const reducerKeys = Object . keys ( reducerMap ) as ( keyof S ) [ ] ; return ( state , action ) => { if ( typeof process === "undefined" || process . env . NODE_ENV !== "production" ) { if ( state === undefined || state === null || typeof state !== "object" ) { throw new Error ( "combineReducers() requires object state; received " + state ) ; } if ( ! hasVerifiedShape ) { verifySameShape ( state , reducerMap ) ; hasVerifiedShape = true ; } } return reduce ( state , action , reducerMap , reducerKeys ) ; } ; }