RxJS in Angular – Antipattern 2 – Stateful Streams

This is the second part of a small series, in which I want to share some pitfalls we discovered multiple times in code reviews over the years, as well a few patterns we identified as helpful. In the first part we discussed how nesting subscriptions can be harmful.

In diesem Artikel:

yb
Yannick Baron ist Architekturberater bei Thinktecture mit einem Fokus auf Angular und RxJS.

Stateful Streams in Use

Often when modeling our streams with RxJS, we end up in a situation where we need to switch our streams but need to keep their results together. Naturally, the need to store the intermediate result emerges. A common attempt to solve this problem we encountered numerous times would be to store these intermediate results outside of our stream. In the case of using Angular, the destination often tends to be the component.

				
					this.route.params.pipe(
  switchMap(({ customerId }) => customerService.getCustomer(customerId)),
  tap((customer) => { 
    this.customer = customer;
    this.code = makeCode(customer);
  }),
  switchMap(() => myService.retrieveByCode(this.code)),
  tap((result) => { this.result = result; }),
  switchMap(() => otherService.byCustomerAndResult(this.customer, this.result)),
).subscribe(combinedResult => {
  this.result = combinedResult;
  this.view = moreComplexComutation(this.customer, this.code, combinedresult);
});

				
			

In the above implementation we react to the change of a route param customerId. Once it changes, we switchMap to request an customer object from our API. In the following tap, we store the intermediate results on our component. We then switchMap to hit our API again using the previously stored this.code and use the following tap to once again store this intermediate result on our component. The next switchMap also requests further information from our API and finally all intermediate results are combined and the view is updated in our subscribe.

The Bad

At every step of the pipeline we are relying on state that is kept outside of our stream. This can introduce side-effects and we cannot argue about our data flow easily, as we cannot know at what point in time which property will be updated, thus making our stream rely on state.

Imagine there is some effect or action outside of the stream mutating these values. It is not obvious to the developer looking at the code that there might actually be something tampering with the state while the stream is executing.

What if one of the requests fails? Then our state will go out of sync and maybe even produce erroneous results. In order to prevent this, proper error handling becomes a hassle, as we have to handle every request separately and perform the appropriate cleanup.

The Fix

The need for state arose from the need to store intermediate results that can be picked up at a later stage of our pipeline. We now want to introduce a way to model our streams that alleviates the need to store results outside of the stream and rather carry them along through our pipeline.

				
					// Emitting customerIds 1 and 2 with a 25ms delay
createStream<number>([1, 2], 25)
  .pipe(
    // Switch to request customer object
    switchMap(id => requestCustomer(id)),
    // Switch to request made by generating the customer code
    switchMap((customer) => {
      const code = makeCode(customer);

      return requestByCode(code)
        // Combine results into a bundle
        .pipe(map(result => ({ customer, code, result })));
    }),
    // Switch to final request
    switchMap(({ customer, result, ...bundle }) => {
      return requestByCustomerAndResult(customer, result)
        // Add the new result to the bundle
        .pipe(map(combinedResult => ({ ...bundle, customer, result, combinedResult })));
    }),
  )
  .subscribe(({ customer, code, result, combinedResult }) => {
    // Update view or properties all at once
    updateView('customer', customer.name);
    updateView('code', code);
    updateView('result', result.result);
    updateView('combined', combinedResult);
  });

				
			

In the snippet above, we react to our stream emitting customer ids. We then switchMap to request the corresponding customer object. Once retrieved, we compute the code and switchMap to request more data from our API. At this point we use map to transform the response to a bundle containing the customer, our code and the result of our request. From this bundle, we use what we need (customer and result) to finally switchMap to our final request. Once retrieved, we once again combine all of our results into a bundle. Finally, we have all the data we need to update our view accordingly in a single step, making sure nothing can go out of sync.

Please note how the use of destructuring helps us accessing the parts of our bundles easily.

You can find a running example on stackblitz.

Conclusion

In this post we have discussed how keeping state outside of our stream can potentially introduce unforeseen side-effects. We introduced a simple way to remove the need for storing intermediate results, which keeps our data flow simple and once again makes it easier to argue about how our data flows. This also increases the readability for fellow developers as they just have to follow the pipeline and not worry about a state that could potentially be manipulated from outside of the stream.

In the next part of the series we show how employing the techniques introduced can make working with RxJS in Angular applications easier for us.

Kostenloser
Newsletter

Aktuelle Artikel, Screencasts, Webinare und Interviews unserer Experten für Sie

Verpassen Sie keine Inhalte zu Angular, .NET Core, Blazor, Azure und Kubernetes und melden Sie sich zu unserem kostenlosen monatlichen Dev-Newsletter an.

Newsletter Anmeldung
Diese Artikel könnten Sie interessieren
Low-angle photography of metal structure
AI
cl-neu

AI-Funktionen zu Angular-Apps hinzufügen: lokal und offlinefähig

Künstliche Intelligenz (KI) ist spätestens seit der Veröffentlichung von ChatGPT in aller Munde. Wit WebLLM können Sie einen KI-Chatbot in Ihre eigenen Angular-Anwendungen integrieren. Wie das funktioniert und welche Vor- und Nachteile WebLLM hat, lesen Sie hier.
26.02.2024
Angular
SL-rund

Konfiguration von Lazy Loaded Angular Modulen

Die Konfigurierbarkeit unserer Angular-Module ist für den Aufbau einer wiederverwendbaren Architektur unerlässlich. Aber in der jüngsten Vergangenheit hat uns Angular seine neue modullose Zukunft präsentiert. Wie sieht das Ganze jetzt aus? Wie konfigurieren wir jetzt unsere Lazy-Komponenten? Lasst uns gemeinsam einen Blick darauf werfen.
03.08.2023
Angular
yb

Using EntityAdapter with ComponentStore: @ngrx/entity Series – Part 3

As someone who enjoys the ComponentStore on an average level, I have written simple reactive CRUD logic several times. While storing a vast number of entities in the component state might not be a frequent use case, I will briefly illustrate the usage of the EntityAdapter with the @ngrx/component-store.
14.02.2023
Angular
yb

Multiple Entity Collections in the Same Feature State: @ngrx/entity-Series – Part 2

After introducing the @ngrx/entity package, I am often asked how to manage multiple entity types in the same feature state. While I hope that the previous part of this article series has made this more apparent, I will further focus on this question in the following.
07.02.2023
Angular
yb

Managing Your Collections With the EntityAdapter: @ngrx/entity-Series – Part 1

This three-part series of blogposts is targeted at developers who have already gained experience with NgRx but still manage their collections themselves. In the first part I introduce the Entity Adapter, in the second part I show you how to connect it to NgRx and in the third part how to do it with the Component Store as well.
31.01.2023
Angular
MS-rund

Implementing Smart and Presentational Components with Angular: Condensed Angular Experiences – Part 4

In this article, we will explore how to apply the concept of smart and presentational components with Angular. We will choose a complex-enough target to see all aspects in action, yet understandable and within the scope of this article. The goal is to teach you how to use this architecture in your way. For that, we will iterate through different development stages, starting with the target selection and implementing it in a naive way. After the first development, we will refactor that naive solution into smart and presentational components that are reusable, refactor-friendly, and testable.
23.01.2023