redbloom
v1.1.4
Published
A predictable state container with pattern matched semantics
Downloads
29
Maintainers
Readme
RedBloom
- a minimal implementation of Redux;
- with an RxJS interface;
- built-in ImmutableJS store; and,
- pattern matching for dispatching actions.
Contents
Installation
npm install --save redbloom
Example
var redbloom = require('redbloom')();
redbloom.handle({ns: 'test', cmd: 'w/ payload'}, function(action, state) {
return state
.set('cmd', action.cmd)
.set('payload', action.payload)
.set('handler', 'w/ payload');
});
redbloom.handle({ns: 'test', cmd: 'w/o payload'}, function(action, state) {
return state
.set('cmd', action.cmd)
.set('payload', action.payload)
.set('handler', 'w/o payload');
});
redbloom.handle({ns: 'test'}, function(action, state) {
return state
.set('cmd', action.cmd)
.set('payload', action.payload)
.set('handler', 'default');
});
redbloom.subscribe(newState => console.log('newState:', newState));
redbloom.dispatch({ns: 'test', cmd: 'w/ payload', payload: 'payload'});
redbloom.dispatch({ns: 'test', cmd: 'w/o payload'});
redbloom.dispatch({ns: 'test'});
// OUTPUTS
// -------
// newState: Map { "cmd": "w/ payload", "payload": "payload", "handler": "w/ payload" }
// newState: Map { "cmd": "w/ payload", "payload": "payload", "handler": "default" }
// newState: Map { "cmd": "w/o payload", "payload": undefined, "handler": "w/o payload" }
// newState: Map { "cmd": "w/o payload", "payload": undefined, "handler": "default" }
// newState: Map { "cmd": undefined, "payload": undefined, "handler": "default" }
API
redbloom([initialState], [opts])
Creates a new instance of redbloom which is basically an RxJS Observable.
Options are:
indexing
: it can be eitherinsertion
(default) ordepth
;- if set to
insertion
, it will try to match entries in insertion order; - if set to
depth
, it will try to match entries with the most properties first.
- if set to
instance.handle(pattern, reducer)
Assigns a reducer for an action pattern.
pattern
: an object for which a dispatched action is matched to - e.g. the pattern{ns: 'namespace', cmd: 'command'}
would match the action{ns: 'namespace', cmd: 'command', payload: {key: 'value'}}
reducer
: ideally a pure function used for state transitions - e.g.function(action, currentState) { ... return newState; }
instance.dispatch(action)
Looks up the appropriate reducer for the action and applies it to the current state of the system.
action
: an object - e.g.{ns: 'namespace', cmd: 'command', payload: {key: 'value'}}