Have a Day at the Spa working on Your SPA with Remote Data
Tags: rxjs, javascript, angular, webdev
Published: 2022-07-20 08:09:53

---
title: Have a Day at the Spa working on Your SPA with Remote Data
published: true
date: 2022-07-20 07:51:15 UTC
tags: rxjs,javascript,angular,webdevelopment
canonical_url: https://medium.com/@dmitrief/have-a-day-at-the-spa-working-on-your-spa-with-remote-data-2fa7bfb83ed3
---

![](https://cdn-images-1.medium.com/max/1024/1*10_sVLy6NE2QVuszA0niVQ.jpeg)

#### 📑 TLDR:

1. Use the **RemoteData** data structure from the [@ngspot/remote-data](https://www.npmjs.com/package/@ngspot/remote-data) library to describe data being requested from an API.
2. Use RxJS and a custom operator trackRemoteData from the [@ngspot/remote-data-rx](https://www.npmjs.com/package/@ngspot/remote-data-rx) library for best results.

Have you ever written a component or a service where an HTTP call is made to request some data from an API and display it to the user? That’s mostly a rhetorical question — most applications do that. There are common scenarios that need to be addressed when dealing with remote data.

#### 🤔 Naive Approach Example

Many times, I’ve written components and services where HTTP calls are made to request some data. Previously, my approach used an imperative style of coding (before I learned the magical power of data streams).

{% gist https://gist.github.com/DmitryEfimenko/e1067e33e5b3b66f9a68fc525a2fa7f0 %}

Although there are many issues with the code above (like manually subscribing, not using OnPush change detection, not using trackBy for the ngFor loop, a bug due to a potential racing condition, etc.), the code above works. Please ignore the imperfections for the time being. Most of them will be taken care of by the end of the article and the rest are omitted for simplicity.

Next, I realized that the call to the API takes time and I need to display some loading template while the data is loading. So I added theisLoading property to keep track of that!

{% gist https://gist.github.com/DmitryEfimenko/70d976e4a90d3b7a24ac8a919819a249 %}

Simple enough! But wait, what if the API returns an error? I want to display something to the user in this circumstance. I know how to handle this! So I introduced another property: error!

{% gist https://gist.github.com/DmitryEfimenko/d173e608497667df4ec62d0607fe2f4d %}

Ufff, three properties to keep track of all the possible state options and a whole bunch of code to maintain these three properties?! And that’s just for one API call. What if there are multiple API calls?? What I have does not have all possible states either. There’s one more — a case when data has not been requested yet.

In the example above, data gets loaded automatically when the component initializes, but that could be different. What if you want to display a prompt to the user with some instructions for the case when data has not yet been requested? That is a lot of spaghetti code!

![](https://cdn-images-1.medium.com/max/498/0*lBdQo0Mvfsky32Vs.gif)

#### 💡 RemoteData to the Rescue!

The spaghetti code of handling all the possible states can be solved with a data structure that encapsulates all these possible cases:

{% gist https://gist.github.com/DmitryEfimenko/204c3593c6312b1b58546d31f7ea68fb %}

You can get better type safety if you create a dedicated type for each of the states and then use a [TypeScript union feature](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#union-exhaustiveness-checking).

{% gist https://gist.github.com/DmitryEfimenko/43ba2f0a83d0dfa1b49de5c81f1a472a %}

Now, I will create a few builder functions that return the RemoteData for each possible state of the request (1) not asked, (2) loading, (3) success, and (4) error.

{% gist https://gist.github.com/DmitryEfimenko/a8bd12e54308c973b5cca7eacb495fa8 %}

With all of this in place, here is the rewritten component:

{% gist https://gist.github.com/DmitryEfimenko/5adc18bfcfead709d1a8177b27e02784 %}

This is much cleaner! Only one property to maintain and this property handles all use-cases. The [@ngspot/remote-data](https://www.npmjs.com/package/@ngspot/remote-data) library has essentially been rebuilt. Feel free to use it!

But I can do better! Read on.

#### 💪 Using the power of RxJS

Remember the numerous issues mentioned at the beginning of the article?

Among them is a bug related to a racing condition. If a user presses the button “Load products” many times quickly, then many requests will be fired up. The chances are, that due to the timing of the network, the responses for these requests will be returned out of order. The response for the request associated with the very first click could end up coming back last. This means that the UI might not display the freshest data.

RxJS is perfect for handling asynchronous data streams. It has mechanisms for dealing with this sort of situation. In addition, it makes it easy to use OnPush change detection, which improves the performance of your application and can [improve the general quality of your components](https://youtu.be/tWy8zaWvkvk).

Without further ado, here is the rewritten component using reactive streams and the RemoteData data structure.

{% gist https://gist.github.com/DmitryEfimenko/957921c34e69c611e364093ced91515d %}

This solution is much more robust. There are no manual subscriptions. Data is propagated to the template via reactive streams with theasync pipe, which allows the use of OnPush change detection. Finally, race conditions are handled via the switchMap operator, which automatically cancels any previous requests in flight and starts a new one.

RxJS allows building a custom operator using multiple existing operators. That’s what I had done with the example above — I took the operators used to handle the RemoteData loading, success, and error cases and extracted these operators into a custom operator called trackRemoteData. Find the trackRemoteDataoperator under [@ngspot/remote-data-rx](https://www.npmjs.com/package/@ngspot/remote-data-rx) library. There are a couple more bells and whistles built-in.

With that, the code becomes even simpler.

{% gist https://gist.github.com/DmitryEfimenko/2423000b4769a30e1596b5773552550d %}

#### 🧡 Credit Where Credit is Due

There are similar solutions for handling remote data out there. I have tried most of them, but none provided me with the exact feature-set I wanted. Here are a few of them:

- Where it all began: “[How Elm Slays a UI Antipattern](http://blog.jenkster.com/2016/06/how-elm-slays-a-ui-antipattern.html)”
- [https://www.npmjs.com/package/ngx-remotedata](https://www.npmjs.com/package/ngx-remotedata)
- [https://github.com/daiscog/ngx-http-request-state](https://github.com/daiscog/ngx-http-request-state)

Nonetheless, these solutions inspired me to create the two libraries that I now use in most of my projects. I hope you find them helpful too.

I wish you happy programming!

**👏 Special thanks to** [**Ana Boca**](https://github.com/anaboca) **for reviewing this article.**