From e473937f8c882c278250d8abf33715a945ae8988 Mon Sep 17 00:00:00 2001 From: Jordan Mele Date: Wed, 9 Jan 2019 15:23:41 +1100 Subject: [PATCH 1/2] TypeScript definitions --- package.json | 6 ++++-- src/index.d.ts | 12 ++++++++++++ src/inline.d.ts | 33 +++++++++++++++++++++++++++++++++ src/worker.d.ts | 28 ++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/index.d.ts create mode 100644 src/inline.d.ts create mode 100644 src/worker.d.ts diff --git a/package.json b/package.json index 1418720..1845720 100644 --- a/package.json +++ b/package.json @@ -6,14 +6,15 @@ "module": "dist/stockroom.es.js", "main": "dist/stockroom.js", "umd:main": "dist/stockroom.umd.js", - "typings": "index.d.ts", + "typings": "src/index.d.ts", "scripts": { - "build": "npm-run-all --silent bundle size docs", + "build": "npm-run-all --silent bundle size docs types", "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", "size": "strip-json-comments --no-whitespace dist/stockroom.js | gzip-size && bundlesize", "docs": "documentation readme src/*.js -q --section API && npm run -s fixreadme", "fixreadme": "node -e 'var fs=require(\"fs\");fs.writeFileSync(\"README.md\", fs.readFileSync(\"README.md\", \"utf8\").replace(/^- /gm, \"- \"))'", "test": "eslint src test && npm run bundle && karma start test/karma.conf.js --single-run", + "types": "cp-cli src/worker.d.ts worker/index.d.ts && cp-cli src/inline.d.ts inline/index.d.ts", "prepare": "npm t", "release": "npm t && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" }, @@ -68,6 +69,7 @@ "babel-preset-stage-0": "^6.24.1", "bundlesize": "^0.15.3", "chai": "^4.1.2", + "cp-cli": "^1.1.2", "documentation": "^4.0.0", "eslint": "^4.16.0", "eslint-config-developit": "^1.1.1", diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..e6acfa6 --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,12 @@ +import { Store } from "unistore"; + +/** + * Given a Web Worker instance, sets up RPC-based synchronization with a WorkerStore running within it. + * @param worker An instantiated Web Worker (eg: `new Worker('./store.worker.js')`) + * @returns A mock unistore store instance sitting in front of the worker store. + * @example + * import createStore from 'stockroom' + * import StoreWorker from 'worker-loader!./store.worker' + * let store = createStore(new StoreWorker) + */ +export default function createStore(worker: Worker): Store; diff --git a/src/inline.d.ts b/src/inline.d.ts new file mode 100644 index 0000000..4584b9e --- /dev/null +++ b/src/inline.d.ts @@ -0,0 +1,33 @@ +import { Store } from "unistore"; + +export interface WorkerStore extends Store { + /** + * Queue all additional processing until unfrozen. + */ + freeze: () => void; + /** + * Remove a freeze lock and process queued work. + */ + unfreeze: () => void; +} + +/** + * For SSR/prerendering, pass your exported worker store through this enhancer to make an inline synchronous version that runs in the same thread. + * @param workerStore The exported `store` instance that would have been invoked in a Worker + * @returns A unistore instance with centralized actions + * @example + * let store + * if (SUPPORTS_WEB_WORKERS) { + * let createStore = require('stockroom/inline') + * store = createStore(require('./store.worker')) + * } + * else { + * let createStore = require('stockroom') + * let StoreWorker = require('worker-loader!./store.worker') + * store = createStore(new StoreWorker()) + * } + * export default store + */ +export default function createInlineStore( + workerStore: WorkerStore +): WorkerStore; diff --git a/src/worker.d.ts b/src/worker.d.ts new file mode 100644 index 0000000..2e56785 --- /dev/null +++ b/src/worker.d.ts @@ -0,0 +1,28 @@ +import { Store } from "unistore"; + +export interface WorkerStore extends Store { + /** + * Queue all additional processing until unfrozen. + */ + freeze: () => void; + /** + * Remove a freeze lock and process queued work. + */ + unfreeze: () => void; +} + +/** + * Creates a unistore instance for use in a Web Worker that synchronizes itself to the main thread. + * @param initialState Initial state to populate + * @returns Enhanced unistore store + * @example + * import createWorkerStore from 'stockroom/worker' + * let initialState = { count: 0 } + * let store = createWorkerStore(initialState) + * store.registerActions({ + * increment(state) { + * return { count: state.count + 1 } + * } + * }) + */ +export default function createWorkerStore(initialState?: K): WorkerStore; From 3f33ca8b431a46b971a5f139d922e42c7f21ee34 Mon Sep 17 00:00:00 2001 From: Jordan Mele Date: Mon, 14 Jan 2019 19:23:38 +1100 Subject: [PATCH 2/2] Removed useless TS code comments and documented generics --- src/index.d.ts | 11 ++--------- src/inline.d.ts | 25 ++----------------------- src/worker.d.ts | 22 ++-------------------- 3 files changed, 6 insertions(+), 52 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index e6acfa6..b11e237 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,12 +1,5 @@ +// K - Store state + import { Store } from "unistore"; -/** - * Given a Web Worker instance, sets up RPC-based synchronization with a WorkerStore running within it. - * @param worker An instantiated Web Worker (eg: `new Worker('./store.worker.js')`) - * @returns A mock unistore store instance sitting in front of the worker store. - * @example - * import createStore from 'stockroom' - * import StoreWorker from 'worker-loader!./store.worker' - * let store = createStore(new StoreWorker) - */ export default function createStore(worker: Worker): Store; diff --git a/src/inline.d.ts b/src/inline.d.ts index 4584b9e..c7646d7 100644 --- a/src/inline.d.ts +++ b/src/inline.d.ts @@ -1,33 +1,12 @@ +// K - Store state + import { Store } from "unistore"; export interface WorkerStore extends Store { - /** - * Queue all additional processing until unfrozen. - */ freeze: () => void; - /** - * Remove a freeze lock and process queued work. - */ unfreeze: () => void; } -/** - * For SSR/prerendering, pass your exported worker store through this enhancer to make an inline synchronous version that runs in the same thread. - * @param workerStore The exported `store` instance that would have been invoked in a Worker - * @returns A unistore instance with centralized actions - * @example - * let store - * if (SUPPORTS_WEB_WORKERS) { - * let createStore = require('stockroom/inline') - * store = createStore(require('./store.worker')) - * } - * else { - * let createStore = require('stockroom') - * let StoreWorker = require('worker-loader!./store.worker') - * store = createStore(new StoreWorker()) - * } - * export default store - */ export default function createInlineStore( workerStore: WorkerStore ): WorkerStore; diff --git a/src/worker.d.ts b/src/worker.d.ts index 2e56785..73eb2df 100644 --- a/src/worker.d.ts +++ b/src/worker.d.ts @@ -1,28 +1,10 @@ +// K - Store state + import { Store } from "unistore"; export interface WorkerStore extends Store { - /** - * Queue all additional processing until unfrozen. - */ freeze: () => void; - /** - * Remove a freeze lock and process queued work. - */ unfreeze: () => void; } -/** - * Creates a unistore instance for use in a Web Worker that synchronizes itself to the main thread. - * @param initialState Initial state to populate - * @returns Enhanced unistore store - * @example - * import createWorkerStore from 'stockroom/worker' - * let initialState = { count: 0 } - * let store = createWorkerStore(initialState) - * store.registerActions({ - * increment(state) { - * return { count: state.count + 1 } - * } - * }) - */ export default function createWorkerStore(initialState?: K): WorkerStore;