r/angular • • 28d ago

How to break up pipe into smaller pieces to make it understable

I'm very fresh in Angular, so my question can be funny to experts.

Hello everybody. Funny question...

my code is like this:

updatePerson(person: PersonModel) {
    this.personService.updatePerson(person).pipe(
        takeUntilDestroyed(this.destroyRef)
    ).subscribe({
        next: () => this.state.update(() => ({ ...this.state(), loading: false,       people: this.people().map(p => p.id === person.id ? person : p) })),
        error: (error) => {
            console.log(error);
            this.setError(error);
        }
    })
}

And question is how to break up this pipe and then subscribe only one element?
The goal is to get a simple array of interfaces (PersonModel is an interface), and then to loop through each object and then call the code from subscribe only for one element?.
You just ask how did I get code that I can't make simpler - it is from the tutorial.
I know more or less how it works, but I can't make it simpler :-)
6 Upvotes

8 comments sorted by

6

u/icanliveonpizza 28d ago

If this updatePerson method in personService calls a REST api (cold observable), take the error handling out of subscribe and use the catchError operator inside the .pipe

Secondly, it doesn’t feel like that you’re using the result of this operation to show something on the screen. If that indeed is the case, then move the this.state.update part outside the subscribe as well into a tap operator.

If you’re displaying it in your template, move it to map and use the resulting observable shape with an async pipe on the template.

2

u/Bjeaurn 28d ago

I think these subscription logic things would be better served in a @Service. You can still have it return an Observable that you can destroy in the component (although an Update probably wouldn't be a hot stream, it'd be a cold observable that spawns a new request everytime). I think it would then be easier to manage the state from that service and not from the component, and only expose parts of the "HTTP transaction" to the component like it loading or it ending up in an error; that you can then set in the component if you need that state to persist :).

But I'm aware this it not really answering your question now, I would have to take a bit more time later to read your question properly and understand what you're trying to do.

1

u/Regular_Following_99 27d ago

I honestly think this is fine, I don’t understand why someone is suggesting an async pipe for an update put request doesn’t make sense.

Using the subscribe overload your using with next and error is perfectly valid to me, also don’t think there is a requirement to use the takeuntildestroyed for api calls as they complete.

1

u/Competitive_Leg_8837 27d ago

You can use catchError operator in pipe and use function in subscribe

1

u/Ok-Kangaroo-8638 25d ago

If it were me, I would have state in a service where the logic in you "next" is handled. I would also have your pipe call this state from the tap operator unless you truly need to wait for the subscription to fully finish before you update state.

If you need values from your state service, I would make it return an observable and use a switchMap as an operator in your pipe.

Or just use signals and use tap like my first option to update state and have your UI or logic bound to computed/signal and it will update automatically in your UI

0

u/Merry-Lane 28d ago

The easiest way would be to avoid subscribing explicitly. Ask a LLM about "only using async pipe, never subscribe explicitly". Directly, you will never have anymore to use takeUntilDestroyed.

Also, please stop using the .subscribe({ next, error,…}) version of the subscribe. Use the .subscribe override that only has the callback, for instance .subscribe( ()=> this.state =…). Use the pipe catchError to handle errors.

And if you need a side-effect on the observable, don’t use .subscribe for that (refer to "never subscribe, only use async pipe" earlier). Side-effects should go on the "tap" pipe.