@@ -31,18 +31,17 @@ export type SetCallState<Prop extends string | undefined> = Prop extends string
31
31
? NamedCallStateSlice < Prop >
32
32
: CallStateSlice ;
33
33
34
- export function getCallStateKeys ( config ?: { collection ?: string } ) {
35
- const prop = config ?. collection ;
34
+ export function getCallStateKeys ( collection ?: string ) {
36
35
return {
37
- callStateKey : prop ? `${ config . collection } CallState` : 'callState' ,
38
- loadingKey : prop ? `${ config . collection } Loading` : 'loading' ,
39
- loadedKey : prop ? `${ config . collection } Loaded` : 'loaded' ,
40
- errorKey : prop ? `${ config . collection } Error` : 'error' ,
36
+ callStateKey : collection ? `${ collection } CallState` : 'callState' ,
37
+ loadingKey : collection ? `${ collection } Loading` : 'loading' ,
38
+ loadedKey : collection ? `${ collection } Loaded` : 'loaded' ,
39
+ errorKey : collection ? `${ collection } Error` : 'error' ,
41
40
} ;
42
41
}
43
42
44
43
export function withCallState < Collection extends string > ( config : {
45
- collection : Collection ;
44
+ collections : Collection [ ] ;
46
45
} ) : SignalStoreFeature <
47
46
EmptyFeatureResult ,
48
47
EmptyFeatureResult & {
@@ -58,16 +57,39 @@ export function withCallState(): SignalStoreFeature<
58
57
}
59
58
> ;
60
59
export function withCallState < Collection extends string > ( config ?: {
61
- collection : Collection ;
60
+ collections : Collection [ ] ;
62
61
} ) : SignalStoreFeature {
63
- const { callStateKey, errorKey, loadedKey, loadingKey } =
64
- getCallStateKeys ( config ) ;
65
-
66
62
return signalStoreFeature (
67
- withState ( { [ callStateKey ] : 'init' } ) ,
63
+ withState (
64
+ config
65
+ ? config . collections . reduce (
66
+ ( acc , cur ) => ( {
67
+ ...acc ,
68
+ ...{ [ cur ? `${ cur } CallState` : 'callState' ] : 'init' } ,
69
+ } ) ,
70
+ { }
71
+ )
72
+ : { callStateKey : 'init' }
73
+ ) ,
68
74
withComputed ( ( state : Record < string , Signal < unknown > > ) => {
75
+ if ( config ) {
76
+ return config . collections . reduce < Record < string , Signal < unknown > > > ( ( acc , cur : string ) => {
77
+ const { callStateKey, errorKey, loadedKey, loadingKey } =
78
+ getCallStateKeys ( cur ) ;
79
+ const callState = state [ callStateKey ] as Signal < CallState > ;
80
+ return {
81
+ ...acc ,
82
+ [ loadingKey ] : computed ( ( ) => callState ( ) === 'loading' ) ,
83
+ [ loadedKey ] : computed ( ( ) => callState ( ) === 'loaded' ) ,
84
+ [ errorKey ] : computed ( ( ) => {
85
+ const v = callState ( ) ;
86
+ return typeof v === 'object' ? v . error : null ;
87
+ } ) ,
88
+ } ;
89
+ } , { } ) ;
90
+ }
91
+ const { callStateKey, errorKey, loadedKey, loadingKey } = getCallStateKeys ( ) ;
69
92
const callState = state [ callStateKey ] as Signal < CallState > ;
70
-
71
93
return {
72
94
[ loadingKey ] : computed ( ( ) => callState ( ) === 'loading' ) ,
73
95
[ loadedKey ] : computed ( ( ) => callState ( ) === 'loaded' ) ,
0 commit comments