@@ -31,18 +31,17 @@ export type SetCallState<Prop extends string | undefined> = Prop extends string
3131 ? NamedCallStateSlice < Prop >
3232 : CallStateSlice ;
3333
34- export function getCallStateKeys ( config ?: { collection ?: string } ) {
35- const prop = config ?. collection ;
34+ export function getCallStateKeys ( collection ?: string ) {
3635 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' ,
4140 } ;
4241}
4342
4443export function withCallState < Collection extends string > ( config : {
45- collection : Collection ;
44+ collections : Collection [ ] ;
4645} ) : SignalStoreFeature <
4746 EmptyFeatureResult ,
4847 EmptyFeatureResult & {
@@ -58,14 +57,39 @@ export function withCallState(): SignalStoreFeature<
5857 }
5958> ;
6059export function withCallState < Collection extends string > ( config ?: {
61- collection : Collection ;
60+ collections : Collection [ ] ;
6261} ) : SignalStoreFeature {
63- const { callStateKey, errorKey, loadedKey, loadingKey } =
64- getCallStateKeys ( config ) ;
65-
6662 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+ ) ,
6874 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+
81+ return {
82+ ...acc ,
83+ [ loadingKey ] : computed ( ( ) => callState ( ) === 'loading' ) ,
84+ [ loadedKey ] : computed ( ( ) => callState ( ) === 'loaded' ) ,
85+ [ errorKey ] : computed ( ( ) => {
86+ const v = callState ( ) ;
87+ return typeof v === 'object' ? v . error : null ;
88+ } ) ,
89+ } ;
90+ } , { } ) ;
91+ }
92+ const { callStateKey, errorKey, loadedKey, loadingKey } = getCallStateKeys ( ) ;
6993 const callState = state [ callStateKey ] as Signal < CallState > ;
7094
7195 return {
0 commit comments