Menu

Stateful Observable: loading, errors and combined data in RxJS without the boilerplate
📰

Stateful Observable: loading, errors and combined data in RxJS without the boilerplate

DEV Community: angular·earthdmitriy·about 6 hours ago
#ECWJ5kB3
#devtoangular#devto#dev#error#stateful
Long article (4700 words) — no AI summary yet.
Reading 0:00
15s threshold

Why I stopped writing tap(() => this.loading = true) .

Every Angular codebase I have worked on has the same few lines somewhere, usually in many places: a loading flag set from a tap , an error field set from a catchError , and a shareReplay somewhere in between.

It works for the happy path. Then someone adds a second async pipe to the template and the request goes out twice. A failure path forgets to reset loading , and the spinner never goes away. Someone combines this stream with another one, and now there are two loading flags and two error fields to merge by hand. A user logs out, and a shareReplay(1) three services away still replays the previous user's data.

When I searched GitHub for tap(() => this.loading , I got about 12 thousand files. That number has no denominator, but it does show that this pattern is everywhere.

This article is about Stateful Observable ( @rx-evo/stateful-observable on npm), a small library I built on top of RxJS to replace that boilerplate.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.