Jargo
Jargo

Reputation: 103

ngrx SignalStore: Calling a service function without parameters, which returns an Observable

I have an Angular Service which lets me load data using HttpClient.request(...) and returns the data as an Observable. Within my ngrx SignalStore I call these service methods within rxMethod()s.

One of my service methods does not have any parameters. So calling it within an rxMethod() is not an option, because rxMethod() requires a parameter, be it

a static value, signal, or observable as an input argument

So this won't work, because I do not have a parameter to call load() with:

withMethods((
        store,
        service = inject(SomeService),
    ) => ({
        load: rxMethod(pipe(
            switchMap(() => service.getAll$()
                .pipe(
                    tap(models => {
                        store.initWithEntities(models);
                    }),
                ),
            ),
        )),
    }),
),

What is the best practice in this case? I'd rather not use a normal SignalStore method, because I'd like to not have to bother with subscription management.

Upvotes: 4

Views: 1102

Answers (1)

Jargo
Jargo

Reputation: 103

Apparently, passing void as the rxMethods type argument works. However, as in my case, eslint might not be happy with it.

So this would work:

withMethods((
        store,
        service = inject(SomeService),
    ) => ({
        load: rxMethod<void>(pipe(
            switchMap(() => service.getAll$()
                .pipe(
                    tap(models => {
                        store.initWithEntities(models);
                    }),
                ),
            ),
        )),
    }),
),

Which can then be called without a parameter, like so: store.load()

Upvotes: 3

Related Questions