@rematch/subscriptions

0.2.2 • Public • Published

Rematch Subscriptions

Subscriptions plugin for Rematch.

Install

npm install @rematch/subscriptions

Setup

import subscriptionsPlugin from '@rematch/subscriptions'
import { init } from '@rematch/core'

const subscriptions = subscriptionsPlugin()

init({
  plugins: [subscriptions]
})

Subscriptions

subscriptions: { [string]: (action, state, unsubscribe) => any }

Subscriptions are way for models to listen to changes in the app.

{
  name: 'settings',
  state: {},
  reducers: {
    set: (payload) => payload
  },
  subscriptions: {
    'profile/load': (action, state, unsubscribe) => {
      dispatch.settings.set(action.payload)
    }
  }
}

In this case, subscriptions avoid the need of coupling the auth model to the profile model. Profile simply listens to an action.

Subscriptions help you make isolated models that know nothing about their neighbours.

Use unsubscribe if you'd like an action to fire only once.

{
  name: 'settings',
  state: {},
  reducers: {
    set: (payload) => payload
  },
  subscriptions: {
    'profile/load': (action, state, unsubscribe) => {
      dispatch.settings.set(action.payload)
      unsubscribe()
    }
  }
}

Subscriptions can also use pattern matching. Bear in mind, that the pattern should avoid matching any actions within your model to prevent an infinite loop.

{
  subscriptions: {
    'routeChange/*': (action) => console.log(action)
  }
}

The following patterns are all valid:

modelName/actionName
*/actionName
modelName/*
a-*/b
a/b-*

If possible, pattern matching should be avoided as it can effect performance.

Package Sidebar

Install

npm i @rematch/subscriptions

Weekly Downloads

0

Version

0.2.2

License

ISC

Unpacked Size

49.1 kB

Total Files

9

Last publish

Collaborators

  • semoal
  • shmck
  • blairbodnar