Skip to content

Commit d9541ab

Browse files
committed
Initial release!
0 parents  commit d9541ab

12 files changed

Lines changed: 878 additions & 0 deletions

.editorconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[{package.json,.*rc,*.yml}]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
16+
indent_style = space
17+
indent_size = 2

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
node_modules
2+
npm-debug.log
3+
/inline
4+
/worker
5+
/dist
6+
/full
7+
package-lock.json
8+
/preact.js
9+
/preact.js.map
10+
/react.js
11+
/react.js.map

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- stable

README.md

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<p align="center">
2+
<img src="https://i.imgur.com/o0u6dto.png" width="300" height="300" alt="stockroom">
3+
<br>
4+
<a href="https://www.npmjs.org/package/stockroom"><img src="https://img.shields.io/npm/v/stockroom.svg?style=flat" alt="npm"></a> <a href="https://travis-ci.org/developit/stockroom"><img src="https://travis-ci.org/developit/stockroom.svg?branch=master" alt="travis"></a>
5+
</p>
6+
7+
# Stockroom
8+
9+
> Offload your store management to a worker.
10+
11+
Stockroom seamlessly runs a [Unistore] 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.
12+
13+
- **Easy** same API as [unistore] - a simple add-on
14+
- **Opt-in** centralized actions with the option of running on the main thread
15+
- **Convenient** action selector shorthand - no action creator needed for simple actions
16+
- **Gracefully degrades** - feature-detect Worker support and fall back to `stockroom/inline`
17+
18+
19+
## Table of Contents
20+
21+
- [Install](#install)
22+
- [Usage](#usage)
23+
- [API](#api)
24+
- [License](#license)
25+
26+
27+
## Install
28+
29+
Stockroom requires that you install [unistore](https://github.com/developit/unistore) (300b) as a peer dependency.
30+
31+
```sh
32+
npm install --save unistore stockroom
33+
```
34+
35+
36+
## Usage
37+
38+
We'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.
39+
40+
**index.js**:
41+
42+
```js
43+
import createStore from 'stockroom'
44+
import StoreWorker from 'worker-loader!./worker'
45+
46+
let store = createStore(new StoreWorker())
47+
48+
let increment = store.action('increment')
49+
store.subscribe(console.log)
50+
51+
// Let's run a registered "increment" action in the worker.
52+
// This will eventually log a state update to the console - `{ count: 1 }`
53+
increment()
54+
```
55+
56+
57+
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 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 funtion it returns.
58+
59+
**worker.js**:
60+
61+
```js
62+
import createStore from 'stockroom/worker'
63+
64+
let store = createStore({
65+
count: 0
66+
})
67+
68+
store.registerActions( store => ({
69+
increment: ({ count }) => ({ count: count+1 })
70+
}) )
71+
72+
export default store // if you wish to use `stockroom/inline`
73+
```
74+
75+
76+
### API
77+
78+
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
79+
80+
#### module:stockroom
81+
82+
The main stockroom module, which runs on the main thread.
83+
84+
##### createStore
85+
86+
Given a Web Worker instance, sets up RPC-based synchronization with a WorkerStore running within it.
87+
88+
**Parameters**
89+
90+
- `worker` **[Worker](https://developer.mozilla.org/docs/Web/JavaScript)** An instantiated Web Worker (eg: `new Worker('./store.worker.js')`)
91+
92+
**Examples**
93+
94+
```javascript
95+
import createStore from 'stockroom'
96+
import StoreWorker from 'worker-loader!./store.worker'
97+
let store = createStore(new StoreWorker)
98+
```
99+
100+
Returns **Store** synchronizedStore - a mock unistore store instance sitting in front of the worker store.
101+
102+
#### module:stockroom/inline
103+
104+
Used to run your whole store on the main thread.
105+
Useful non-worker environments or as a fallback.
106+
107+
##### createInlineStore
108+
109+
For SSR/prerendering, pass your exported worker store through this enhancer
110+
to make an inline synchronous version that runs in the same thread.
111+
112+
**Parameters**
113+
114+
- `workerStore` **WorkerStore** The exported `store` instance that would have been invoked in a Worker
115+
116+
**Examples**
117+
118+
```javascript
119+
let store
120+
if (SUPPORTS_WEB_WORKERS) {
121+
let createStore = require('stockroom/inline')
122+
store = createStore(require('./store.worker'))
123+
}
124+
else {
125+
let createStore = require('stockroom')
126+
let StoreWorker = require('worker-loader!./store.worker')
127+
store = createStore(new StoreWorker())
128+
}
129+
export default store
130+
```
131+
132+
Returns **Store** inlineStore - a unistore instance with centralized actions
133+
134+
#### module:stockroom/worker
135+
136+
The other half of stockroom, which runs inside a Web Worker.
137+
138+
##### createWorkerStore
139+
140+
Creates a unistore instance for use in a Web Worker that synchronizes itself to the main thread.
141+
142+
**Parameters**
143+
144+
- `initialState` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Initial state to populate (optional, default `{}`)
145+
146+
**Examples**
147+
148+
```javascript
149+
import createWorkerStore from 'stockroom/worker'
150+
let initialState = { count: 0 }
151+
let store = createWorkerStore(initialState)
152+
store.registerActions({
153+
increment(state) {
154+
return { count: state.count + 1 }
155+
}
156+
})
157+
```
158+
159+
Returns **WorkerStore** workerStore (enhanced unistore store)
160+
161+
162+
### License
163+
164+
[MIT License](https://oss.ninja/mit/developit) © [Jason Miller](https://jasonformat.com/)
165+
166+
167+
[unistore]: https://github.com/developit/unistore
168+
[preact]: https://github.com/developit/preact
169+
[worker-loader]: https://github.com/webpack-contrib/worker-loader
170+
[workerize-loader]: https://github.com/developit/workerize-loader

package.json

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
"name": "stockroom",
3+
"version": "1.0.0",
4+
"description": "Offload your store management to a worker.",
5+
"source": "src/index.js",
6+
"module": "dist/stockroom.es.js",
7+
"main": "dist/stockroom.js",
8+
"umd:main": "dist/stockroom.umd.js",
9+
"typings": "index.d.ts",
10+
"scripts": {
11+
"build": "npm-run-all --silent bundle size docs",
12+
"bundle": "microbundle src/index.js -o dist/stockroom.js && microbundle src/inline.js -o inline/index.js && microbundle src/worker.js -o worker/index.js",
13+
"size": "strip-json-comments --no-whitespace dist/stockroom.js | gzip-size && bundlesize",
14+
"docs": "documentation readme src/*.js -q --section API && npm run -s fixreadme",
15+
"fixreadme": "node -e 'var fs=require(\"fs\");fs.writeFileSync(\"README.md\", fs.readFileSync(\"README.md\", \"utf8\").replace(/^- /gm, \"- \"))'",
16+
"test": "eslint src test && npm run bundle && karma start test/karma.conf.js --single-run",
17+
"prepare": "npm t",
18+
"release": "npm t && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
19+
},
20+
"eslintConfig": {
21+
"extends": "eslint-config-developit",
22+
"rules": {
23+
"prefer-rest-params": 0,
24+
"jest/valid-expect": 0
25+
}
26+
},
27+
"bundlesize": [
28+
{
29+
"path": "dist/stockroom.js",
30+
"maxSize": "1kb"
31+
},
32+
{
33+
"path": "worker/index.js",
34+
"maxSize": "1kb"
35+
},
36+
{
37+
"path": "inline/index.js",
38+
"maxSize": "300b"
39+
}
40+
],
41+
"files": [
42+
"src",
43+
"dist",
44+
"inline",
45+
"worker"
46+
],
47+
"keywords": [
48+
"preact",
49+
"unistore",
50+
"state machine",
51+
"worker",
52+
"threads",
53+
"redux"
54+
],
55+
"repository": "developit/stockroom",
56+
"author": "Jason Miller <jason@developit.ca>",
57+
"license": "MIT",
58+
"devDependencies": {
59+
"babel-jest": "^21.2.0",
60+
"babel-loader": "^7.1.2",
61+
"babel-plugin-transform-react-jsx": "^6.24.1",
62+
"babel-polyfill": "^6.26.0",
63+
"babel-preset-env": "^1.6.1",
64+
"babel-preset-stage-0": "^6.24.1",
65+
"bundlesize": "^0.15.3",
66+
"chai": "^4.1.2",
67+
"documentation": "^4.0.0",
68+
"eslint": "^4.12.1",
69+
"eslint-config-developit": "^1.1.1",
70+
"jest": "^21.2.1",
71+
"karma": "^2.0.0",
72+
"karma-chai-sinon": "^0.1.5",
73+
"karma-chrome-launcher": "^2.2.0",
74+
"karma-mocha": "^1.3.0",
75+
"karma-mocha-reporter": "^2.2.5",
76+
"karma-sourcemap-loader": "^0.3.7",
77+
"karma-webpack": "^2.0.9",
78+
"microbundle": "^0.3.1",
79+
"mocha": "^4.1.0",
80+
"npm-run-all": "^4.1.2",
81+
"sinon": "^4.1.6",
82+
"sinon-chai": "^2.14.0",
83+
"strip-json-comments-cli": "^1.0.1",
84+
"unistore": "^3.0.3",
85+
"webpack": "^3.10.0",
86+
"workerize-loader": "^1.0.1"
87+
},
88+
"peerDependencies": {
89+
"unistore": "*"
90+
}
91+
}

0 commit comments

Comments
 (0)