{"id":13394349,"url":"https://github.com/developit/stockroom","last_synced_at":"2025-05-15T17:04:37.200Z","repository":{"id":48971697,"uuid":"118027299","full_name":"developit/stockroom","owner":"developit","description":"🗃 Offload your store management to a worker easily.","archived":false,"fork":false,"pushed_at":"2019-08-06T16:51:46.000Z","size":31,"stargazers_count":1755,"open_issues_count":10,"forks_count":44,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-05-13T22:26:53.099Z","etag":null,"topics":["flux","state-container","state-management","unistore","web-worker","worker"],"latest_commit_sha":null,"homepage":"https://stockroom.surge.sh","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/developit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-18T19:21:39.000Z","updated_at":"2025-03-04T15:16:21.000Z","dependencies_parsed_at":"2022-08-30T06:10:35.562Z","dependency_job_id":null,"html_url":"https://github.com/developit/stockroom","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fstockroom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fstockroom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fstockroom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fstockroom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developit","download_url":"https://codeload.github.com/developit/stockroom/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254384987,"owners_count":22062422,"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":["flux","state-container","state-management","unistore","web-worker","worker"],"created_at":"2024-07-30T17:01:16.738Z","updated_at":"2025-05-15T17:04:32.190Z","avatar_url":"https://github.com/developit.png","language":"JavaScript","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://i.imgur.com/352HMex.jpg\" width=\"1000\" alt=\"stockroom\"\u003e\n  \u003cbr\u003e\n  \u003cimg src=\"https://i.imgur.com/mSDvn6Z.jpg\" width=\"1000\" alt=\"stockroom\"\u003e\n  \u003cbr\u003e\n  \u003ca href=\"https://www.npmjs.org/package/stockroom\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/stockroom.svg?style=flat\" alt=\"npm\"\u003e\u003c/a\u003e \u003ca href=\"https://travis-ci.org/developit/stockroom\"\u003e\u003cimg src=\"https://travis-ci.org/developit/stockroom.svg?branch=master\" alt=\"travis\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n# Stockroom\n\n\u003e Offload your store management to a worker.\n\nStockroom seamlessly runs a [Unistore] store (and its actions) in a Web Worker, setting up optimized bidirectional sync so you can also use \u0026 subscribe to it on the main thread.\n\n- **Easy** same API as [unistore] - a simple add-on\n- **Opt-in** centralized actions with the option of running on the main thread\n- **Convenient** action selector shorthand - no action creator needed for simple actions\n- **Gracefully degrades** - feature-detect Worker support and fall back to `stockroom/inline`\n\n## Table of Contents\n\n- [Install](#install)\n- [Usage](#usage)\n- [API](#api)\n- [License](#license)\n\n## Install\n\nStockroom requires that you install [unistore](https://github.com/developit/unistore) (300b) as a peer dependency.\n\n```sh\nnpm install --save unistore stockroom\n```\n\n## Usage\n\nWe'll have two files: `index.js` and `worker.js`.  The first is what we import from our app, so it runs on the main thread - it imports our worker (using [worker-loader] or [workerize-loader]) and passes it to Stockroom to create a store instance around it.\n\n**index.js**:\n\n```js\nimport createStore from 'stockroom'\nimport StoreWorker from 'worker-loader!./worker'\n\nlet store = createStore(new StoreWorker())\n\nlet increment = store.action('increment')\nstore.subscribe(console.log)\n\n// Let's run a registered \"increment\" action in the worker.\n// This will eventually log a state update to the console - `{ count: 1 }`\nincrement()\n```\n\nThe second file is our worker code, which runs in the background thread. Here we import Stockroom's worker-side \"other half\", `stockroom/worker`.  This function returns a store instance just like `createStore()` does in [Unistore], but sets things up to synchronize with the main/parent thread.  It also adds a `registerActions` method to the store, which you can use to define globally-available actions for that store.  These actions can be triggered from the main thread by invoking `store.action('theActionName')` and calling the function it returns.\n\n**worker.js**:\n\n```js\nimport createStore from 'stockroom/worker'\n\nlet store = createStore({\n  count: 0\n})\n\nstore.registerActions( store =\u003e ({\n  increment: ({ count }) =\u003e ({ count: count+1 })\n}) )\n\nexport default store  // if you wish to use `stockroom/inline`\n```\n\n### API\n\n\u003c!-- Generated by documentation.js. Update this documentation by updating the source code. --\u003e\n\n#### module:stockroom\n\nThe main stockroom module, which runs on the main thread.\n\n##### createStore\n\nGiven a Web Worker instance, sets up RPC-based synchronization with a WorkerStore running within it.\n\n**Parameters**\n\n- `worker` **[Worker](https://developer.mozilla.org/docs/Web/JavaScript)** An instantiated Web Worker (eg: `new Worker('./store.worker.js')`)\n\n**Examples**\n\n```javascript\nimport createStore from 'stockroom'\nimport StoreWorker from 'worker-loader!./store.worker'\nlet store = createStore(new StoreWorker)\n```\n\nReturns **Store** synchronizedStore - a mock unistore store instance sitting in front of the worker store.\n\n#### module:stockroom/inline\n\nUsed to run your whole store on the main thread.\nUseful non-worker environments or as a fallback.\n\n##### createInlineStore\n\nFor SSR/prerendering, pass your exported worker store through this enhancer\n to make an inline synchronous version that runs in the same thread.\n\n**Parameters**\n\n- `workerStore` **WorkerStore** The exported `store` instance that would have been invoked in a Worker\n\n**Examples**\n\n```javascript\nlet store\nif (SUPPORTS_WEB_WORKERS === false) {\n\tlet createStore = require('stockroom/inline')\n\tstore = createStore(require('./store.worker'))\n}\nelse {\n\tlet createStore = require('stockroom')\n\tlet StoreWorker = require('worker-loader!./store.worker')\n\tstore = createStore(new StoreWorker())\n}\nexport default store\n```\n\nReturns **Store** inlineStore - a unistore instance with centralized actions\n\n#### module:stockroom/worker\n\nThe other half of stockroom, which runs inside a Web Worker.\n\n##### createWorkerStore\n\nCreates a unistore instance for use in a Web Worker that synchronizes itself to the main thread.\n\n**Parameters**\n\n- `initialState` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Initial state to populate (optional, default `{}`)\n\n**Examples**\n\n```javascript\nimport createWorkerStore from 'stockroom/worker'\nlet initialState = { count: 0 }\nlet store = createWorkerStore(initialState)\nstore.registerActions({\n\tincrement(state) {\n\t\treturn { count: state.count + 1 }\n\t}\n})\n```\n\nReturns **WorkerStore** workerStore (enhanced unistore store)\n\n#### freeze\n\nQueue all additional processing until unfrozen.\nfreeze/unfreeze manages a cumulative lock:\nunfreeze must be called as many times as freeze was called in order to remove the lock.\n\n#### unfreeze\n\nRemove a freeze lock and process queued work.\n\n### License\n\n[MIT License](https://oss.ninja/mit/developit) © [Jason Miller](https://jasonformat.com/)\n\n[unistore]: https://github.com/developit/unistore\n\n[preact]: https://github.com/developit/preact\n\n[worker-loader]: https://github.com/webpack-contrib/worker-loader\n\n[workerize-loader]: https://github.com/developit/workerize-loader\n","funding_links":[],"categories":["JavaScript","Web Worker"],"sub_categories":["Runner"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fstockroom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevelopit%2Fstockroom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fstockroom/lists"}