Skip to content

Commit 737d5cc

Browse files
author
ben.durrant
committed
simplify injectInto implementation
1 parent e92c7db commit 737d5cc

File tree

2 files changed

+30
-49
lines changed

2 files changed

+30
-49
lines changed

packages/toolkit/src/createSlice.ts

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,8 @@ export function createSlice<
477477

478478
const selectSelf = (state: State) => state
479479

480-
const selectorCache = new WeakMap<
481-
(rootState: any) => State,
482-
Record<string, (rootState: any) => any>
483-
>()
484-
485480
const injectedSelectorCache = new WeakMap<
486-
CombinedSliceReducer<any>,
481+
Slice<State, CaseReducers, Name, ReducerPath, Selectors>,
487482
WeakMap<
488483
(rootState: any) => State | undefined,
489484
Record<string, (rootState: any) => any>
@@ -492,7 +487,7 @@ export function createSlice<
492487

493488
let _reducer: ReducerWithInitialState<State>
494489

495-
return {
490+
const slice: Slice<State, CaseReducers, Name, ReducerPath, Selectors> = {
496491
name,
497492
reducerPath,
498493
reducer(state, action) {
@@ -507,64 +502,44 @@ export function createSlice<
507502

508503
return _reducer.getInitialState()
509504
},
510-
getSelectors(selectState?: (rootState: any) => State) {
511-
if (selectState) {
512-
const cached = selectorCache.get(selectState)
513-
if (cached) {
514-
return cached
515-
}
516-
const selectors: Record<string, (rootState: any) => any> = {}
505+
getSelectors(selectState: (rootState: any) => State = selectSelf) {
506+
let selectorCache = injectedSelectorCache.get(this)
507+
if (!selectorCache) {
508+
selectorCache = new WeakMap()
509+
injectedSelectorCache.set(this, selectorCache)
510+
}
511+
let cached = selectorCache.get(selectState)
512+
if (!cached) {
513+
cached = {}
517514
for (const [name, selector] of Object.entries(
518515
options.selectors ?? {}
519516
)) {
520-
selectors[name] = (rootState: any, ...args: any[]) =>
521-
selector(selectState(rootState), ...args)
517+
cached[name] = (rootState: any, ...args: any[]) =>
518+
selector(
519+
selectState(rootState) ??
520+
(this !== slice ? this.getInitialState() : (undefined as any)),
521+
...args
522+
)
522523
}
523-
selectorCache.set(selectState, selectors)
524-
return selectors as any
525-
} else {
526-
return options.selectors ?? {}
524+
selectorCache.set(selectState, cached)
527525
}
526+
return cached as any
528527
},
529528
get selectors() {
530529
return this.getSelectors(defaultSelectSlice)
531530
},
532-
injectInto(reducer, { reducerPath, ...config } = {}) {
533-
reducer.inject(
531+
injectInto(injectable, { reducerPath, ...config } = {}) {
532+
injectable.inject(
534533
{ reducerPath: reducerPath ?? this.reducerPath, reducer: this.reducer },
535534
config
536535
)
537-
let selectorCache = injectedSelectorCache.get(reducer)
538-
if (!selectorCache) {
539-
selectorCache = new WeakMap()
540-
injectedSelectorCache.set(reducer, selectorCache)
541-
}
542536
return {
543537
...this,
544-
getSelectors(
545-
selectState: (rootState: any) => State | undefined = selectSelf
546-
) {
547-
const cached = selectorCache!.get(selectState)
548-
if (cached) {
549-
return cached
550-
}
551-
const selectors: Record<string, (rootState: any) => any> = {}
552-
for (const [name, selector] of Object.entries(
553-
options.selectors ?? {}
554-
)) {
555-
selectors[name] = (rootState: any, ...args: any[]) =>
556-
selector(
557-
selectState(rootState) ?? this.getInitialState(),
558-
...args
559-
)
560-
}
561-
selectorCache!.set(selectState, selectors)
562-
return selectors as any
563-
},
564538
get selectors() {
565539
return this.getSelectors(defaultSelectSlice)
566540
},
567541
} as any
568542
},
569543
}
544+
return slice
570545
}

packages/toolkit/src/tests/createSlice.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,17 +534,23 @@ describe('createSlice', () => {
534534

535535
const combinedReducer = combineSlices({
536536
static: slice.reducer,
537-
}).withLazyLoadedSlices<{ injected: number }>()
537+
}).withLazyLoadedSlices<WithSlice<typeof slice> & { injected2: number }>()
538538

539539
const uninjectedState = combinedReducer(undefined, increment())
540540

541541
expect(uninjectedState.injected).toBe(undefined)
542542

543-
slice.injectInto(combinedReducer, { reducerPath: 'injected' })
543+
slice.injectInto(combinedReducer)
544544

545545
const injectedState = combinedReducer(undefined, increment())
546546

547547
expect(injectedState.injected).toBe(slice.getInitialState() + 1)
548+
549+
slice.injectInto(combinedReducer, { reducerPath: 'injected2' })
550+
551+
const injected2State = combinedReducer(undefined, increment())
552+
553+
expect(injected2State.injected2).toBe(slice.getInitialState() + 1)
548554
})
549555
})
550556
})

0 commit comments

Comments
 (0)