npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

electron-global-state-sync

v0.1.1

Published

Type-safe global state synchronized across Electron main and renderer processes, with React hooks.

Readme

SyncState

A small TypeScript library for one authoritative Electron state store. The main process owns every value; sandboxed renderer windows read, write, and subscribe through a narrow contextBridge API. A React hook wraps the renderer client.

Install

npm install electron-global-state-sync

Minimal React example

After exposing a typed bridge from preload, create one renderer client and hook:

import { createGlobalStateClient } from "electron-global-state-sync/renderer";
import { createUseGlobalState } from "electron-global-state-sync/react";
import type { GlobalStateBridge } from "electron-global-state-sync/types";

interface AppState {
  count: number;
}

declare global {
  interface Window {
    globalState: GlobalStateBridge<AppState>;
  }
}

const client = createGlobalStateClient(window.globalState, { count: 0 });
const useGlobalState = createUseGlobalState(client);

export function Counter() {
  const [count, setCount] = useGlobalState("count");

  return <button onClick={() => setCount((value) => value + 1)}>{count}</button>;
}

Electron wiring

Install the canonical store in the main process with the same shared state type:

import { ipcMain } from "electron";
import { createGlobalStateMain } from "electron-global-state-sync";
import { initialState } from "./shared";

const globalState = createGlobalStateMain(ipcMain, initialState);

globalState.get("count");
globalState.set("status", "working");

Expose only the typed bridge from the preload script:

import { exposeGlobalState } from "electron-global-state-sync/preload";
import type { AppState } from "./shared";

exposeGlobalState<AppState>();

Keep renderer security enabled:

new BrowserWindow({
  webPreferences: {
    preload: PRELOAD_PATH,
    contextIsolation: true,
    nodeIntegration: false,
    sandbox: true
  }
});

The renderer's initial snapshot prevents a loading-only render; the subscribed main-process value replaces it immediately.

The bridge and client also expose typed get, set, subscribe, and unsubscribe methods. client.subscribe() returns an unsubscribe function.

Architecture

React component
  ↕ useGlobalState / renderer cache
contextBridge API in preload
  ↕ narrow IPC channels
canonical state in Electron main
  ↳ broadcasts changed keys to every subscribed renderer

The main process validates keys, tracks subscriptions per webContents, removes them when a window is destroyed, and broadcasts updates after every accepted write. Renderer clients ignore stale initial reads so an older response cannot overwrite a newer subscription event.

Limitations

  • State is in memory only and resets when the app exits.
  • Concurrent writes are last-write-wins. Functional setters use the renderer's latest snapshot; they are not cross-window atomic transactions.
  • Keys are validated at runtime, but value types are enforced by TypeScript only.
  • Values must be Electron structured-clone compatible. Replace objects through set rather than mutating references returned inside the main process.
  • One IPC handler set is installed per Electron app; call dispose() before replacing it in tests or during teardown.

Development

npm install
npm test
npm run build
npm run demo

npm run demo opens two windows. Change the count or message in either window to see the other update immediately. npm run demo:smoke performs the same bidirectional check automatically and exits.