@snowcodes/nanostores-rxjs
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@snowcodes/nanostores-rxjs

Add reactivity to nanostores.

With @snowcodes/nanostores-rxjs, add rxjs in the game.
You can now use the power of observable with nanostores.

How to use it ?

We expose to simple functions : listenStore and subscribeStore.

Basic usage

You can listen your store with listenStore.

Important note: listenStore use listen from nanostores.
First value will be read not immediately.

import { atom } from 'nanostores';
import { listenStore } from '@snowcodes/nanostores-rxjs';
import { map } from 'rxjs/operators';

const counterState = atom<number>(0);

listenStore(counterState)
  .pipe(map((value) => doSomething(value)))
  .subscribe((afterDoSomething) => {
    // ...
  });

You can also use subscribeStore.

Important note: subscribeStore use subscribe from nanostores.
First value will be read immediately.

import { atom } from 'nanostores';
import { subscribeStore } from '@snowcodes/nanostores-rxjs';
import { map } from 'rxjs/operators';

const counterState = atom<number>(0);

subscribeStore(counterState)
  .pipe(map((value) => doSomething(value)))
  .subscribe((afterDoSomething) => {
    // ... Read immediately
  });

Advanced usage

You can select or transform data directly.

listenStore or subscribeStore accept a second parameter. Follow the example :

import { map } from 'nanostores';
import { subscribeStore, listenStore } from '@snowcodes/nanostores-rxjs';
import { Observable } from 'rxjs';

type ApplicationState = {
  loading: boolean;
};

const applicationState = map<ApplicationState>({ loading: false });

// 1. Create a selector or a transformation or what you waht
const selectLoading = (state: ApplicationState) => state.loading;

// 2. Use it as second parameter
const isLoadingFromListen$: Observable<boolean> = listenStore(applicationState, selectLoading);
const isLoadingFromSubscribe$: Observable<boolean> = subscribeStore(applicationState, selectLoading);

Package Sidebar

Install

npm i @snowcodes/nanostores-rxjs

Weekly Downloads

0

Version

1.0.1

License

none

Unpacked Size

5.6 kB

Total Files

9

Last publish

Collaborators

  • ruizalexandre