fluxury
Overview
State management library; works like redux but with option for many stores, side effects, Promises and other practical things.
Library includes:
- createStore(key, reducerOrSpec, actionsOrSelectors)
- dispatch(action)
- getStores()
- getReducer()
- getState()
- promiseAction(type, data)
- replaceState(state)
- subscribe(cb)
Quick start
npm install --save fluxury
; // creates a key="A" in the root store, connected to a reducer function.let storeA = ; let storeB = ; // Store with dependencies on state in storeA and storeB.let storeC = ; ;;;; // -> { a1: 2, b1: 2, c1: 4 }
Polyfills
This library depends on a modern JavaScript runtime. Load a polyfill like in core-js or babel-polyfill to support old browsers.
Manually install required polyfills with core-js:
;;;;
API
createStore( key, reducerOrSpec, actionsOrSelectors )
A store responds to actions by returning the next state.
const inc = 'inc'; // a simple counting storevar store =
Optionally, you may define a store with a specification.
const inc = "inc";; // a simple counting storevar countStore = ; // object spec makes action creators automatically...countStore;countStore;
dispatch( action )
The entry point to effecting state changes in the app is when an action is dispatch.
Dispatch accepts action as object, promise, or type/data; returns promise.
// Import the dispatch function.var dispatch = // Dispatch action as object // Dispatch action as promise // Dispatch action with type:string and data:object.
Store Properties
Here is a list of store properties that are part of the public API.
name | comment |
---|---|
name | The name of the store |
dispatch | Access to dispatch function |
dispatchToken | A number used to identity the store |
subscribe | A function to tegister a listener |
getState | A function to access state |
setState | Replace the store's state |
replaceReducer | Replace the store's reducer |
getStores( )
Returns an object with the name as key and store as value.
replaceState( state )
Rehydrate the root state.
;
subscribe( listener )
Listen to changes to all stores. This will trigger once each time createStore or dispatch is invoked.
Please note that action will be undefined when createStore is invoked.
var unsubscribe = subscribe( (state, action) => {
// got change
})
// stop listening
unsubscribe()
getReducer( )
Return app's reducer function, use with Redux.