Controller

import {Controller} from 'cerebral'

const controller = Controller({
  // Defines the top level state
  state: {},

  // Defines the top level signals
  signals: {},

  // Defines the top level modules
  modules: {},

  // A map of state changes to run before instantiation,
  // where the key is the path and value is the state value
  stateChanges: {}
})

export default controller

Errors

import {Controller} from 'cerebral'
import {HttpProviderError} from '@cerebral/http'

const controller = Controller({
  catch: new Map([
    [HttpProviderError, someCustomCatchHandlerSequence],
    [Error, someCatchHandlerSequence]
  ])
})

export default controller

You can also define a global error handler. This error handler will be called no matter if an error is caught or not. Useful for tracking failures in production.

controller.on('error', (error) => {})

Methods

getState

Returns state from the state tree

const someState = controller.getState('some.state')

getSignal

Returns signal from Cerebral

const someSignal = controller.getSignal('some.signal')
// Run signal
someSignal({foo: 'bar'})

getModel

Returns the model (state tree) of Cerebral

const model = controller.getModel()

flush

Flushes out changes to UI based on recent state changes, can be forced

controller.flush()

runSignal

Allows you to run an arbitrary function tree definition

controller.runSignal('someSignal', [actionA, actionB], {foo: 'bar'})

addModule

Allows you to add modules to the controller after instantiation (lazy)

controller.addModule('someModule', module)

removeModule

Allows you to remove modules from the controller

controller.removeModule('someModule')

Events

initialized:model

Triggers when Cerebral model has initialized.

controller.on('initialized:model', () => {})

initialized

Triggers when Cerebral controller has initialized.

controller.on('initialized', () => {})

flush

Triggered whenever Cerebral flushes out changes to the UI. Passes a map of changes.

controller.on('flush', (changes) => {})

start

Triggered whenever Cerebral starts a signal execution.

controller.on('start', (execution, payload) => {})

end

Triggered whenever Cerebral ends a signal execution.

controller.on('end', (execution, payload) => {})

pathStart

Triggered whenever Cerebral starts execution a path in a signal

controller.on('pathStart', (execution, payload) => {})

pathEnd

Triggered whenever Cerebral ends execution a path in a signal

controller.on('pathEnd', (execution, payload) => {})

functionStart

Triggered whenever Cerebral starts executing an action.

controller.on('functionStart', (execution, functionDetails, payload) => {})

functionEnd

Triggered whenever Cerebral ends executing an action.

controller.on('functionEnd', (execution, functionDetails, payload) => {})

asyncFunction

Triggered whenever Cerebral executed an async action.

controller.on('asyncFunction', (execution, functionDetails, payload) => {})

parallelStart

Triggered whenever Cerebral executes actions in parallel.

controller.on('parallelStart', (execution, payload, functionsToResolveCount) => {})

parallelProgress

Triggered whenever Cerebral executes actions in parallel.

controller.on('parallelProgress', (execution, payload, functionsStillResolvingCount) => {})

parallelEnd

Triggered whenever Cerebral ends executing actions in parallel.

controller.on('parallelEnd', (execution, payload, functionsExecutedCount) => {})

remember

Triggered whenever Cerebral travels back in time. Passes the timestamp it travelled to.

controller.on('remember', (datetime) => {})