@@ -14,7 +14,7 @@ export type CallStateSlice = {
14
14
} ;
15
15
16
16
export type NamedCallStateSlice < Collection extends string > = {
17
- [ K in keyof CallStateSlice as `${Collection } ${Capitalize < K > } `] : CallStateSlice [ K ] ;
17
+ [ K in keyof CallStateSlice as Collection extends '' ? `${ Collection } ${ K } ` : `${Collection } ${Capitalize < K > } `] : CallStateSlice [ K ] ;
18
18
} ;
19
19
20
20
export type CallStateSignals = {
@@ -24,23 +24,44 @@ export type CallStateSignals = {
24
24
} ;
25
25
26
26
export type NamedCallStateSignals < Prop extends string > = {
27
- [ K in keyof CallStateSignals as `${Prop } ${Capitalize < K > } `] : CallStateSignals [ K ] ;
27
+ [ K in keyof CallStateSignals as Prop extends '' ? `${ Prop } ${ K } ` : `${Prop } ${Capitalize < K > } `] : CallStateSignals [ K ] ;
28
28
} ;
29
29
30
30
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 deriveCallStateKeys < Collection extends string > ( collection ?: Collection ) {
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
43
+ export function getCallStateKeys ( config ?: { collection ?: string } ) {
44
+ const prop = config ?. collection ;
45
+ return deriveCallStateKeys ( prop ) ;
46
+ }
47
+
48
+ export function getCollectionArray ( config : { collection ?: string } | { collections ?: string [ ] } ) {
49
+ return 'collections' in config
50
+ ? config . collections
51
+ : 'collection' in config && config . collection
52
+ ? [ config . collection ]
53
+ : undefined ;
54
+ }
55
+
56
+ export function withCallState < Collection extends string > ( config : {
57
+ collections : Collection [ ] ;
58
+ } ) : SignalStoreFeature <
59
+ EmptyFeatureResult ,
60
+ EmptyFeatureResult & {
61
+ state : NamedCallStateSlice < Collection > ;
62
+ props : NamedCallStateSignals < Collection > ;
63
+ }
64
+ > ;
44
65
export function withCallState < Collection extends string > ( config : {
45
66
collection : Collection ;
46
67
} ) : SignalStoreFeature <
@@ -56,18 +77,51 @@ export function withCallState(): SignalStoreFeature<
56
77
state : CallStateSlice ;
57
78
props : CallStateSignals ;
58
79
}
59
- > ;
60
- export function withCallState < Collection extends string > ( config ?: {
80
+ > ; export function withCallState < Collection extends string > ( config ?: {
61
81
collection : Collection ;
82
+ } | {
83
+ collections : Collection [ ] ;
62
84
} ) : SignalStoreFeature {
63
- const { callStateKey, errorKey, loadedKey, loadingKey } =
64
- getCallStateKeys ( config ) ;
65
-
66
85
return signalStoreFeature (
67
- withState ( { [ callStateKey ] : 'init' } ) ,
86
+ withState ( ( ) => {
87
+ if ( ! config ) {
88
+ return { callState : 'init' } ;
89
+ }
90
+ const collections = getCollectionArray ( config ) ;
91
+ if ( collections ) {
92
+ return collections . reduce (
93
+ ( acc , cur ) => ( {
94
+ ...acc ,
95
+ ...{ [ cur ? `${ cur } CallState` : 'callState' ] : 'init' } ,
96
+ } ) ,
97
+ { }
98
+ ) ;
99
+ }
100
+
101
+ return { callState : 'init' } ;
102
+ } ) ,
68
103
withComputed ( ( state : Record < string , Signal < unknown > > ) => {
104
+ if ( config ) {
105
+ const collections = getCollectionArray ( config ) ;
106
+ if ( collections ) {
107
+ return collections . reduce < Record < string , Signal < unknown > > > ( ( acc , cur : string ) => {
108
+ const { callStateKey, errorKey, loadedKey, loadingKey } =
109
+ deriveCallStateKeys ( cur ) ;
110
+ const callState = state [ callStateKey ] as Signal < CallState > ;
111
+ return {
112
+ ...acc ,
113
+ [ loadingKey ] : computed ( ( ) => callState ( ) === 'loading' ) ,
114
+ [ loadedKey ] : computed ( ( ) => callState ( ) === 'loaded' ) ,
115
+ [ errorKey ] : computed ( ( ) => {
116
+ const v = callState ( ) ;
117
+ return typeof v === 'object' ? v . error : null ;
118
+ } ) ,
119
+ } ;
120
+ } , { } ) ;
121
+ }
122
+ }
123
+ const { callStateKey, errorKey, loadedKey, loadingKey } = deriveCallStateKeys ( ) ;
69
124
const callState = state [ callStateKey ] as Signal < CallState > ;
70
-
71
125
return {
72
126
[ loadingKey ] : computed ( ( ) => callState ( ) === 'loading' ) ,
73
127
[ loadedKey ] : computed ( ( ) => callState ( ) === 'loaded' ) ,
0 commit comments