npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ngspot/remote-data-rx

v1.1.2

Published

[![MIT](https://img.shields.io/packagist/l/doctrine/orm.svg?style=flat-square)]() [![commitizen](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square)]() [![PRs](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=

Downloads

61

Readme

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.

Features

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

Installation

NPM

npm install @ngspot/remote-data-rx

Yarn

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.

Configuration

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$) =>
    data$.pipe(
      mergeMap((data) =>
        this.http
          .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
        .get(`/api/user/${userId}`)
        .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:

  • https://github.com/daiscog/ngx-http-request-state

License

MIT © Dmitry Efimenko