Repository navigation
Replies: 1 comment
|
Exposing two versions isn't off. That split is what makes this composable. export const FooStore = signalStore(
{ providedIn: 'root' },
withState({ fooById: {} as Record<string, Foo>, isLoading: false }),
withMethods((store, http = inject(FooHttpService)) => {
// Composable: returns the created Foo and updates this store on the way through.
const createFoo$ = (foo: Omit<Foo, 'id'>): Observable<Foo> =>
http.createFoo(foo).pipe(
tap((created) =>
patchState(store, (s) => ({ fooById: { ...s.fooById, [created.id]: created } })),
),
);
return {
createFoo$,
// Fire-and-forget entry point.
createFoo: rxMethod<Omit<Foo, 'id'>>(
pipe(concatMap((foo) => createFoo$(foo).pipe(catchError(() => EMPTY)))),
),
};
}),
);
export const BarStore = signalStore(
{ providedIn: 'root' },
withState({ barById: {} as Record<string, Bar> }),
withMethods((store, http = inject(BarHttpService), fooStore = inject(FooStore)) => {
const linkBarToFoo$ = (q: { fooId: string; barId: string }) =>
http.linkBarToFoo(q).pipe(
tap(() =>
patchState(store, (s) => ({
barById: { ...s.barById, [q.barId]: { ...s.barById[q.barId], fooId: q.fooId } },
})),
),
);
return {
linkBarToFoo$,
createFooAndLinkItToBar: rxMethod<{ foo: Omit<Foo, 'id'>; barId: string }>(
pipe(
concatMap(({ foo, barId }) =>
fooStore.createFoo$(foo).pipe(
switchMap((created) => linkBarToFoo$({ fooId: created.id, barId })),
catchError(() => EMPTY),
),
),
),
),
};
}),
);Each store still owns its own state updates. Two things in the original pipelines I'd change for writes:
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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
FooStoreto handle modelFoo, and another signal storeBarStoreto handle modelBar.FooStoreexposes a methodcreateFoo(wrapped in rxMethod) that, using anhttpService, makes an http call, and pipe this call to patching the state to add the newly createdfooin FooStore.BarStoreexposes a methodlinkBarToFoo(wrapped inrxMethod) that, using anhttpService, makes an http call, and pipe this call to patching the state to add the id offooinbar's attribute calledfooIdNow I would like to create a third method called
createFooAndLinkItToBarwithinBarStore. My issue is that sincecreateFoois 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 ?All reactions