boxed-state

Implements a wrapper for immutable state with encapsulation of save()
and cancel()
implementations allowing making implementation independent use of transactional changes to the
encapsulated state.
Immutability is provided by the boxed-immutable
module
Install
Use npm to install.
npm install boxed-state --save
Usage

require('boxed-state')
function
Provides a boxed proxy to immutable state, with .save()
and .cancel()
methods for saving or
canceling state changes. With minimal code this allows transparent access to current state
without the callers worrying about stale data or how to apply the changes back to the state
holder.
Applying partial changes to component's state is as easy as setting a value in a boxState
instance and invoking .save()
const boxState = require('boxed-state');
function getSimpleState() {
}
let state_$;
function saveState(newState) {
state_$.cancel();
}
state_$ = boxState(() => {
return getSimpleState();
}, (modified, boxed) => {
saveState(modified);
});
function getState() {
return state_$;
}
function handleEvent() {
let state_$ = getState();
state_$.save();
}
Property | Get | Set | Delete | Call |
---|
save | function | error | error | calls the saveState callback passed to boxState function, callback only called if there were changes made to boxed object |
cancel | function | error | error | cancels any changes, next access will get new current state, returns undefined or delta which can be used to re-apply the changes |
boxState(getState, saveState, options)
const boxState = require('boxed-state').boxState;
const boxedState = boxState();
Used to construct a new boxed state proxy.
argument | default | Description |
---|
getState | none | callback to obtain the current state |
saveState | none | callback to save modified, arguments: (newState, boxedState), return value passed back to caller of save() . |
options | box | options to use. Can be a box as provided by boxedImmutable.box or boxedImmutable.createBox(), then all other options are set to defaults |
options
:
Option | Default | Description |
---|
box : | global box | Which box creation to use for each new boxed state object |
saveBoxedProp : | 'save' | name of the save property to use, allows changing of the function to 'commit' or something that will not conflict with your state's properties |
cancelBoxedProp : | 'cancel' | name of the cancel property to use, allows changing of the function to something that will not conflict with your state's properties |
getTransforms | undefined | get-transforms to use when creating a boxed state |
setTransforms | undefined | set-transforms to use when creating a boxed state |