You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
hexmap/client/src/reducers/AppStateReducer.ts

50 lines
1.9 KiB

import {AppAction} from "../../../common/src/actions/AppAction";
import {AppState} from "../state/AppState";
import {isTileAction} from "../../../common/src/actions/TileAction";
import {tileReducer} from "./TileReducer";
import {networkReducer} from "./NetworkReducer";
import {CLIENT_PENDING, isSendableAction} from "../../../common/src/actions/ClientAction";
import {syncedStateReducer} from "../../../common/src/reducers/SyncedStateReducer";
import {exhaustivenessCheck} from "../../../common/src/util/TypeUtils";
import {isMapAction, MapAction} from "../../../common/src/actions/MapAction";
import {isUserAction, UserAction} from "../../../common/src/actions/UserAction";
import {clientReducer} from "./ClientReducer";
import {isNetworkAction} from "../../../common/src/actions/NetworkAction";
function appSyncedStateReducer(oldState: AppState, action: MapAction|UserAction): AppState {
if (oldState.localState === null) {
return oldState
}
const localState = syncedStateReducer(oldState.localState, action)
if (localState === oldState.localState) {
return oldState
}
if (isSendableAction(action)) {
return {
...oldState,
localState,
network: clientReducer(oldState.network, {
type: CLIENT_PENDING,
pending: action
})
}
} else {
return {
...oldState,
localState
}
}
}
export function appStateReducer(oldState: AppState, action: AppAction): AppState {
if (isMapAction(action) || isUserAction(action)) {
return appSyncedStateReducer(oldState, action)
} else if (isTileAction(action)) {
return tileReducer(oldState, action)
} else if (isNetworkAction(action)) {
return networkReducer(oldState, action)
}
exhaustivenessCheck(action)
}
export type AppStateReducer = typeof appStateReducer;