{"id":26706913,"url":"https://github.com/zoubingwu/electron-shared-state","last_synced_at":"2025-04-13T15:22:57.975Z","repository":{"id":50857105,"uuid":"241214704","full_name":"zoubingwu/electron-shared-state","owner":"zoubingwu","description":"❤️ easily sharing state across electron main and renderer processes.","archived":false,"fork":false,"pushed_at":"2023-09-06T19:54:54.000Z","size":1432,"stargazers_count":57,"open_issues_count":3,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-20T09:35:43.318Z","etag":null,"topics":["cross-process","electron","immer","immutability","shared-state","state-management","sync"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zoubingwu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-17T21:47:27.000Z","updated_at":"2024-11-29T11:14:33.000Z","dependencies_parsed_at":"2024-06-21T13:02:00.996Z","dependency_job_id":"71203b5c-9d3e-4188-b795-135822ed4cc6","html_url":"https://github.com/zoubingwu/electron-shared-state","commit_stats":null,"previous_names":["shadeofgod/electron-shared-state"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoubingwu%2Felectron-shared-state","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoubingwu%2Felectron-shared-state/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoubingwu%2Felectron-shared-state/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoubingwu%2Felectron-shared-state/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zoubingwu","download_url":"https://codeload.github.com/zoubingwu/electron-shared-state/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248733227,"owners_count":21152980,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cross-process","electron","immer","immutability","shared-state","state-management","sync"],"created_at":"2025-03-27T06:19:50.218Z","updated_at":"2025-04-13T15:22:57.944Z","avatar_url":"https://github.com/zoubingwu.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# electron-shared-state\n\n![](https://img.shields.io/npm/l/electron-shared-state)\n![](https://badgen.net/npm/v/electron-shared-state)\n![](https://badgen.net/npm/types/electron-shared-state)\n![](https://badgen.net/bundlephobia/minzip/electron-shared-state)\n![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/zoubingwu/electron-shared-state/publish.yaml?label=publish)\n![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/zoubingwu/electron-shared-state/test.yaml?label=test)\n\nSharing state between main and renderer process can be this easy.\n\n- 🚀 Mutate your state while keep them in sync with other process\n- 🎯 Write in typescript with full typing support\n- ❤️ Elegant and easy to learn API\n- 👻 Immutability and structural sharing out of the box with built-in immer\n\n![](./showcase.gif)\n\n## Install\n\n```sh\nnpm install electron-shared-state\n```\n\nor\n\n```sh\nyarn add electron-shared-state\n```\n\n## Usage\n\nYou can check source code under [example directory](/example).\n\n```ts\n// shared\nexport const initialState = 0;\n\n// renderer\nimport { createSharedStore } from 'electron-shared-state';\n\nconst sharedStore = createSharedStore(initialState);\n\nsharedStore.subscribe((state) =\u003e {\n  console.log(state);\n});\n\nsetTimeout(() =\u003e {\n  sharedStore.setState((state) =\u003e {\n    state = state + 1;\n  });\n}, 2000);\n\n// main\nimport { createSharedStore } from 'electron-shared-state';\n\nconst sharedStore = createSharedStore(initialState);\n\nsharedStore.subscribe((state) =\u003e {\n  console.log(state);\n});\n\n// both main and renderer will print the state after two seconds.\n```\n\nIf your project already using state management tools like redux, you can easily replace a slice of your state with electron-shared-state, so you can just share part of the state you want to share without create a whole state tree in both processes.\n\n```ts\n// renderer\n\nconst sharedStore = createSharedStore(initialState);\n\n// split state into a reducer\nfunction sharedReducer(state, action) {\n  switch (action.type) {\n    case 'some action type':\n      const nextState = sharedStore.setState(...);\n      return nextState;\n  }\n}\n\n// combine with other reducer\nconst rootReducer = combindReducers({\n  other: ...,\n  shared: sharedReducer,\n  ...\n});\n\n// create redux store\nconst store = createStore(rootReducer)\n\n// in main process\n// only this part of state will be shared across main and renderer\nexport const store = createSharedStore(initialState);\n```\n\n## API Reference\n\nelectron-shared-state only provides one simple function: `createSharedStore`. The signature is like below:\n\n```ts\ninterface Options {\n  name?: string;\n}\n\nfunction createSharedStore\u003cT\u003e(\n  state: T,\n  options?: Options\n): {\n  setState: (recipe: (draft: T) =\u003e void, description?: string | undefined) =\u003e T;\n  getState: () =\u003e T;\n  subscribe: (\n    listener: (state: T, description?: string | undefined) =\u003e void\n  ) =\u003e () =\u003e void;\n};\n```\n\nThe input is the state your want to share across processes, generally it's an object.\n\nIt also accepts an optional `Option` object, you can pass a store name if you want to have multiple stores.\n\n```ts\nconst s1 = createSharedStore(..., { name: 's1' })\nconst s2 = createSharedStore(..., { name: 's2' })\n```\n\nIt returns a Store object with a few methods on it.\n\n**`setState(stateUpdater, description)`**\n\nAccepts a stateUpdater function and a description string for debug purpose. The stateUpdater is like the second argument of immer's produce, so it inherits [immer's pitfalls](https://immerjs.github.io/immer/docs/pitfalls).\n\nReturns the new state. It use immer underneath so the state remains immutable, to keep it in sync across processes, you should always use setState to update it.\n\n**`getState()`**\n\nReturns the current state.\n\n**`subscribe(listener)`**\n\nAdds a change listener. It will be called any time the state is changed, the listener receives the latest state and a description string as arguments.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzoubingwu%2Felectron-shared-state","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzoubingwu%2Felectron-shared-state","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzoubingwu%2Felectron-shared-state/lists"}