@jessemc98/simple-store v0.0.1
Table of Contents
Install
$ npm install --save @jessemc98/simple-storeThen with a module bundler like rollup or webpack, use as you would anything else:
// using ES6 modules
import createStore from '@jessemc98/simple-store'
// using CommonJS modules
var createStore = require('@jessemc98/simple-store')Basic Usage
function numberReducer(state, event) {
switch (event.type) {
case "increment":
return state + 1
case "decrement":
return state - 1
default:
return state
}
}
const subscribers = { number: numberReducer }
const initialState = { number: 10 }
const store = createStore(subscribers, initialState)
store.emit({type: "increment"})
// expect(store.getState().number).toEqual(11)
store.emit({type: "decrement"})
// expect(store.getState().number).toEqual(10)API
createStore
simple-store is an experimental way to deal with state in javascript, inspired by redux.
Returns simple-store
Parameters
reducersObject Required. Object full of Reducer functions.initialStateObject Object with the same shape as reducers parameter with initial values to set to state.
emit
Calls all reducers in store with event and current state and sets the new state to the value returned by the reducers.
Parameters
eventCustom event object Required. An event object to emit.
getState
Returns the current state of the store. Do not mutate any values of the returned object directly, emit events whenever you want to cause a state change otherwise subscribers will not fire.
subscribe
Subscribe to a given key in state.
Parameters
keyString Required. Key in state to watch for changes.callbackFunction Handler function to call with new value ofstate[key]whenever its value changes.
unsubscribe
Unsubscribe previously added subscriber from key in state.
Parameters
keyString Required. Key in state to removecallbackfrom.callbackFunction Handler function to remove from any subscribers ofstate[key].
Custom event object
Object you need to pass to emit method. The only limitation is the type property is required and must be a String but otherwise it can be any javascript object with any properties.
Properties
typeString Required. Type of event to emit.
Reducer function
A js function which is called with state and event every time an event is emitted. State is set to the return value when a reducer is called.
If state is mutated but the same object is returned subscribers will not be called so if state changes the function should return a new object.
Injected parameters
stateany js value Current state.eventCustom event Object Theeventwhich was emitted that caused reducers to be called.
9 years ago