{"id":16057442,"url":"https://github.com/aduth/unistore-browser-sync","last_synced_at":"2026-04-30T10:31:16.902Z","repository":{"id":36673405,"uuid":"229492632","full_name":"aduth/unistore-browser-sync","owner":"aduth","description":"Extend a Unistore store to replicate state for your browser extension using browser.runtime.Port","archived":false,"fork":false,"pushed_at":"2023-07-25T00:48:55.000Z","size":163,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-12T17:16:15.458Z","etag":null,"topics":["redux","state","unistore","webextension"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/aduth.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","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":"2019-12-21T22:54:20.000Z","updated_at":"2021-11-14T19:32:22.000Z","dependencies_parsed_at":"2024-10-30T23:40:43.316Z","dependency_job_id":"6d24da7e-7b38-4682-8e02-c35509f33847","html_url":"https://github.com/aduth/unistore-browser-sync","commit_stats":{"total_commits":13,"total_committers":2,"mean_commits":6.5,"dds":"0.23076923076923073","last_synced_commit":"4fe7df25987d704533100c3a3a666aee26393bd4"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aduth%2Funistore-browser-sync","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aduth%2Funistore-browser-sync/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aduth%2Funistore-browser-sync/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aduth%2Funistore-browser-sync/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aduth","download_url":"https://codeload.github.com/aduth/unistore-browser-sync/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247305947,"owners_count":20917208,"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":["redux","state","unistore","webextension"],"created_at":"2024-10-09T03:02:25.765Z","updated_at":"2026-04-30T10:31:16.855Z","avatar_url":"https://github.com/aduth.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Unistore Browser Sync\n\nExtend a [Unistore](https://github.com/developit/unistore) store to replicate state for your browser extension using [`browser.runtime.Port`](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/Port), enabling you to share and manipulate state between an extension's background, popup, and options scripts.\n\n## Example\n\n```js\n// background.js\nimport createStore from 'https://unpkg.com/browse/unistore@3.5.1/dist/unistore.es.js';\nimport { primary } from 'https://unpkg.com/unistore-browser-sync';\n\nconst initialState = { count: 1 };\nconst actions = {\n\tincrement( state ) {\n\t\treturn { count: state.count + 1 };\n\t},\n};\nconst store = primary( createStore( initialState ), actions );\n```\n\n```js\n// popup.js\nimport createStore from 'https://unpkg.com/browse/unistore@3.5.1/dist/unistore.es.js';\nimport { replica } from 'https://unpkg.com/unistore-browser-sync';\n\nreplica( createStore() ).then( ( store ) =\u003e {\n\tconst logCount = () =\u003e console.log( 'The current count is %d.', store.getState().count );\n\tlogCount();\n\t// Logs: \"The current count is 1.\"\n\tstore.subscribe( logCount );\n\n\tstore.dispatch( 'increment' );\n\t// Logs: \"The current count is 2.\"\n} );\n```\n\n## Installation\n\nInstall using [npm](https://www.npmjs.com/):\n\n```\nnpm install unistore-browser-sync\n```\n\nOr reference directly from [unpkg](https://unpkg.com/):\n\nhttps://unpkg.com/unistore-browser-sync\n\n## Usage\n\nUnistore Browser Sync uses the [`browser.runtime.Port` interface](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/Port). If you support Chrome, you should include the [WebExtension `browser` API polyfill](https://github.com/mozilla/webextension-polyfill) if you are not already using it:\n\n```html\n\u003cscript src=\"/path/to/browser-polyfill.min.js\"\u003e\u003c/script\u003e\n```\n\nUnistore Browser Sync is written using ES Modules. You can import directly from the script if you are already using `\u003cscript type=\"module\"\u003e` for your extension:\n\n```js\nimport { replica } from 'https://unpkg.com/unistore-browser-sync';\n```\n\nOtherwise, include Unistore Browser Sync using a script tag. Note that it must be assigned `type=\"module\"`.\n\n```html\n\u003cscript type=\"module\" src=\"https://unpkg.com/unistore-browser-sync\"\u003e\u003c/script\u003e\n```\n\nWhen included as a script tag, the exported members will be added to the `window` global as `window.unistoreBrowserSync`:\n\n```js\nconst { primary, replica } = window.unistoreBrowserSync;\n```\n\nSyncing occurs between one _primary_ store and one or more _replica_ stores. You should initialize your primary store in your extension's background script, passing an instance of a Unistore store, plus an optional object of named action functions. Actions are bound to the store in the same way as [Unistore's `action` function](https://github.com/developit/unistore#action). A new `dispatch` method will be added to primary and replica store objects, enabling you to reference the actions in other scripts.\n\n```js\n// background.js\nimport createStore from 'https://unpkg.com/browse/unistore@3.5.1/dist/unistore.es.js';\nimport { primary } from 'https://unpkg.com/unistore-browser-sync';\n\nconst initialState = { count: 1 };\nconst actions = {\n\tincrement( state ) {\n\t\treturn { count: state.count + 1 };\n\t},\n};\nconst store = primary( createStore( initialState ), actions );\n```\n\nIn other scripts where you wish to consume from the primary store, you should use the `replica` exported member of the module. This function accepts an instance of a store on which the primary store state will be set continuously. The return value is a promise which resolves once a connection has been established to the primary store. The resolved value of the promise is a reference to the enhanced store object. A replica store should be treated as read-only with the exception of the `dispatch` action. All state mutations should occur via the actions defined in the primary store, called using the replica store's `dispatch` function.\n\n```js\n// popup.js\nimport createStore from 'https://unpkg.com/browse/unistore@3.5.1/dist/unistore.es.js';\nimport { replica } from 'https://unpkg.com/unistore-browser-sync';\n\nreplica( createStore() ).then( ( store ) =\u003e {\n\tconst logCount = () =\u003e console.log( 'The current count is %d.', store.getState().count );\n\tlogCount();\n\t// Logs: \"The current count is 1.\"\n\tstore.subscribe( logCount );\n\n\tstore.dispatch( 'increment' );\n\t// Logs: \"The current count is 2.\"\n} );\n```\n\n## API\n\n### `primary`\n\nEnhances the given store to sync changes to all connected replica stores. An optional object of actions keyed by action name enables state mutation from replica stores or the primary store itself. An object returned by a dispatched action applies as a patch on the current store state.\n\n**Parameters:**\n\n- `store` (`Store`) Store to enhance.\n- `actions` (`Object`) Action map.\n\n**Returns:** (`SyncStore`) Primary store\n\n### `replica`\n\nGiven a store to enhance as a replica of the primary store, returns a promise resolving to the enhanced store. The promise resolves once its initial state has been set from the primary store.\n\n**Parameters:**\n\n- `store` (`Store`) Store to enhance.\n\n**Returns:** (`Promise\u003cSyncStore\u003e`) Promise resolving to replica store.\n\n## Types\n\n### `SyncStore`\n\nEnhanced Unistore store, including dispatch function.\n\nSee: [https://github.com/developit/unistore#api](https://github.com/developit/unistore#api)\n\n### `store.dispatch`\n\nDispatch an action, applying the object result as a patch on the current store state.\n\n**Parameters:**\n\n- `action` (`string`) Action name.\n- `...args` (`...any`) Action arguments.\n\n## Browser Support\n\nUnistore Browser Sync is written and distributed using modern JavaScript, including [ES Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). Browser support for these features is quite good, and you should expect no issues in browsers which have been updated at least once since 2017. Specifically, this includes Chrome versions 61 and newer (September 2017) and Firefox 60 and newer (May 2018), and Edge 16 and newer (October 2017). If you need to support older versions of these browsers, you can consider to transpile the module as part of your application's build process.\n\nThe sync implementation relies on the `browser.runtime` interface. Consult the [MDN browser compatibility chart](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime#Browser_compatibility) for detailed information about browser support. Specifically, Unistore Browser Sync uses [`runtime.onConnect`](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onConnect), [`runtime.connect`](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/connect), and [`runtime.Port`](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/Port). You should not expect to have any issues when targeting reasonably up-to-date browsers.\n\n## License\n\nCopyright 2019 Andrew Duthie\n\nReleased under the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faduth%2Funistore-browser-sync","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faduth%2Funistore-browser-sync","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faduth%2Funistore-browser-sync/lists"}