TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

Remote Data RX

MIT commitizen PRs styled with prettier

An RxJS extension library for tracking observables (such as HTTP requests) with loading and error information via RemoteData data structure.


  • Simple syntax that reduces boilerplate
  • Configurable for many scenarios
  • Compatible with any framework



npm install @ngspot/remote-data-rx


yarn add @ngspot/remote-data-rx

The RemoteData data structure

See @ngspot/remote-data

trackRemoteData RxJS Operator

Using RemoteData state builder functions is better than composing the object by hand, but there is an even better way if using observables. Use the trackRemoteData operator:

import { trackRemoteData } from '@ngspot/remote-data-rx';

const data$ = api.loadMyDataObservable().pipe(trackRemoteData());

The resulting observable will emit an object with appropriate properties set to the correct values depending on state of the request observable.


In most scenarios using the trackRemoteData() operator is sufficient without any configuration. However, the following configuration object is available to pass in to customize behavior:

export interface TrackRemoteDataOpts<T, E = Error> {
   * A subject to which the remote data will be tracked.
  subject?: Subject<RemoteData<T, E>>;

   * Whether to keep the previous value during loading.
  keepPreviousValue?: PreviousValueCache<T>;

option: "subject":

Imagine having a side-effect in the application, which is in charge of posting data to the server. In this case, there is no way to assign the result of the post to an observable. So instead, developers tell the trackRemoteData operator to push results into a different subject. For convenience's sake, this library provides a tiny function to generate such subject, which is a BehaviorSubject initialized with notAskedState. The function is called trackingRemoteDataSubject().

import { trackRemoteData } from '@ngspot/remote-data-rx';

class MyComponent {
  savingState$ = trackingRemoteDataSubject<MyData>();

  saveData = this.createEffect<MyData>((data$) =>
      mergeMap((data) =>
          .post('/api/data', { data })
          .pipe(trackRemoteData({ subject: this.savingState$ }))

Now developers can subscribe to the savingState$ in the template.

Notice, that under the hood, the "complete" event is not passed into the provided subject. This way saveData() method can be called many times and the provided Subject won't be completed.

option "keepPreviousValue"

By default, when the trackRemoteData() operator is used, it will always set value property of the RemoteData to undefined when the state is "loading". However, this may not be the desired behavior. Perhaps, instead of just displaying the loading indicator the application requires to keep the previously loaded data shown while graying the data out. The keepPreviousValue is exactly for this use-case.

Imagine loading data based on the change of the userId$ observable:

import { trackRemoteData, PreviousValueCache } from '@ngspot/remote-data-rx';

class MyComponent {
  private keepPreviousValue = new PreviousValueCache();

  remoteData$ = this.userId$.pipe(
    switchMap((userId) => {
      return this.http
        .pipe(trackRemoteData({ keepPreviousValue: this.keepPreviousValue }));

The PreviousValueCache is storage to provide so that the trackRemoteData operator has a place to store the previous value between each new request.

Previous Art

The library is heavily inspired by:


MIT © Dmitry Efimenko


npm i @ngspot/remote-data-rx

DownloadsWeekly Downloads






Unpacked Size

11.7 kB

Total Files


Last publish


  • dmitryefimenko