Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🗃 Offload your store management to a worker easily.

NotificationsYou must be signed in to change notification settings

developit/stockroom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

stockroom
stockroom
npmtravis

Stockroom

Offload your store management to a worker.

Stockroom seamlessly runs aUnistore store (and its actions) in a Web Worker, setting up optimized bidirectional sync so you can also use & subscribe to it on the main thread.

  • Easy same API asunistore - a simple add-on
  • Opt-in centralized actions with the option of running on the main thread
  • Convenient action selector shorthand - no action creator needed for simple actions
  • Gracefully degrades - feature-detect Worker support and fall back tostockroom/inline

Table of Contents

Install

Stockroom requires that you installunistore (300b) as a peer dependency.

npm install --save unistore stockroom

Usage

We'll have two files:index.js andworker.js. The first is what we import from our app, so it runs on the main thread - it imports our worker (usingworker-loader orworkerize-loader) and passes it to Stockroom to create a store instance around it.

index.js:

importcreateStorefrom'stockroom'importStoreWorkerfrom'worker-loader!./worker'letstore=createStore(newStoreWorker())letincrement=store.action('increment')store.subscribe(console.log)// Let's run a registered "increment" action in the worker.// This will eventually log a state update to the console - `{ count: 1 }`increment()

The 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 likecreateStore() does inUnistore, but sets things up to synchronize with the main/parent thread. It also adds aregisterActions 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 invokingstore.action('theActionName') and calling the function it returns.

worker.js:

importcreateStorefrom'stockroom/worker'letstore=createStore({count:0})store.registerActions(store=>({increment:({ count})=>({count:count+1})}))exportdefaultstore// if you wish to use `stockroom/inline`

API

module:stockroom

The main stockroom module, which runs on the main thread.

createStore

Given a Web Worker instance, sets up RPC-based synchronization with a WorkerStore running within it.

Parameters

  • workerWorker An instantiated Web Worker (eg:new Worker('./store.worker.js'))

Examples

importcreateStorefrom'stockroom'importStoreWorkerfrom'worker-loader!./store.worker'letstore=createStore(newStoreWorker)

ReturnsStore synchronizedStore - a mock unistore store instance sitting in front of the worker store.

module:stockroom/inline

Used to run your whole store on the main thread.Useful non-worker environments or as a fallback.

createInlineStore

For SSR/prerendering, pass your exported worker store through this enhancerto make an inline synchronous version that runs in the same thread.

Parameters

  • workerStoreWorkerStore The exportedstore instance that would have been invoked in a Worker

Examples

letstoreif(SUPPORTS_WEB_WORKERS===false){letcreateStore=require('stockroom/inline')store=createStore(require('./store.worker'))}else{letcreateStore=require('stockroom')letStoreWorker=require('worker-loader!./store.worker')store=createStore(newStoreWorker())}exportdefaultstore

ReturnsStore inlineStore - a unistore instance with centralized actions

module:stockroom/worker

The other half of stockroom, which runs inside a Web Worker.

createWorkerStore

Creates a unistore instance for use in a Web Worker that synchronizes itself to the main thread.

Parameters

  • initialStateObject Initial state to populate (optional, default{})

Examples

importcreateWorkerStorefrom'stockroom/worker'letinitialState={count:0}letstore=createWorkerStore(initialState)store.registerActions({increment(state){return{count:state.count+1}}})

ReturnsWorkerStore workerStore (enhanced unistore store)

freeze

Queue all additional processing until unfrozen.freeze/unfreeze manages a cumulative lock:unfreeze must be called as many times as freeze was called in order to remove the lock.

unfreeze

Remove a freeze lock and process queued work.

License

MIT License ©Jason Miller


[8]ページ先頭

©2009-2025 Movatter.jp