Urgent.News

What's breaking now, across hundreds of outlets.

Tech

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

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…

Stateful Observable is a small library built on top of RxJS that eliminates the need for boilerplate code when working with loading, error, and combined data in Angular applications. The library provides a stateful observable that carries its own loading, error, and inactive state, survives errors, shares itself by default, and can be combined with other stateful observables.

Traditionally, developers have to manually manage loading flags, error fields, and combine streams, which can lead to various issues. For instance, if someone adds a second async pipe to the template, the request may go out twice. If a failure path forgets to reset the loading, the spinner will never go away. Combining streams with other observables can result in multiple loading flags and error fields to merge manually.

Stateful Observable addresses these problems by providing a unified approach to managing loading, error, and combined data. It comes with an npm package named "@rx-evo/stateful-observable," which depends only on RxJS and works in Node.js environments as well.

Using Stateful Observable simplifies the code structure and makes it easier to understand and maintain. For example, a component example provided in the article shows how to replace the boilerplate code with stateful observables. Instead of having three mutable loading subjects, five tap functions, and a manually merged combinedLoading$, the component now only has two stateful observables: clientId$ and allProducts$.

The component with Stateful Observable automatically handles loading, error, and combined data. If the bucket request fails, bucket$ ends, and populatedBucket$ ends with it. Additionally, if a new clientId is provided, it won't affect the stream since it is already dead. Error handling is also built-in, eliminating the need for catchError and error subjects per stream, as well as reducing the manual merging of combinedError$.

In summary, Stateful Observable streamlines the process of handling loading, error, and combined data in Angular applications, reducing boilerplate code and potential issues.

Rewritten by urgent.news; may contain errors — check the original.

Read the original at dev.to →

Report a problem with this story

More in Tech

Pricing fixed-price software projects with integer arithmetic

A fixed-price contract has two numbers: what the customer pays and what the developer retains. Confusing them creates a budget error before a single line of application code is written.

  • Begin with work breakdown, calculate developer's expected hours and target net amount
  • Apply 12% service fee, calculate gross amount needed to meet target net
  • Define fee contract explicitly using integer cents and percentages as basis points

Why your yt-dlp cookies stop working after a day

The fix everyone gives for yt-dlp's "Sign in to confirm you're not a bot" is to pass your YouTube cookies. It works. Then a day or two later the same error is back, with the same cookies.txt, and…

  • YouTube rotates session cookies frequently.
  • Exporting cookies from active browser results in stale session.
  • Use private/incognito window to sign in and export cookies.

WWW-Authenticate challenge order is security policy

WWW-Authenticate challenge order is security policy An HTTP 401 response tells a client that authentication is required. The WWW-Authenticate header tells it which schemes the server accepts.

  • HTTP servers' challenge order impacts security.
  • RFC 9110 mandates at least one challenge in 401 response.
  • Basic auth should be scoped, not broadly advertised.

More from Saturday 21 November →