@@ -88,22 +88,21 @@ export interface Slice<
88
88
/**
89
89
* Get localised slice selectors (expects to be called with *just* the slice's state as the first parameter)
90
90
*/
91
- getSelectors ( this : this ) : Id < SliceDefinedSelectors < State , Selectors , State > >
91
+ getSelectors ( ) : Id < SliceDefinedSelectors < State , Selectors , State > >
92
92
93
93
/**
94
94
* Get globalised slice selectors (`selectState` callback is expected to receive first parameter and return slice state)
95
95
*/
96
96
getSelectors < RootState > (
97
- this : this,
98
- selectState : ( this : this, rootState : RootState ) => State
97
+ selectState : ( rootState : RootState ) => State
99
98
) : Id < SliceDefinedSelectors < State , Selectors , RootState > >
100
99
101
100
/**
102
101
* Selectors that assume the slice's state is `rootState[slice.reducerPath]` (which is usually the case)
103
102
*
104
103
* Equivalent to `slice.getSelectors((state: RootState) => state[slice.reducerPath])`.
105
104
*/
106
- selectors : Id <
105
+ get selectors ( ) : Id <
107
106
SliceDefinedSelectors < State , Selectors , { [ K in ReducerPath ] : State } >
108
107
>
109
108
@@ -126,7 +125,7 @@ export interface Slice<
126
125
*
127
126
* Will throw an error if slice is not found.
128
127
*/
129
- selectSlice ( this : this , state : { [ K in ReducerPath ] : State } ) : State
128
+ selectSlice ( state : { [ K in ReducerPath ] : State } ) : State
130
129
}
131
130
132
131
/**
@@ -153,15 +152,15 @@ interface InjectedSlice<
153
152
* Get globalised slice selectors (`selectState` callback is expected to receive first parameter and return slice state)
154
153
*/
155
154
getSelectors < RootState > (
156
- selectState : ( this : this , rootState : RootState ) => State | undefined
155
+ selectState : ( rootState : RootState ) => State | undefined
157
156
) : Id < SliceDefinedSelectors < State , Selectors , RootState > >
158
157
159
158
/**
160
159
* Selectors that assume the slice's state is `rootState[slice.name]` (which is usually the case)
161
160
*
162
161
* Equivalent to `slice.getSelectors((state: RootState) => state[slice.name])`.
163
162
*/
164
- selectors : Id <
163
+ get selectors ( ) : Id <
165
164
SliceDefinedSelectors <
166
165
State ,
167
166
Selectors ,
@@ -740,8 +739,8 @@ export function buildCreateSlice({ creators }: BuildCreateSliceConfig = {}) {
740
739
741
740
const selectSelf = ( state : State ) = > state
742
741
743
- const injectedSelectorCache = new WeakMap <
744
- Slice < State , CaseReducers , Name , ReducerPath , Selectors > ,
742
+ const injectedSelectorCache = new Map <
743
+ string ,
745
744
WeakMap <
746
745
( rootState : any ) => State | undefined ,
747
746
Record < string , ( rootState : any ) => any >
@@ -750,23 +749,42 @@ export function buildCreateSlice({ creators }: BuildCreateSliceConfig = {}) {
750
749
751
750
let _reducer : ReducerWithInitialState < State >
752
751
753
- const slice : Slice < State , CaseReducers , Name , ReducerPath , Selectors > = {
754
- name,
755
- reducerPath,
756
- reducer ( state , action ) {
757
- if ( ! _reducer ) _reducer = buildReducer ( )
752
+ function reducer ( state : State | undefined , action : UnknownAction ) {
753
+ if ( ! _reducer ) _reducer = buildReducer ( )
758
754
759
- return _reducer ( state , action )
760
- } ,
761
- actions : context . actionCreators as any ,
762
- caseReducers : context . sliceCaseReducersByName as any ,
763
- getInitialState ( ) {
764
- if ( ! _reducer ) _reducer = buildReducer ( )
755
+ return _reducer ( state , action )
756
+ }
765
757
766
- return _reducer . getInitialState ( )
767
- } ,
768
- getSelectors ( selectState : ( rootState : any ) => State = selectSelf ) {
769
- const selectorCache = emplace ( injectedSelectorCache , this , {
758
+ function getInitialState ( ) {
759
+ if ( ! _reducer ) _reducer = buildReducer ( )
760
+
761
+ return _reducer . getInitialState ( )
762
+ }
763
+
764
+ function makeSelectorProps (
765
+ reducerPath : ReducerPath ,
766
+ injected = false
767
+ ) : Pick <
768
+ Slice < State , CaseReducers , Name , ReducerPath , Selectors > ,
769
+ 'getSelectors' | 'selectors' | 'selectSlice' | 'reducerPath'
770
+ > {
771
+ function selectSlice ( state : { [ K in ReducerPath ] : State } ) {
772
+ let sliceState = state [ reducerPath ]
773
+ if ( typeof sliceState === 'undefined' ) {
774
+ if ( injected ) {
775
+ sliceState = getInitialState ( )
776
+ } else if ( process . env . NODE_ENV !== 'production' ) {
777
+ throw new Error (
778
+ 'selectSlice returned undefined for an uninjected slice reducer'
779
+ )
780
+ }
781
+ }
782
+ return sliceState
783
+ }
784
+ function getSelectors (
785
+ selectState : ( rootState : any ) => State = selectSelf
786
+ ) {
787
+ const selectorCache = emplace ( injectedSelectorCache , reducerPath , {
770
788
insert : ( ) => new WeakMap ( ) ,
771
789
} )
772
790
@@ -777,39 +795,39 @@ export function buildCreateSlice({ creators }: BuildCreateSliceConfig = {}) {
777
795
options . selectors ?? { }
778
796
) ) {
779
797
map [ name ] = wrapSelector (
780
- this ,
798
+ { getInitialState } ,
781
799
selector ,
782
800
selectState ,
783
- this !== slice
801
+ injected
784
802
)
785
803
}
786
804
return map
787
805
} ,
788
806
} ) as any
789
- } ,
790
- selectSlice ( state ) {
791
- let sliceState = state [ this . reducerPath ]
792
- if ( typeof sliceState === 'undefined' ) {
793
- // check if injectInto has been called
794
- if ( this !== slice ) {
795
- sliceState = this . getInitialState ( )
796
- } else if ( process . env . NODE_ENV !== 'production' ) {
797
- throw new Error (
798
- 'selectSlice returned undefined for an uninjected slice reducer'
799
- )
800
- }
801
- }
802
- return sliceState
803
- } ,
804
- get selectors ( ) {
805
- return this . getSelectors ( this . selectSlice )
806
- } ,
807
+ }
808
+ return {
809
+ reducerPath,
810
+ getSelectors ,
811
+ get selectors ( ) {
812
+ return getSelectors ( selectSlice )
813
+ } ,
814
+ selectSlice ,
815
+ }
816
+ }
817
+
818
+ const slice : Slice < State , CaseReducers , Name , ReducerPath , Selectors > = {
819
+ name ,
820
+ reducer ,
821
+ actions : context . actionCreators as any ,
822
+ caseReducers : context . sliceCaseReducersByName as any ,
823
+ getInitialState ,
824
+ ... makeSelectorProps ( reducerPath ) ,
807
825
injectInto ( injectable , { reducerPath : pathOpt , ...config } = { } ) {
808
- const reducerPath = pathOpt ?? this . reducerPath
809
- injectable . inject ( { reducerPath, reducer : this . reducer } , config )
826
+ const newReducerPath = pathOpt ?? reducerPath
827
+ injectable . inject ( { reducerPath : newReducerPath , reducer } , config )
810
828
return {
811
- ...this ,
812
- reducerPath ,
829
+ ...slice ,
830
+ ... makeSelectorProps ( newReducerPath as any , true ) ,
813
831
} as any
814
832
} ,
815
833
}
@@ -818,13 +836,13 @@ export function buildCreateSlice({ creators }: BuildCreateSliceConfig = {}) {
818
836
}
819
837
820
838
function wrapSelector < State , NewState , S extends Selector < State > > (
821
- slice : Slice ,
839
+ slice : { getInitialState ( ) : State } ,
822
840
selector : S ,
823
841
selectState : Selector < NewState , State > ,
824
842
injected ?: boolean
825
843
) {
826
844
function wrapper ( rootState : NewState , ...args : any [ ] ) {
827
- let sliceState = selectState . call ( slice , rootState )
845
+ let sliceState = selectState ( rootState )
828
846
if ( typeof sliceState === 'undefined' ) {
829
847
if ( injected ) {
830
848
sliceState = slice . getInitialState ( )
0 commit comments