@@ -42,7 +42,7 @@ export function getCallStateKeys(config?: { collection?: string }) {
42
42
}
43
43
44
44
export function withCallState < Collection extends string > ( config : {
45
- collection : Collection ;
45
+ collection : Collection | Collection [ ] ;
46
46
} ) : SignalStoreFeature <
47
47
EmptyFeatureResult ,
48
48
EmptyFeatureResult & {
@@ -58,51 +58,61 @@ export function withCallState(): SignalStoreFeature<
58
58
}
59
59
> ;
60
60
export function withCallState < Collection extends string > ( config ?: {
61
- collection : Collection ;
61
+ collection : Collection | Collection [ ] ;
62
62
} ) : SignalStoreFeature {
63
- const { callStateKey, errorKey, loadedKey, loadingKey } =
64
- getCallStateKeys ( config ) ;
63
+ const collections = Array . isArray ( config ?. collection ) ? config . collection : [ config ?. collection ] ;
64
+ const keys = collections . reduce ( ( acc , collection ) => {
65
+ const { callStateKey, errorKey, loadedKey, loadingKey } = getCallStateKeys ( { collection } ) ;
66
+ acc . callStateKeys . push ( callStateKey ) ;
67
+ acc . errorKeys . push ( errorKey ) ;
68
+ acc . loadedKeys . push ( loadedKey ) ;
69
+ acc . loadingKeys . push ( loadingKey ) ;
70
+ return acc ;
71
+ } , { callStateKeys : [ ] , errorKeys : [ ] , loadedKeys : [ ] , loadingKeys : [ ] } ) ;
65
72
66
73
return signalStoreFeature (
67
- withState ( { [ callStateKey ] : 'init' } ) ,
74
+ withState ( keys . callStateKeys . reduce ( ( acc , key ) => {
75
+ acc [ key ] = 'init' ;
76
+ return acc ;
77
+ } , { } ) ) ,
68
78
withComputed ( ( state : Record < string , Signal < unknown > > ) => {
69
- const callState = state [ callStateKey ] as Signal < CallState > ;
70
-
71
- return {
72
- [ loadingKey ] : computed ( ( ) => callState ( ) === 'loading' ) ,
73
- [ loadedKey ] : computed ( ( ) => callState ( ) === 'loaded' ) ,
74
- [ errorKey ] : computed ( ( ) => {
79
+ return keys . callStateKeys . reduce ( ( acc , callStateKey , index ) => {
80
+ const callState = state [ callStateKey ] as Signal < CallState > ;
81
+ acc [ keys . loadingKeys [ index ] ] = computed ( ( ) => callState ( ) === 'loading' ) ;
82
+ acc [ keys . loadedKeys [ index ] ] = computed ( ( ) => callState ( ) === 'loaded' ) ;
83
+ acc [ keys . errorKeys [ index ] ] = computed ( ( ) => {
75
84
const v = callState ( ) ;
76
85
return typeof v === 'object' ? v . error : null ;
77
- } ) ,
78
- } ;
86
+ } ) ;
87
+ return acc ;
88
+ } , { } ) ;
79
89
} )
80
90
) ;
81
91
}
82
92
83
- export function setLoading < Prop extends string | undefined = undefined > (
84
- prop ? : Prop
93
+ export function setLoading < Prop extends string > (
94
+ prop : Prop | Prop [ ]
85
95
) : SetCallState < Prop > {
86
- if ( prop ) {
87
- return { [ ` ${ prop } CallState` ] : 'loading' } as SetCallState < Prop > ;
88
- }
89
-
90
- return { callState : 'loading' } as SetCallState < Prop > ;
96
+ const props = Array . isArray ( prop ) ? prop : [ prop ] ;
97
+ return props . reduce ( ( acc , p ) => {
98
+ acc [ ` ${ p } CallState` ] = 'loading' ;
99
+ return acc ;
100
+ } , { } as SetCallState < Prop > ) ;
91
101
}
92
102
93
- export function setLoaded < Prop extends string | undefined = undefined > (
94
- prop ? : Prop
103
+ export function setLoaded < Prop extends string > (
104
+ prop : Prop | Prop [ ]
95
105
) : SetCallState < Prop > {
96
- if ( prop ) {
97
- return { [ ` ${ prop } CallState` ] : 'loaded' } as SetCallState < Prop > ;
98
- } else {
99
- return { callState : 'loaded' } as SetCallState < Prop > ;
100
- }
106
+ const props = Array . isArray ( prop ) ? prop : [ prop ] ;
107
+ return props . reduce ( ( acc , p ) => {
108
+ acc [ ` ${ p } CallState` ] = 'loaded' ;
109
+ return acc ;
110
+ } , { } as SetCallState < Prop > ) ;
101
111
}
102
112
103
- export function setError < Prop extends string | undefined = undefined > (
113
+ export function setError < Prop extends string > (
104
114
error : unknown ,
105
- prop ? : Prop
115
+ prop : Prop | Prop [ ]
106
116
) : SetCallState < Prop > {
107
117
let errorMessage : string ;
108
118
@@ -114,11 +124,9 @@ export function setError<Prop extends string | undefined = undefined>(
114
124
errorMessage = String ( error ) ;
115
125
}
116
126
117
- if ( prop ) {
118
- return {
119
- [ `${ prop } CallState` ] : { error : errorMessage } ,
120
- } as SetCallState < Prop > ;
121
- } else {
122
- return { callState : { error : errorMessage } } as SetCallState < Prop > ;
123
- }
127
+ const props = Array . isArray ( prop ) ? prop : [ prop ] ;
128
+ return props . reduce ( ( acc , p ) => {
129
+ acc [ `${ p } CallState` ] = { error : errorMessage } ;
130
+ return acc ;
131
+ } , { } as SetCallState < Prop > ) ;
124
132
}
0 commit comments