Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

memoize the wrappedReducer function #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 59 additions & 52 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,63 +181,70 @@ export function useEffectReducer<
effectsMap?: EffectsMap<TState, TEvent, TEffect>
): [TState, React.Dispatch<TEvent | TEvent['type']>] {
const entitiesRef = useRef<Set<EffectEntity<TState, TEvent>>>(new Set());
const wrappedReducer = (
[state, stateEffectTuples, entitiesToStop]: AggregatedEffectsState<
TState,
TEvent
>,
event: TEvent | FlushEvent
): AggregatedEffectsState<TState, TEvent> => {
const nextEffectEntities: Array<EffectEntity<TState, TEvent>> = [];
const nextEntitiesToStop: Array<EffectEntity<TState, TEvent>> = [];

if (event.type === flushEffectsSymbol) {
// Record that effects have already been executed
return [state, stateEffectTuples.slice(event.count), nextEntitiesToStop];
}

const exec = (
effect: TEffect | EffectFunction<TState, TEvent, TEffect>
) => {
const effectObject = toEffectObject(effect, effectsMap);
const effectEntity = createEffectEntity<TState, TEvent, TEffect>(
effectObject
);
nextEffectEntities.push(effectEntity);
const wrappedReducer = useMemo(
() => (
[state, stateEffectTuples, entitiesToStop]: AggregatedEffectsState<
TState,
TEvent
>,
event: TEvent | FlushEvent
): AggregatedEffectsState<TState, TEvent> => {
const nextEffectEntities: Array<EffectEntity<TState, TEvent>> = [];
const nextEntitiesToStop: Array<EffectEntity<TState, TEvent>> = [];

if (event.type === flushEffectsSymbol) {
// Record that effects have already been executed
return [
state,
stateEffectTuples.slice(event.count),
nextEntitiesToStop,
];
}

return effectEntity;
};
const exec = (
effect: TEffect | EffectFunction<TState, TEvent, TEffect>
) => {
const effectObject = toEffectObject(effect, effectsMap);
const effectEntity = createEffectEntity<TState, TEvent, TEffect>(
effectObject
);
nextEffectEntities.push(effectEntity);

exec.stop = (entity: EffectEntity<TState, TEvent>) => {
nextEntitiesToStop.push(entity);
};
return effectEntity;
};

exec.replace = (
entity: EffectEntity<TState, TEvent>,
effect: TEffect | EffectFunction<TState, TEvent, TEffect>
) => {
if (entity) {
exec.stop = (entity: EffectEntity<TState, TEvent>) => {
nextEntitiesToStop.push(entity);
}
return exec(effect);
};
};

exec.replace = (
entity: EffectEntity<TState, TEvent>,
effect: TEffect | EffectFunction<TState, TEvent, TEffect>
) => {
if (entity) {
nextEntitiesToStop.push(entity);
}
return exec(effect);
};

const nextState = effectReducer(
state,
event,
exec as EffectReducerExec<TState, TEvent, TEffect>
);

return [
nextState,
nextEffectEntities.length
? [...stateEffectTuples, [nextState, nextEffectEntities]]
: stateEffectTuples,
entitiesToStop.length
? [...entitiesToStop, ...nextEntitiesToStop]
: nextEntitiesToStop,
];
};
const nextState = effectReducer(
state,
event,
exec as EffectReducerExec<TState, TEvent, TEffect>
);

return [
nextState,
nextEffectEntities.length
? [...stateEffectTuples, [nextState, nextEffectEntities]]
: stateEffectTuples,
entitiesToStop.length
? [...entitiesToStop, ...nextEntitiesToStop]
: nextEntitiesToStop,
];
},
[effectReducer, effectsMap]
);

const initialStateAndEffects: AggregatedEffectsState<
TState,
Expand Down