NewHava / electron-shared-state

❤️ easily sharing state between electron main and renderer process.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

electron-shared-state

Sharing state between main and renderer process can be this easy.

  • 🚀 Mutate your state while keep them in sync with other process!
  • 🎯 Write in typescript with full typing support!
  • ❤️ Elegant and easy to learn API!

Install

npm install electron-shared-state

or

yarn add electron-shared-state

Usage

// shared
export const initialState = 0;

// renderer
const sharedStore = createSharedStore(initialState);
sharedStore.subscribe(state => {
  console.log(state);
});

setTimeout(() => {
  sharedStore.setState(state => {
    state = state + 1;
  });
}, 2000);

// main
const sharedStore = createSharedStore(initialState);
sharedStore.subscribe(state => {
  console.log(state);
});

// both main and renderer will print the state after two seconds.

check source code under example directory for more info.

API Reference

function createSharedStore<T>(
  state: T
): {
  setState: (recipe: (draft: T) => void, description?: string | undefined) => T;
  getState: () => T;
  subscribe: (
    listener: (state: T, description?: string | undefined) => void
  ) => () => void;
};

About

❤️ easily sharing state between electron main and renderer process.

License:MIT License


Languages

Language:TypeScript 58.1%Language:JavaScript 32.3%Language:HTML 9.6%