Skip to content

Latest commit

 

History

History

docs

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Documentation

Main thread API

createStore(initialState, worker)

Create store

Arguments

  1. Initial store state [object]
  2. WebWorker instance Returns Store object which is responble for the state of your app

Example

import { createStore } from "worker-store";
const initState = { count: 0, news: [] };
const store = createStore({}, new Worker());

store.dispatch("INCREMENT");
store.subscribe(message => console.log("new state", message.state));

dispatch(action, payload)

Dispatches an action is the only way to trigger a state change.

Arguments

  1. Action name
  2. Payload

Example

connect()( ({dispatch}) => <button onClick={ dispatch('FETCH_NEWS' {id: 10})} )

Provider

Makes the store available to the connect() calls in the component hierarchy below.

Example

<Provider store={store}>
  <MyComponent />
</Provider>

connect

Connects a React/Preact component to the store.

Arguments

  1. mapStateToProps(state, [ownProps]): stateProps: If this argument is specified, the new component will subscribe to store updates. This means that any time the store is updated, mapStateToProps will be called. The results of mapStateToProps must be a plain object, which will be merged into the component’s props. If you don't want to subscribe to store updates, pass null or undefined in place of mapStateToProps.
const mapStateToProps = ( state, ownProps) => ({
    count: state.count,
});
const App = connect(mapStateToProps)(({count}) => <h1>{count}/h1>);

WebWorker API

runStore

Object with actions. Every action accepts current state and parameters passed from dispatch function

put

Can be used inside generator to update state

Parameters

  • new state [object]