@@ -15,7 +15,7 @@ import type {
15
15
import { createReducer } from './createReducer'
16
16
import type { ActionReducerMapBuilder } from './mapBuilders'
17
17
import { executeReducerBuilderCallback } from './mapBuilders'
18
- import type { ActionFromMatcher , Id , Matcher , Tail } from './tsHelpers'
18
+ import type { Id , Tail } from './tsHelpers'
19
19
import type { InjectConfig } from './combineSlices'
20
20
import type {
21
21
AsyncThunk ,
@@ -78,13 +78,14 @@ export interface Slice<
78
78
/**
79
79
* Get localised slice selectors (expects to be called with *just* the slice's state as the first parameter)
80
80
*/
81
- getSelectors ( ) : Id < SliceDefinedSelectors < State , Selectors , State > >
81
+ getSelectors ( this : this ) : Id < SliceDefinedSelectors < State , Selectors , State > >
82
82
83
83
/**
84
84
* Get globalised slice selectors (`selectState` callback is expected to receive first parameter and return slice state)
85
85
*/
86
86
getSelectors < RootState > (
87
- selectState : ( rootState : RootState ) => State
87
+ this : this,
88
+ selectState : ( this : this, rootState : RootState ) => State
88
89
) : Id < SliceDefinedSelectors < State , Selectors , RootState > >
89
90
90
91
/**
@@ -100,6 +101,7 @@ export interface Slice<
100
101
* Inject slice into provided reducer (return value from `combineSlices`), and return injected slice.
101
102
*/
102
103
injectInto < NewReducerPath extends string = ReducerPath > (
104
+ this : this,
103
105
injectable : {
104
106
inject : (
105
107
slice : { reducerPath : string ; reducer : Reducer } ,
@@ -108,6 +110,13 @@ export interface Slice<
108
110
} ,
109
111
config ?: InjectIntoConfig < NewReducerPath >
110
112
) : InjectedSlice < State , CaseReducers , Name , NewReducerPath , Selectors >
113
+
114
+ /**
115
+ * Select the slice state, using the slice's current reducerPath.
116
+ *
117
+ * Will throw an error if slice is not found.
118
+ */
119
+ selectSlice ( this : this, state : { [ K in ReducerPath ] : State } ) : State
111
120
}
112
121
113
122
/**
@@ -134,7 +143,7 @@ interface InjectedSlice<
134
143
* Get globalised slice selectors (`selectState` callback is expected to receive first parameter and return slice state)
135
144
*/
136
145
getSelectors < RootState > (
137
- selectState : ( rootState : RootState ) => State | undefined
146
+ selectState : ( this : this , rootState : RootState ) => State | undefined
138
147
) : Id < SliceDefinedSelectors < State , Selectors , RootState > >
139
148
140
149
/**
@@ -149,6 +158,13 @@ interface InjectedSlice<
149
158
{ [ K in ReducerPath ] ?: State | undefined }
150
159
>
151
160
>
161
+
162
+ /**
163
+ * Select the slice state, using the slice's current reducerPath.
164
+ *
165
+ * Returns initial state if slice is not found.
166
+ */
167
+ selectSlice ( state : { [ K in ReducerPath ] ?: State | undefined } ) : State
152
168
}
153
169
154
170
/**
@@ -660,10 +676,6 @@ export function createSlice<
660
676
} )
661
677
}
662
678
663
- const defaultSelectSlice = (
664
- rootState : { [ K in ReducerPath ] : State }
665
- ) : State => rootState [ reducerPath ]
666
-
667
679
const selectSelf = ( state : State ) = > state
668
680
669
681
const injectedSelectorCache = new WeakMap <
@@ -704,7 +716,7 @@ export function createSlice<
704
716
options . selectors ?? { }
705
717
) ) {
706
718
cached [ name ] = ( rootState : any , ...args : any [ ] ) => {
707
- let sliceState = selectState ( rootState )
719
+ let sliceState = selectState . call ( this , rootState )
708
720
if ( typeof sliceState === 'undefined' ) {
709
721
// check if injectInto has been called
710
722
if ( this !== slice ) {
@@ -722,19 +734,29 @@ export function createSlice<
722
734
}
723
735
return cached as any
724
736
} ,
737
+ selectSlice ( state ) {
738
+ let sliceState = state [ this . reducerPath ]
739
+ if ( typeof sliceState === 'undefined' ) {
740
+ // check if injectInto has been called
741
+ if ( this !== slice ) {
742
+ sliceState = this . getInitialState ( )
743
+ } else if ( process . env . NODE_ENV !== 'production' ) {
744
+ throw new Error (
745
+ 'selectSlice returned undefined for an uninjected slice reducer'
746
+ )
747
+ }
748
+ }
749
+ return sliceState
750
+ } ,
725
751
get selectors ( ) {
726
- return this . getSelectors ( defaultSelectSlice )
752
+ return this . getSelectors ( this . selectSlice )
727
753
} ,
728
754
injectInto ( injectable , { reducerPath : pathOpt , ...config } = { } ) {
729
755
const reducerPath = pathOpt ?? this . reducerPath
730
756
injectable . inject ( { reducerPath, reducer : this . reducer } , config )
731
- const selectSlice = ( state : any ) => state [ reducerPath ]
732
757
return {
733
758
...this ,
734
759
reducerPath,
735
- get selectors ( ) {
736
- return this . getSelectors ( selectSlice )
737
- } ,
738
760
} as any
739
761
} ,
740
762
}
0 commit comments