The App
function creates the main Cerebral controller instance that manages your application state and logic.
import App from 'cerebral'
import main from './main' // The root module
const app = App(main, {
// Options (all optional)
devtools: null, // Debugger integration
throwToConsole: true, // Log errors to console
noRethrow: false, // Prevent rethrow of errors after handling
stateChanges: {}, // Initial state changes
returnSequencePromise: false, // Make sequences return promises
hotReloading: false // Handle hot reloading
})
Option | Type | Default | Description |
---|---|---|---|
devtools | Object/null | null | Debugger configuration |
throwToConsole | Boolean | true | Log caught errors to console |
noRethrow | Boolean | false | Prevent rethrowing errors after handling them |
stateChanges | Object | {} | Initial state changes to apply |
returnSequencePromise | Boolean | false | Make sequence executions return promises |
hotReloading | Boolean | false | Enable smart merging for hot reloading |
Gets state from a specific path.
// Get value at path
const user = app.getState('user')
// Get nested value
const username = app.getState('user.details.name')
// Get full state tree
const fullState = app.getState()
Gets a value using a tag.
import { state } from 'cerebral'
// Get state using tag
const user = app.get(state`user`)
// With dynamic path
const item = app.get(state`items.${itemId}`)
Returns a callable sequence from a specific path.
// Get a sequence
const authenticate = app.getSequence('auth.authenticate')
// Run the sequence with optional payload
authenticate({ username: 'user', password: 'pass' })
Returns all sequences from a module.
// Get all sequences from a module
const authSequences = app.getSequences('auth')
// Run a sequence from the collection
authSequences.authenticate({ username: 'user' })
Run an arbitrary sequence definition.
// Run an inline sequence
app.runSequence(
'myInlineSequence',
[setLoading(true), fetchData, setLoading(false)],
{ id: 123 }
)
// Run a predefined sequence by path
app.runSequence('auth.authenticate', {
username: 'user',
password: 'pass'
})
Add a module after app initialization.
import analyticsModule from './modules/analytics'
// Add a module at a specific path
app.addModule('analytics', analyticsModule)
// Add a nested module
app.addModule('settings.theme', themeModule)
Remove a module from the app.
// Remove a module
app.removeModule('analytics')
// Remove a nested module
app.removeModule('settings.theme')
Returns the model (state tree) of the app.
const model = app.getModel()
Triggers UI updates based on state changes.
// Standard flush
app.flush()
// Force flush regardless of changes
app.flush(true)
The app instance is an event emitter that you can subscribe to:
// Listen for a specific event
app.on('flush', (changes) => {
console.log('State changes:', changes)
})
// Remove an event listener
app.off('flush', myListener)
// Listen once for an event
app.once('initialized', () => {
console.log('App is ready!')
})
Event | Arguments | Description |
---|---|---|
initialized:model | - | Model has initialized |
initialized | - | App has fully initialized |
moduleAdded | path, module | A module has been added |
moduleRemoved | path, module | A module has been removed |
flush | changes | State changes have been applied to UI |
Event | Arguments | Description |
---|---|---|
start | execution, payload | Sequence execution started |
end | execution, payload | Sequence execution ended |
pathStart | execution, payload | Path execution started |
pathEnd | execution, payload | Path execution ended |
functionStart | execution, functionDetails, payload | Action execution started |
functionEnd | execution, functionDetails, payload, result | Action execution ended |
asyncFunction | execution, functionDetails, payload | Async action executed |
parallelStart | execution, payload, functionsToResolveCount | Parallel execution started |
parallelProgress | execution, payload, functionsStillResolvingCount | Parallel execution progress update |
parallelEnd | execution, payload, functionsExecutedCount | Parallel execution ended |
mutation | mutation | State mutation occurred |
remember | datetime | Time travel occurred (debugging) |
import App from 'cerebral'
import main from './main'
const app = App(main, {
devtools: process.env.NODE_ENV === 'development' && {
host: 'localhost:8585',
reconnect: true
}
})
// Wait for initialization
app.once('initialized', () => {
// App is ready to use
app.getSequence('app.initialize')()
// Listen for state changes
app.on('flush', (changes) => {
console.log('Changes:', changes)
})
})
See UniversalApp for server-side rendering support.