@@ -23,6 +23,14 @@ type DevtoolsType = {
23
23
error : ( payload : any ) => void
24
24
}
25
25
26
+ export type StoreApiWithRedux <
27
+ T extends State ,
28
+ A extends { type : unknown }
29
+ > = StoreApi < T & { dispatch : ( a : A ) => A } > & {
30
+ dispatch : ( a : A ) => A
31
+ devtools ?: DevtoolsType
32
+ }
33
+
26
34
export const redux =
27
35
< S extends State , A extends { type : unknown } > (
28
36
reducer : ( state : S , action : A ) => S ,
@@ -31,10 +39,7 @@ export const redux =
31
39
(
32
40
set : SetState < S & { dispatch : ( a : A ) => A } > ,
33
41
get : GetState < S & { dispatch : ( a : A ) => A } > ,
34
- api : StoreApi < S & { dispatch : ( a : A ) => A } > & {
35
- dispatch : ( a : A ) => A
36
- devtools ?: DevtoolsType
37
- }
42
+ api : StoreApiWithRedux < S , A >
38
43
) : S & { dispatch : ( a : A ) => A } => {
39
44
api . dispatch = ( action : A ) => {
40
45
set ( ( state : S ) => reducer ( state , action ) )
@@ -59,24 +64,18 @@ export type NamedSet<T extends State> = {
59
64
) : void
60
65
}
61
66
67
+ export type StoreApiWithDevtools < T extends State > = StoreApi < T > & {
68
+ devtools ?: DevtoolsType
69
+ }
70
+
62
71
export const devtools =
63
72
<
64
73
S extends State ,
65
- InnerCustomSetState extends NamedSet < S > ,
66
- InnerCustomGetState extends GetState < S > ,
67
- InnerCustomStoreApi extends StoreApi < S > & {
68
- dispatch ?: unknown
69
- devtools ?: DevtoolsType
70
- } ,
71
- OuterCustomSetState extends SetState < S > ,
72
- OuterCustomGetState extends InnerCustomGetState ,
73
- OuterCustomStoreApi extends InnerCustomStoreApi
74
+ CustomSetState extends SetState < S > ,
75
+ CustomGetState extends GetState < S > ,
76
+ CustomStoreApi extends StoreApiWithDevtools < S >
74
77
> (
75
- fn : (
76
- set : InnerCustomSetState ,
77
- get : InnerCustomGetState ,
78
- api : InnerCustomStoreApi
79
- ) => S ,
78
+ fn : ( set : NamedSet < S > , get : CustomGetState , api : CustomStoreApi ) => S ,
80
79
options ?:
81
80
| string
82
81
| {
@@ -99,9 +98,9 @@ export const devtools =
99
98
}
100
99
) =>
101
100
(
102
- set : OuterCustomSetState ,
103
- get : OuterCustomGetState ,
104
- api : OuterCustomStoreApi
101
+ set : CustomSetState ,
102
+ get : CustomGetState ,
103
+ api : CustomStoreApi & { dispatch ?: unknown }
105
104
) : S => {
106
105
let extension
107
106
try {
@@ -118,23 +117,15 @@ export const devtools =
118
117
console . warn ( 'Please install/enable Redux devtools extension' )
119
118
}
120
119
delete api . devtools
121
- return fn (
122
- set as unknown as InnerCustomSetState ,
123
- get as InnerCustomGetState ,
124
- api as InnerCustomStoreApi
125
- )
120
+ return fn ( set , get , api )
126
121
}
127
122
const namedSet : NamedSet < S > = ( state , replace , name ) => {
128
123
set ( state , replace )
129
124
if ( ! api . dispatch && api . devtools ) {
130
125
api . devtools . send ( api . devtools . prefix + ( name || 'action' ) , get ( ) )
131
126
}
132
127
}
133
- const initialState = fn (
134
- namedSet as InnerCustomSetState ,
135
- get as InnerCustomGetState ,
136
- api as InnerCustomStoreApi
137
- )
128
+ const initialState = fn ( namedSet , get , api )
138
129
if ( ! api . devtools ) {
139
130
const savedSetState = api . setState
140
131
api . setState = <
@@ -213,79 +204,35 @@ export const devtools =
213
204
return initialState
214
205
}
215
206
216
- type SubscribeWithSelector < T extends State > = {
217
- ( listener : StateListener < T > ) : ( ) => void
218
- < StateSlice > (
219
- selector : StateSelector < T , StateSlice > ,
220
- listener : StateSliceListener < StateSlice > ,
221
- options ?: {
222
- equalityFn ?: EqualityChecker < StateSlice >
223
- fireImmediately ?: boolean
224
- }
225
- ) : ( ) => void
226
- }
227
-
228
- export function subscribeWithSelector < S extends State > (
229
- fn : (
230
- set : SetState < S > ,
231
- get : GetState < S > ,
232
- api : Omit < StoreApi < S > , 'subscribe' > & {
233
- subscribe : SubscribeWithSelector < S >
234
- subscribeWithSelectorEnabled : true
235
- }
236
- ) => S
237
- ) : (
238
- set : SetState < S > ,
239
- get : GetState < S > ,
240
- api : Omit < StoreApi < S > , 'subscribe' > & {
241
- subscribe : SubscribeWithSelector < S >
242
- subscribeWithSelectorEnabled : true
207
+ export type StoreApiWithSubscribeWithSelector < T extends State > = Omit <
208
+ StoreApi < T > ,
209
+ 'subscribe'
210
+ > & {
211
+ subscribe : {
212
+ ( listener : StateListener < T > ) : ( ) => void
213
+ < StateSlice > (
214
+ selector : StateSelector < T , StateSlice > ,
215
+ listener : StateSliceListener < StateSlice > ,
216
+ options ?: {
217
+ equalityFn ?: EqualityChecker < StateSlice >
218
+ fireImmediately ?: boolean
219
+ }
220
+ ) : ( ) => void
243
221
}
244
- ) => S
222
+ // Note: This will be removed in v4
223
+ subscribeWithSelectorEnabled : true
224
+ }
245
225
246
- export function subscribeWithSelector <
247
- S extends State ,
248
- CustomSetState extends SetState < S >
249
- > (
250
- fn : (
251
- set : CustomSetState ,
252
- get : GetState < S > ,
253
- api : Omit < StoreApi < S > , 'subscribe' > & {
254
- subscribe : SubscribeWithSelector < S >
255
- subscribeWithSelectorEnabled : true
256
- }
257
- ) => S
258
- ) : (
259
- set : CustomSetState ,
260
- get : GetState < S > ,
261
- api : Omit < StoreApi < S > , 'subscribe' > & {
262
- subscribe : SubscribeWithSelector < S >
263
- subscribeWithSelectorEnabled : true
264
- }
265
- ) => S
266
-
267
- export function subscribeWithSelector <
268
- S extends State ,
269
- CustomSetState extends SetState < S > ,
270
- CustomGetState extends GetState < S > ,
271
- CustomStoreApi extends Omit < StoreApi < S > , 'subscribe' > & {
272
- subscribe : SubscribeWithSelector < S >
273
- subscribeWithSelectorEnabled : true
274
- }
275
- > (
276
- fn : ( set : CustomSetState , get : CustomGetState , api : CustomStoreApi ) => S
277
- ) : ( set : CustomSetState , get : CustomGetState , api : CustomStoreApi ) => S
278
-
279
- export function subscribeWithSelector <
280
- S extends State ,
281
- CustomSetState extends SetState < S > ,
282
- CustomGetState extends GetState < S > ,
283
- CustomStoreApi extends Omit < StoreApi < S > , 'subscribe' > & {
284
- subscribe : SubscribeWithSelector < S >
285
- subscribeWithSelectorEnabled : true
286
- }
287
- > ( fn : ( set : CustomSetState , get : CustomGetState , api : CustomStoreApi ) => S ) {
288
- return ( set : CustomSetState , get : CustomGetState , api : CustomStoreApi ) : S => {
226
+ export const subscribeWithSelector =
227
+ <
228
+ S extends State ,
229
+ CustomSetState extends SetState < S > ,
230
+ CustomGetState extends GetState < S > ,
231
+ CustomStoreApi extends StoreApiWithSubscribeWithSelector < S >
232
+ > (
233
+ fn : ( set : CustomSetState , get : CustomGetState , api : CustomStoreApi ) => S
234
+ ) =>
235
+ ( set : CustomSetState , get : CustomGetState , api : CustomStoreApi ) : S => {
289
236
const origSubscribe = api . subscribe as Subscribe < S >
290
237
api . subscribe = ( ( selector : any , optListener : any , options : any ) => {
291
238
let listener : StateListener < S > = selector // if no selector
@@ -304,65 +251,35 @@ export function subscribeWithSelector<
304
251
}
305
252
}
306
253
return origSubscribe ( listener )
307
- } ) as SubscribeWithSelector < S >
254
+ } ) as any
255
+ // Note: This will be removed in v4
308
256
api . subscribeWithSelectorEnabled = true
309
257
const initialState = fn ( set , get , api )
310
258
return initialState
311
259
}
312
- }
313
260
314
261
type Combine < T , U > = Omit < T , keyof U > & U
315
262
316
- export function combine <
317
- PrimaryState extends State ,
318
- SecondaryState extends State
319
- > (
320
- initialState : PrimaryState ,
321
- create : (
322
- set : NamedSet < PrimaryState > ,
323
- get : GetState < PrimaryState > ,
324
- api : StoreApi < PrimaryState >
325
- ) => SecondaryState
326
- ) : (
327
- set : NamedSet < Combine < PrimaryState , SecondaryState > > ,
328
- get : GetState < Combine < PrimaryState , SecondaryState > > ,
329
- api : StoreApi < Combine < PrimaryState , SecondaryState > >
330
- ) => Combine < PrimaryState , SecondaryState >
331
-
332
- export function combine <
333
- PrimaryState extends State ,
334
- SecondaryState extends State
335
- > (
336
- initialState : PrimaryState ,
337
- create : (
338
- set : SetState < PrimaryState > ,
339
- get : GetState < PrimaryState > ,
340
- api : StoreApi < PrimaryState >
341
- ) => SecondaryState
342
- ) : (
343
- set : SetState < Combine < PrimaryState , SecondaryState > > ,
344
- get : GetState < Combine < PrimaryState , SecondaryState > > ,
345
- api : StoreApi < Combine < PrimaryState , SecondaryState > >
346
- ) => Combine < PrimaryState , SecondaryState >
347
-
348
- export function combine <
349
- PrimaryState extends State ,
350
- SecondaryState extends State
351
- > (
352
- initialState : PrimaryState ,
353
- create : (
354
- set : SetState < PrimaryState > ,
355
- get : GetState < PrimaryState > ,
356
- api : StoreApi < PrimaryState >
357
- ) => SecondaryState
358
- ) {
359
- return (
263
+ export const combine =
264
+ < PrimaryState extends State , SecondaryState extends State > (
265
+ initialState : PrimaryState ,
266
+ create : (
267
+ // Note: NamedSet added for convenience
268
+ set : SetState < PrimaryState > & NamedSet < PrimaryState > ,
269
+ get : GetState < PrimaryState > ,
270
+ api : StoreApi < PrimaryState >
271
+ ) => SecondaryState
272
+ ) =>
273
+ (
360
274
set : SetState < Combine < PrimaryState , SecondaryState > > ,
361
275
get : GetState < Combine < PrimaryState , SecondaryState > > ,
362
276
api : StoreApi < Combine < PrimaryState , SecondaryState > >
363
277
) =>
364
- Object . assign ( { } , initialState , create ( set as any , get as any , api as any ) )
365
- }
278
+ Object . assign (
279
+ { } ,
280
+ initialState ,
281
+ create ( set as any , get as any , api as any )
282
+ ) as Combine < PrimaryState , SecondaryState >
366
283
367
284
type DeepPartial < T extends Object > = {
368
285
[ P in keyof T ] ?: DeepPartial < T [ P ] >
0 commit comments