GithubHelp home page GithubHelp logo

nerdwalletoss / epic-state-subscriptions Goto Github PK

View Code? Open in Web Editor NEW
2.0 4.0 0.0 191 KB

Epicly perform actions and side effects when *.the.paths.you.care.about change in the Redux store.

License: Apache License 2.0

JavaScript 100.00%
redux epics rxjs redux-observable

epic-state-subscriptions's Introduction

npm version

Epic State Subscriptions

Epicly perform actions and side effects when *.the.paths.you.care.about change in the Redux store.

If you have not used Redux-Observable epics before, here's a link to the documentation.

Installation

There is an additional peer dependency of redux-observable@^1.1.x

npm i @nerdwallet/epic-state-subscriptions

Basic Usage

Import the createStateSubscription operator and add it to your Epic like any other operator. Pass it the paths that you want to subscribe to in the Redux store and it will transform the action stream into a stream of path changes.

import { map } from 'rxjs/operators';
import { createStateSubscription } from '@nerdwallet/epic-state-subscriptions';

const exampleEpic = (action$, state$) =>
  action$.pipe(
    createStateSubscription(state$, {
      paths: ['x.y.z', 'a.b.*', '*.c.d'],
    }),
    map(paths => {
      paths.forEach({ pathPattern, path } => {
        console.log(`path ${path} has been reported to change because of matched pattern ${pathPattern}`);
      });
      return sideEffectAction(changeSet);
    })
  );

The path changes are emitted as objects with the structure below:

Key Type Description
path String The path that changed in the Redux store
pathPattern String The state subscription path pattern that triggered the path to change
prevState Any The previous state of the path that changed in the Redux store
nextState Any The new state of the path that changed in the Redux store

In the above example, if path a.b.c had changed from false to true the path object emitted would be { prevState: false, nextState: true, path: 'a.b.c', pathPattern: 'a.b.*' }.

Note: Since operators do not normally get access to the state$ stream, it is passed explicitly as the first argument, followed by the configuration options.

Configuration Options

Option Type Required Default Description
key String false Random uuid Optional key name to identify the subscription
paths Array false [ ] The . delimited initial paths to watch in the Redux store with support for wildcards such as store.*.y

Advanced Subscriptions

As a standard RxJS operator, your epic can chain createStateSubscription to support additional use cases like buffering path changes:

import { bufferTime, filter, map } from 'rxjs/operators';
import { createStateSubscription } from '@nerdwallet/epic-state-subscriptions';

const exampleEpic (action$, state$) =>
  action$.pipe(
    // Buffer the actions changes since they are frequent
    bufferTime(500),
    // Only emit updates to the state subscription if actions
    // have occurred in the buffer interval
    filter(actions => actions.length > 0),
    createStateSubscription(state$, {
      paths: ['a.b.c'],
    }),
    map(paths => {
      paths.forEach({ prevState, nextState, pathPattern, path } => {
        console.log(`path ${path} has been reported to change because of matched pattern ${pathPattern}`);
      });
      return sideEffectAction(changeSet);
    })
  );

Dynamic State Subscription Paths

If a config will need dynamic state subscription paths as the application runs, there is a provided state subscription reducer and action for overriding the default paths initialized in the config which you can use.

Include the state subscriptions reducer in your combineReducers redux configuration:

import { stateSubscriptionReducer } from '@nerdwallet/epic-state-subscriptions';
import { combineReducers } from 'redux';

const rootReducer = combineReducers([...reducers, stateSubscriptionReducer]);

and then you can dispatch the overrideStateSubscriptionPaths action:

import { overrideStateSubscriptionPaths } from '@nerdwallet/epic-state-subscriptions';

dispatch(overrideStateSubscriptionPaths({ key: 'exampleKey', paths: ['state.x.y'] });

The state subscription key passed in the action should match the key in the createStateSubscription config. It automatically will now favour configurations in the reducer at that subscription key over the static initial paths specified in the config.

FAQs

  1. My state subscription just needs to perform side effects and not fire any actions, how do I terminate my Epic?

You can use the tap operator to perform side effects and use the ignoreElements operator to instruct the stream to not emit elements and fire a termination event:

import { ignoreElements, tap } from 'rxjs/operators';
import { createStateSubscription } from 'epic-state-subscriptions';
import { sideEffectAction } from './actions';

const exampleEpic = (action$, state$) =>
  action$.pipe(
    createStateSubscription(state$, {
      paths: ['x.y.z', 'a.b.*', '*.c.d'],
    }),
    tap(paths => {
      paths.forEach({ pathPattern, path } => {
        console.log(`path ${path} has been reported to change because of matched pattern ${pathPattern}`);
      });
    }),
    ignoreElements()
  );
  1. My state subscription is created dynamically and it is updating incorrectly, what gives?

An operator that is applied to the observable more than once needs to share the same key as the previous iteration to preserve its cache. Specify a key in your subscription and it will behave as expected.

import { ignoreElements, tap } from 'rxjs/operators';
import { createStateSubscription } from 'epic-state-subscriptions';
import { sideEffectAction } from './actions';

const exampleEpic = (action$, state$) =>
  action$.pipe(
    mergeMap(action => {
      return of(action).pipe(
        createStateSubscription(state$, {
          key: 'dynamicSubscription',
          paths: ['x.y.z', 'a.b.*', '*.c.d'],
        }),
        tap(paths => {
          paths.forEach({ pathPattern, path } => {
          console.log(`path ${path} has been reported to change because of matched pattern ${pathPattern}`);
        }),
        ignoreElements()
      )
    }),
  );

epic-state-subscriptions's People

Contributors

danreynolds avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.