funky-store
extremely lightweight FSA-based functional store, for managing application or module state with:
- an action-triggered reducer for updating local state with pure functions:
(state, action) => State
- an action-triggered async effect for side-effects external to local state:
asapDispatch => (state, action) => void
,
where actions from the effects are asynchronously dispatched withasapDispatch
on the microtask queue at the end of the current run of the JavaScript event loop - an action-triggered side-effect-free trigger for dispatching actions synchronously:
(state, action) => void | false | Action | (void | false | Action)[]
when an action is dispatched to the store,
it first hits the reducer, then the effect, and finally the trigger.
when first called, the reducer receives an undefined
state:
it should fallback to a predefined initial state.
finally, although the effect runs before the trigger,
the actions it triggers are always dispatched after those from the trigger.
effect and trigger hence both operate from the same reduced state.
Example
run the example 2D-Breakout Game in a browser:
npm installnpm run example
API
run the tests with npm test
in node, or npm test:web
in the browser.
core API (231 bytes gzip)
the core API exposes the store factory, which returns a dispatch function for synchronously dispatching actions into the store:
S extends Readonly<S>, A extends StandardAction = StandardAction> reducer: Reducer<S, A>, effectFactory: EffectFactory<S, A>, trigger: Trigger<S, A>: Dispatcher<A> declare declare
utils (396 bytes gzip)
beyond the core store factory, this module also exposes a few helper functions, e.g. for specifying reducers, effects or triggers by action-type.
the example 2D-Breakout Game demonstrates usage of these utilities.
declare S extends Readonly<S>, A extends StandardAction = StandardAction, I extends Partial<Readonly<S>> = Partial<Readonly<S>>> declare declare
TypeScript
although this library is written in TypeScript, it may also be imported into plain JavaScript code: modern code editors will still benefit from the available type definition, e.g. for helpful code completion.
License: MIT
Copyright 2019 Stéphane M. Catala
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.