node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »

redux-electron-enhancer

redux-electron-enhancer

js-standard-style Build Status Coverage Status Maintainer status

This library is a fork of redux-electron-store with the following changes:

  • Compatibility with redux-saga
  • Removed features:
    • Removed support for renderer-side reducers / 'synchronous' mode of operation
    • Removed pre-dispatch and post-dispatch callbacks
    • Removed 'only sending actions when watched state changes', renderer stores now get all actions, regardless of whether it's in the filter or not
  • Minor changes:
    • Drop dependency on lodash
    • Use const whenever possible
    • Javascript standard style instead of airbnb's eslint preset

This is an internal fork built for a specific purpose — PRs that add features or complexity will be rejected, as I have no time to maintain it.

Installation

npm i redux-electron-enhancer

Usage

Main Process

import {createStore, applyMiddleware, compose} from 'redux'
import {electronEnhancer} from 'redux-electron-enhancer'
 
// this is required so that injected events (received from renderers via IPC) 
// go through the entire middleware stack again (e.g. redux-saga 'take' effects) 
const inject = (x) => store.dispatch(x)
 
const enhancer = compose(
  applyMiddleware(...middleware),
  // electronEnhancer needs to be last in the chain so that it can dispatch 
  // all actions to renderers via IPC (e.g. redux-saga 'put' effects) 
  electronEnhancer({inject})
)
 
// Note: passing enhancer as the last argument to createStore requires redux@>=3.1.0 
const store = createStore(reducer, initialState, enhancer)

Renderer / Webview Process

In the renderer process, the store will handle the filter property in its parameter. filter is a way of describing exactly what data this renderer process wishes to be notified of. If a filter is provided, all updates which do not change a property which passes the filter will not be forwarded to the current renderer.

const filter = {
  notifications: true,
  settings: true
}
 
const enhancer = compose(
  applyMiddleware(...middleware),
  electronEnhancer({filter}),
  DevTools.instrument()
)
 
// Note: passing enhancer as the last argument to createStore requires redux@>=3.1.0 
const store = createStore(reducer, initialState, enhancer)
Filters

A filter can be an object, a function, or true.

If the filter is true, the entire variable will pass through the filter.

If the filter is a function, the function will be called with the variable the filter is acting on as a parameter, and the return value of the function must itself be a filter (either an object or true)

If the filter is an object, its keys must be properties of the variable the filter is acting on, and its values are themselves filters which describe the value(s) of that property that will pass through the filter.

Example Problem:

I am creating a Notifications window for Slack's application. For this to work, I need to know the position to display the notifications, the notifications themselves, and the icons for each team to display as a thumbnail. Any other data in my app has no bearing on this window, therefore it would be a waste for this window to have updates for any other data sent to it.

Solution:

// Note: The Lodash library is being used here as _ 
const filter = {
  notifications: true,
  settings: {
    notifyPosition: true
  },
  teams: (teams) => {
    return _.mapValues(teams, (team) => {
      return {icons: true}
    })
  }
}

More options are documented in the api docs.

License

MIT