GithubHelp home page GithubHelp logo

gaearon / redux-electron-store Goto Github PK

View Code? Open in Web Editor NEW

This project forked from samiskin/redux-electron-store

2.0 3.0 1.0 65 KB

A redux implementation that synchronizes itself between Electron processes

License: MIT License

JavaScript 100.00%

redux-electron-store's Introduction

redux-electron-store

npm version npm downloads

This library solves the problem of synchronizing Redux stores in Electron apps. Electron is based on Chromium, and thus all Electron apps have a single main process and (potentially) multiple renderer processes, one for each web page. redux-electron-store allows us to define a store per process, and uses ipc to keep them in sync in an efficient manner. It is implemented as a redux store enhancer.

This library only works if the data in your store is immutable, as objects are compared by reference to determine changes.

Installation

npm i redux-electron-store

Usage

Main Process

import { electronEnhancer } from 'redux-electron-store';

let finalCreateStore = compose(
  applyMiddleware(...middleware),
  electronEnhancer()
)(createStore);

let store = finalCreateStore(reducer);

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.

let filter = {
  notifications: true,
  settings: true
};

let finalCreateStore = compose(
  applyMiddleware(...middleware),
  electronEnhancer({filter}),
  DevTools.instrument()
)(createStore);
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 _
let filter = {
  notifications: true,
  settings: {
    notifyPosition: true
  },
  teams: (teams) => {
    return _.mapValues(teams, (team) => {
      return {icons: true};
    });
  }
};

More options are documented in the api docs, and a description of exactly how this library works is on the way.

TODOs

  1. Add the functionality to persist state across application executions.
  2. Formalize and implement unit testing
  3. Create working Electron App to serve as a complete example

License

MIT

redux-electron-store's People

Contributors

charliehess avatar samiskin avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

rubythonode

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.