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.…