Skip to content

Latest commit

 

History

History
53 lines (30 loc) · 1.02 KB

README.md

File metadata and controls

53 lines (30 loc) · 1.02 KB

channel-store

A channel-event middleware to create an ambient state that functions similar to redux but without the large amount of boilerplate and verbosity.

Bundlephobia gzip + minified

Installation

yarn add channel-store

npm install channel-store

Use

import { createHub } from "channel-event";
import { createStoreMiddleware, IStoreEvents } from "channel-store";

export interface AppStore {
   count: number
}


const hub = createHub(...);

const store = createStoreMiddleware<AppStore>(hub)
                  .addDefaultState({ count: 4 })
                  .build();


const channel = hub.newChannel<IStoreEvents>();

channel.send(storeEvents.UPDATE_STATE, state => ({ count: state.count + 1 }))

To read the store

const state = store.getState();

// or
const state = channel.send(storeEvents.GET_STATE);


// listen to state change
channel.listen(storeEvents.STATE_UPDATED, data => {
   const newState = data.payload;
});