cerebral-operators
Common operators (actions) for Cerebral.
Usage
Data paths
Cerebral operators allow you to set, copy, unset or check values across multiple data sources and destinations. To simplify the mechanism of addressing these values cerebral operators uses URLs.
scheme:path
where scheme
can be one of:
input
- (readonly)state
- (readwrite)output
- (writeonly)
the path
is the location of data to get or set.
Cerebral will "pre-compile" these URLs into performant functions so that at run time the URL does not need to be parsed. See the Factory Helpers section below for information on how you can integrate these URLs into your own actions.
Examples
user name from the input (readonly) { user: { name: 'Brian' } }
input:user.name
user name from the root of the store
state:user.name
user name to the output (writeonly)
output:user.name
Action Factories
copy
Copies a value from input, global state or module state to output, global state or module state.
copy(from, to)
// copy serverSettings from input to the store at /settings getServerSettings success: error:
// copy newAccount from account module state to output ajax success: error:
debounce
debounce(time, continueChain, { terminateChain = [], immediate = true, throttle = true })
debounce can be used to limit the number a times an actionChain is called, for example on keyboard activity.
By default the first signal call will execute the continueChain immediately and the last call during the time
will execute at the end. To change this to only execute the most recent continueChain at the end, set the
options to { immediate: false }
.
It is also possible to pass a terminateChain
to the options which will be called whenever a signal is terminated.
filter
filter(path, compare, acceptChain = null, options = { discardedChain: [] })
filter accepts a path and compares the value at the path with compare
value.
If compare
is a function then it will be executed and should return a truthy/falsy value.
- compare function:
(value, context) => value === 'what I expected'
set
set(path, value)
getOptionsFromServer success: error:
throttle
throttle(time, continueChain, { terminateChain = [] })
throttle can be used to limit the number a times an actionChain is called, for example on keyboard activity.
It is also possible to pass a terminateChain
to the options which will be called whenever a signal is terminated.
toggle
toggle(path)
// toggle the menu between true and false
// toggle the switch between "On" and "Off"
unset
unset(path)
delay
delay(time, continueChain)
addItem
when
When can be used to check input or state for a specific value, truthy or falsy and then run an action chain when the condition is matched. To check multiple paths, see the operators section below. If no when.otherwise
condition is provided then an otherwise
output path will be created for you.
when(path, conditions = { 'true': when.truthy, 'false': when.otherwise })
when exports the following symbols
when.truthy
when.falsy
when.otherwise
// simple when using default outputs true: tryAgainLater false: doReload
// create custom output path nameslet whenUser = whenUser isLoggedIn: getPageData isUnknown: redirectToHome
// check for specific valueslet whenFormIsValid = validateForm whenFormIsValid valid: sendToServer invalid: showErrorSnackBarMessage
// check for specific values against an array of possible matches close: open: otherwise:
Contribute
Fork repo
npm install
npm start
runs dev mode which watches for changes and auto lints, tests and buildsnpm test
runs the testsnpm run lint
lints the codenpm run build
compiles es6 to es5