Directorio de artículos
preprocesamiento
1. Atraviesa el objeto, todos los valores son funcionales y contados.
const reducerKeys = Object.keys(reducers)
const finalReducers: ReducersMapObject = {
}
//筛选函数
for (let i = 0; i < reducerKeys.length; i++) {
const key = reducerKeys[i]
if (process.env.NODE_ENV !== 'production') {
if (typeof reducers[key] === 'undefined') {
warning(`No reducer provided for key "${
key}"`)
}
}
if (typeof reducers[key] === 'function') {
finalReducers[key] = reducers[key]
}
}
const finalReducerKeys = Object.keys(finalReducers)
2. Manejo de excepciones.
let unexpectedKeyCache: {
[key: string]: true }
if (process.env.NODE_ENV !== 'production') {
unexpectedKeyCache = {
}
}
let shapeAssertionError: unknown
try {
assertReducerShape(finalReducers)
} catch (e) {
shapeAssertionError = e
}
3. Volver al Reductor final
return function combination(
state: StateFromReducersMapObject<typeof reducers> = {
},
action: AnyAction
) {
//...
}
Reductor Total
1. Continúe con el manejo de excepciones.
if (shapeAssertionError) {
throw shapeAssertionError
}
if (process.env.NODE_ENV !== 'production') {
const warningMessage = getUnexpectedStateShapeWarningMessage(
state,
finalReducers,
action,
unexpectedKeyCache
)
if (warningMessage) {
warning(warningMessage)
}
}
2. Proceso de actualización de estado.
let hasChanged = false
const nextState: StateFromReducersMapObject<typeof reducers> = {
}
for (let i = 0; i < finalReducerKeys.length; i++) {
const key = finalReducerKeys[i]
const reducer = finalReducers[key]
const previousStateForKey = state[key]
const nextStateForKey = reducer(previousStateForKey, action)
if (typeof nextStateForKey === 'undefined') {
const actionType = action && action.type
throw new Error(
`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.`
)
}
nextState[key] = nextStateForKey
hasChanged = hasChanged || nextStateForKey !== previousStateForKey
}
hasChanged =
hasChanged || finalReducerKeys.length !== Object.keys(state).length
return hasChanged ? nextState : state
Proceso de actualización de estado
1, el estado inicial.
Un indicador que indica si se ha producido una actualización.
Un nuevo estado, un objeto vacío.
let hasChanged = false
const nextState: StateFromReducersMapObject<typeof reducers> = {
}
2. Atraviese cada Reductor.
for (let i = 0; i < finalReducerKeys.length; i++) {
const key = finalReducerKeys[i]
//目标Reducer
const reducer = finalReducers[key]
//旧状态
const previousStateForKey = state[key]
//新状态
const nextStateForKey = reducer(previousStateForKey, action)
//没新状态?
if (typeof nextStateForKey === 'undefined') {
const actionType = action && action.type
throw new Error(
`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.`
)
}
//新状态加入
nextState[key] = nextStateForKey
//判断是否改变
hasChanged = hasChanged || nextStateForKey !== previousStateForKey
}
3. Posprocesamiento.
hasChanged = hasChanged || finalReducerKeys.length !== Object.keys(state).length
return hasChanged ? nextState : state
Análisis de Procesos Críticos
1. Determinar si el estado ha cambiado
Al principio, nada cambió.
let hasChanged = false
Hay una frase en el bucle:
hasChanged = hasChanged || nextStateForKey !== previousStateForKey
Si cambia, no te muevas.
Si no hay cambio, el nuevo estado y el antiguo estado se juzgan congruentemente.
Después de que termine el bucle:
hasChanged = hasChanged || finalReducerKeys.length !== Object.keys(state).length
Si cambia, no te muevas.
Si no ha cambiado, juzgue la longitud del Reductor y el estado.
Al regresar:
return hasChanged ? nextState : state
Si se cambia, devuelve el estado nuevo, de lo contrario, el estado anterior.