Fix instrument build

This commit is contained in:
Nathan Bierema 2023-12-25 00:16:56 -05:00
parent a5957ed194
commit b630e5c7e9

View File

@ -4,7 +4,6 @@ import isPlainObject from 'lodash/isPlainObject';
import { import {
Action, Action,
Observer, Observer,
PreloadedState,
Reducer, Reducer,
Store, Store,
StoreEnhancer, StoreEnhancer,
@ -271,8 +270,8 @@ export const INIT_ACTION = { type: '@@INIT' };
/** /**
* Computes the next entry with exceptions catching. * Computes the next entry with exceptions catching.
*/ */
function computeWithTryCatch<S, A extends Action<string>>( function computeWithTryCatch<S, A extends Action<string>, PreloadedState>(
reducer: Reducer<S, A>, reducer: Reducer<S, A, PreloadedState>,
action: A, action: A,
state: S, state: S,
) { ) {
@ -301,8 +300,8 @@ function computeWithTryCatch<S, A extends Action<string>>(
/** /**
* Computes the next entry in the log by applying an action. * Computes the next entry in the log by applying an action.
*/ */
function computeNextEntry<S, A extends Action<string>>( function computeNextEntry<S, A extends Action<string>, PreloadedState>(
reducer: Reducer<S, A>, reducer: Reducer<S, A, PreloadedState>,
action: A, action: A,
state: S, state: S,
shouldCatchErrors: boolean | undefined, shouldCatchErrors: boolean | undefined,
@ -316,10 +315,10 @@ function computeNextEntry<S, A extends Action<string>>(
/** /**
* Runs the reducer on invalidated actions to get a fresh computation log. * Runs the reducer on invalidated actions to get a fresh computation log.
*/ */
function recomputeStates<S, A extends Action<string>>( function recomputeStates<S, A extends Action<string>, PreloadedState>(
computedStates: { state: S; error?: string }[], computedStates: { state: S; error?: string }[],
minInvalidatedStateIndex: number, minInvalidatedStateIndex: number,
reducer: Reducer<S, A>, reducer: Reducer<S, A, PreloadedState>,
committedState: S, committedState: S,
actionsById: { [actionId: number]: PerformAction<A> }, actionsById: { [actionId: number]: PerformAction<A> },
stagedActionIds: number[], stagedActionIds: number[],
@ -413,11 +412,12 @@ export interface LiftedState<S, A extends Action<string>, MonitorState> {
function liftReducerWith< function liftReducerWith<
S, S,
A extends Action<string>, A extends Action<string>,
PreloadedState,
MonitorState, MonitorState,
MonitorAction extends Action<string>, MonitorAction extends Action<string>,
>( >(
reducer: Reducer<S, A>, reducer: Reducer<S, A, PreloadedState>,
initialCommittedState: PreloadedState<S> | undefined, initialCommittedState: S | PreloadedState | undefined,
monitorReducer: Reducer<MonitorState, MonitorAction>, monitorReducer: Reducer<MonitorState, MonitorAction>,
options: Options<S, A, MonitorState, MonitorAction>, options: Options<S, A, MonitorState, MonitorAction>,
): Reducer<LiftedState<S, A, MonitorState>, LiftedAction<S, A, MonitorState>> { ): Reducer<LiftedState<S, A, MonitorState>, LiftedAction<S, A, MonitorState>> {
@ -575,7 +575,7 @@ function liftReducerWith<
if (maxAge && stagedActionIds.length > maxAge) { if (maxAge && stagedActionIds.length > maxAge) {
// States must be recomputed before committing excess. // States must be recomputed before committing excess.
computedStates = recomputeStates<S, A>( computedStates = recomputeStates<S, A, PreloadedState>(
computedStates, computedStates,
minInvalidatedStateIndex, minInvalidatedStateIndex,
reducer, reducer,
@ -872,6 +872,7 @@ export type EnhancedStore<S, A extends Action<string>, MonitorState> = Store<
function unliftStore< function unliftStore<
S, S,
A extends Action<string>, A extends Action<string>,
PreloadedState,
MonitorState, MonitorState,
MonitorAction extends Action<string>, MonitorAction extends Action<string>,
NextExt, NextExt,
@ -882,7 +883,9 @@ function unliftStore<
LiftedAction<S, A, MonitorState> LiftedAction<S, A, MonitorState>
> & > &
NextExt, NextExt,
liftReducer: (r: Reducer<S, A>) => LiftedReducer<S, A, MonitorState>, liftReducer: (
r: Reducer<S, A, PreloadedState>,
) => LiftedReducer<S, A, MonitorState>,
options: Options<S, A, MonitorState, MonitorAction>, options: Options<S, A, MonitorState, MonitorAction>,
) { ) {
let lastDefinedState: S & NextStateExt; let lastDefinedState: S & NextStateExt;
@ -924,7 +927,7 @@ function unliftStore<
replaceReducer(nextReducer: Reducer<S & NextStateExt, A>) { replaceReducer(nextReducer: Reducer<S & NextStateExt, A>) {
liftedStore.replaceReducer( liftedStore.replaceReducer(
liftReducer( liftReducer(
nextReducer as unknown as Reducer<S, A>, nextReducer as unknown as Reducer<S, A, PreloadedState>,
) as unknown as Reducer< ) as unknown as Reducer<
LiftedState<S, A, MonitorState> & NextStateExt, LiftedState<S, A, MonitorState> & NextStateExt,
LiftedAction<S, A, MonitorState> LiftedAction<S, A, MonitorState>
@ -1006,14 +1009,17 @@ export function instrument<
); );
} }
return <NextExt, NextStateExt>( return <
NextExt extends NonNullable<unknown>,
NextStateExt extends NonNullable<unknown>,
>(
createStore: StoreEnhancerStoreCreator<NextExt, NextStateExt>, createStore: StoreEnhancerStoreCreator<NextExt, NextStateExt>,
) => ) =>
<S, A extends Action<string>>( <S, A extends Action<string>, PreloadedState>(
reducer: Reducer<S, A>, reducer: Reducer<S, A, PreloadedState>,
initialState?: PreloadedState<S>, initialState?: PreloadedState | undefined,
) => { ) => {
function liftReducer(r: Reducer<S, A>) { function liftReducer(r: Reducer<S, A, PreloadedState>) {
if (typeof r !== 'function') { if (typeof r !== 'function') {
if (r && typeof (r as { default: unknown }).default === 'function') { if (r && typeof (r as { default: unknown }).default === 'function') {
throw new Error( throw new Error(
@ -1025,7 +1031,13 @@ export function instrument<
} }
throw new Error('Expected the reducer to be a function.'); throw new Error('Expected the reducer to be a function.');
} }
return liftReducerWith<S, A, MonitorState, MonitorAction>( return liftReducerWith<
S,
A,
PreloadedState,
MonitorState,
MonitorAction
>(
r, r,
initialState, initialState,
monitorReducer, monitorReducer,
@ -1057,12 +1069,17 @@ export function instrument<
return unliftStore< return unliftStore<
S, S,
A, A,
PreloadedState,
MonitorState, MonitorState,
MonitorAction, MonitorAction,
NextExt, NextExt,
NextStateExt NextStateExt
>( >(
liftedStore, liftedStore as Store<
LiftedState<S, A, MonitorState> & NextStateExt,
LiftedAction<S, A, MonitorState>
> &
NextExt,
liftReducer, liftReducer,
options as unknown as Options<S, A, MonitorState, MonitorAction>, options as unknown as Options<S, A, MonitorState, MonitorAction>,
); );