Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Commit c590dbf

Browse files
committed
fix: independent ref state option (CT-000)
1 parent 5a9fb9c commit c590dbf

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@ type ReducerMap<T extends Record<string, any>, A> = {
77
type ReducerMapState<T> = T extends ReducerMap<infer R, any> ? R : never;
88
type ReducerMapAction<T> = T extends ReducerMap<any, infer R> ? R : never;
99

10+
interface CompositeReducerOptions {
11+
independent?: boolean;
12+
}
13+
1014
const compositeReducer =
1115
<S extends Record<string, any>, A extends Record<string, any>, M extends ReducerMap<Record<string, any>, any>>(
1216
rootReducer: RootReducer<S, A>,
13-
reducers: M
17+
reducers: M,
18+
options: CompositeReducerOptions = {}
1419
): RootReducer<S & ReducerMapState<M>, A | ReducerMapAction<M>> =>
1520
(state, action) =>
1621
Object.keys(reducers).reduce((acc, key) => {
17-
if (acc) {
18-
const subState = reducers[key]!(acc[key], action as ReducerMapAction<M>);
19-
if (subState !== acc[key]) {
22+
const refState = options.independent ? state : acc;
23+
if (refState) {
24+
const subState = reducers[key]!(refState[key], action as ReducerMapAction<M>);
25+
if (subState !== refState[key]) {
2026
return {
21-
...acc,
27+
...refState,
2228
[key]: subState,
2329
};
2430
}

0 commit comments

Comments
 (0)