You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sorry if the question was already asked, I really searched but did not find any answer.
Let say I have a signal store FooStore to handle model Foo, and another signal store BarStore to handle model Bar.
FooStore exposes a method createFoo (wrapped in rxMethod) that, using an httpService, makes an http call, and pipe this call to patching the state to add the newly created foo in FooStore.
BarStore exposes a method linkBarToFoo (wrapped in rxMethod) that, using an httpService, makes an http call, and pipe this call to patching the state to add the id of foo in bar's attribute called fooId
Now I would like to create a third method called createFooAndLinkItToBar within BarStore. My issue is that since createFoo is wrapped into a rxMethod, I cannot pipe it to linkBarToFoo. I thought about exposing two versions of createFoo (one wrapped into rxMethod, and the other not wrapped), but it seems off. Do you have a better idea ?
typeFoo={...}typeFooState={fooById: Record<string,Foo>};constinitialStateFoo: FooState={fooById: {}};exportconstFooStore=signalStore(withState(initialStateFoo),withComputed(/* ... */),withMethods((store,fooHttpService=inject(FooHttpService))=>({createFoo: rxMethod<Omit<Foo,"id">>(pipe(debounceTime(300),distinctUntilChanged(),tap(()=>patchState(store,{isLoading: true})),switchMap((query)=>{returnfooHttpService.createFoo(query).pipe(tapResponse({next: (foo)=>patchState(store,state=>({fooById: {...state.fooById,[foo.id]: foo}})),error: (err)=>{patchState(store,{isLoading: false});console.error(err);},}));}))),})))typeBar={id: string,fooId: string|undefined, ...}typeBarState={barById: Record<string,Bar>};constinitialStateBar: BarState={barById: {}};exportconstBarStore=signalStore(withState(initialStateBar),withComputed(/* ... */),withMethods((store,barHttpService=inject(BarHttpService),fooStore=inject(FooStore))=>{constlinkBarToFoo=rxMethod<{fooId: string,barId: string}>(pipe(debounceTime(300),distinctUntilChanged(),tap(()=>patchState(store,{isLoading: true})),switchMap((query)=>{returnbarHttpService.linkBarToFoo(query).pipe(tapResponse({next: ()=>(patchState(store,state=>{constbar=state.barById[query.barId]return{barById: {
...state.barById,[query.barId]: {...bar,fooId: query.fooId}}}})),error: (err)=>{patchState(store,{isLoading: false});console.error(err);},}));})))return{
linkBarToFoo,createFooAndLinkItToBar: rxMethod<{foo: Omit<Foo,"id">,barId: string}>(pipe(debounceTime(300),distinctUntilChanged(),tap(()=>patchState(store,{isLoading: true})),switchMap((query)=>{returnfooStore.createFoo(query.foo).pipe(// This will throw because rxMethod are not chainabletapResponse({next: (foo)=>linkBarToFoo({fooId: foo.id,barId: query.barId}),error: (err)=>{patchState(store,{isLoading: false});console.error(err);},}))}))),}}))
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello everyone,
Sorry if the question was already asked, I really searched but did not find any answer.
Let say I have a signal store
FooStore
to handle modelFoo
, and another signal storeBarStore
to handle modelBar
.FooStore
exposes a methodcreateFoo
(wrapped in rxMethod) that, using anhttpService
, makes an http call, and pipe this call to patching the state to add the newly createdfoo
in FooStore.BarStore
exposes a methodlinkBarToFoo
(wrapped inrxMethod
) that, using anhttpService
, makes an http call, and pipe this call to patching the state to add the id offoo
inbar
's attribute calledfooId
Now I would like to create a third method called
createFooAndLinkItToBar
withinBarStore
. My issue is that sincecreateFoo
is wrapped into arxMethod
, I cannot pipe it tolinkBarToFoo
. I thought about exposing two versions ofcreateFoo
(one wrapped into rxMethod, and the other not wrapped), but it seems off. Do you have a better idea ?Beta Was this translation helpful? Give feedback.
All reactions