Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Alorel committed Sep 26, 2020
1 parent 928b899 commit 92d885b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ A set of utilities for running Redux in a web worker.

- [Installation](#installation)
- [Overview](#overview)
- [Usage](#usage)
- [Usage & Examples](#usage--examples)
- [Basic usage](#basic-usage)
- [Sending default state from the main thread](#sending-default-state-from-the-main-thread)
- [Adding Redux devtools support](#adding-redux-devtools-support)
- [Letting the worker provide the initial state](#letting-the-worker-provide-the-initial-state)
- [API](#api)
- [Worker](#worker)
- [Main thread](#main-thread)
Expand Down Expand Up @@ -64,7 +65,7 @@ a web worker so your main thread doesn't slow down the UI! The general process i
1. The main thread's store wrapper clones only the paths that changed and applies the diff to the new state object.
1. A change is emitted.

# Usage
# Usage & Examples

## Basic usage

Expand Down Expand Up @@ -160,6 +161,32 @@ const store = createWrappedStore({
});
```

## Letting the worker provide the initial state

```javascript
// index.js

import {resolveWrappedStore} from '@alorel/redux-off-main-thread/main-thread';
const worker = new Worker('/worker.js');

// Same options as createWrappedStore, but initialState & syncInitialState are not allowed
resolveWrappedStore({worker})
.then(store => {
store.dispatch({type: 'foo'});
})
```

```javascript
// worker.js

import {provideReduxOMTInitialState} from '@alorel/redux-off-main-thread/worker';

provideReduxOMTInitialState({someInitialState: 'foo'})
.then(() => {
// main thread promise resolved
});
```

# API

Typescript definitions are provided for clarity
Expand Down
6 changes: 6 additions & 0 deletions projects/core/main-thread/lib/resolveWrappedStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import {timeoutListener} from '../../common/timeoutListener';
import {createWrappedStore, CreateWrappedStoreInit} from './createWrappedStore';
import {WrappedStore} from './wrapped-store/WrappedStore';

/** Same as a regular {@link CreateWrappedStoreInit}, but with initialState & syncInitialState omitted */
export type ResolveWrappedStoreInit<S> = Omit<CreateWrappedStoreInit<S>, 'initialState' | 'syncInitialState'>;

/**
* Similar to {@link createWrappedStore}, but the store on the worker is used to provide the initial state via
* {@link provideReduxOMTInitialState}.
* @return A promise that resolves with a {@link WrappedStore} when the worker store is initialised.
*/
export function resolveWrappedStore<S, A extends Action>(
init: ResolveWrappedStoreInit<S>
): Promise<WrappedStore<S, A>> {
Expand Down
7 changes: 6 additions & 1 deletion projects/core/worker/lib/provideReduxOMTInitialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import '../../common/declarations';
import {createInitialStateEvent} from '../../common/InitialStateEvent';
import {isInitialStateRequestEvent} from '../../common/InitialStateRequestEvent';
import {ReduxOMTEvent} from '../../common/ReduxOMTEvent';
import {IS_ON_WORKER} from './support';
import {timeoutListener} from '../../common/timeoutListener';
import {IS_ON_WORKER} from './support';

/**
* Used to provide the initial state to a main thread worker initialised via {@link resolveWrappedStore}. This function
* should be called immediately on the worker entrypoint.
* @return A void promise that resolves once the initial state request has been fulfilled.
*/
export function provideReduxOMTInitialState<S = any>(state: S): Promise<void> {
if (!IS_ON_WORKER) {
return Promise.reject(new Error('provideReduxOMTInitialState can only be run on the worker thread'));
Expand Down

0 comments on commit 92d885b

Please sign in to comment.