diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..cf6e03f --- /dev/null +++ b/index.d.ts @@ -0,0 +1,10 @@ +import { WorkerStore } from "./worker"; +import { Store } from "unistore"; + +/** + * Create a new store based on a Worker. + * @param worker Instance of WorkerStore. + */ +export default function createStore( + worker: WorkerStore +): WorkerStore; diff --git a/worker.d.ts b/worker.d.ts new file mode 100644 index 0000000..1782831 --- /dev/null +++ b/worker.d.ts @@ -0,0 +1,61 @@ +import { Action, Store } from "unistore"; + +/** + * Actions object. + */ +export interface Actions { + [action: string]: Action; +} + +/** + * Types of actions received by registerActions. + */ +export type ActionRegister = ( + store: WorkerStore +) => Actions | Actions; + +/** + * Enhanced unistore store. + */ +export interface WorkerStore extends Store { + + /** + * Creates a new instance of WorkerStore. + */ + new(): WorkerStore; + + /** + * Retrieves the given action. + * @param action Action to retrieve (Worker actions take strings). + */ + action(action: Action | string): (...params: any[]) => void; + + /** + * List of registered actions. + */ + actions: Actions; + + /** + * Register new actions. + * @param newActions Object or callback function. + */ + registerActions(newActions: ActionRegister): void; + + /** + * Queue all additional processing until unfrozen. + */ + freeze(): void; + + /** + * Remove a freeze lock and process queued work + */ + unfreeze(): void; +} + +/** + * Creates a WebWorker unistore instance. + * @param initialState Initial state to populate. + */ +export default function createWorkerStore( + initialState: WorkerState +): WorkerStore;