Skip to content

App State

The DevCore includes a state management service that serves as a centralized state store for all the components in the application.

The state service allows arbitrary data to be centrally stored and shared between app components. One or more State stores can be created for different state.

The primary advantages of using a centralized state store is the predictable ordering of state changes and the uncoupling of components that need to share state.

State Stores

The DevCore provides the following state stores:

NameDescription
appState data regarding the app in general
authUser authentication data
configConfiguration data from the ioto.json5 and other config files
contextApplication specific state and data

Accessing State

js
import {State} from '@embedthis/devcore'

let darkMode = State.app.dark

Creating a State Store

You typically should not need to create a new store and can store your application state in the State.context object.

If you do wish to create a new state store, create a state class and use the Store.add to add the store to the centralized state service.

State stores are available as properties on the State object.

js
import {Store, State} from '@embedthis/devcore'
class MyState {
    temp = 0
    speed: 42
    increment() {
        temp++
    }
    get speed() { return this.speed }
    set speed(v) { this.speed = v }
    async stop() {
        await database.stop()
    }
}
Store.add('my', new MyState())

console.log(State.my.temp)
State.my.stop()
console.log(State.my.speed)

The App Store

The App store holds application global state including:

PropertyDescription
brightModeCurrent app dark/light mode
darkTrue if in dark mode
displayCurrent active display
fullDisplayFull set of all possible displays
managerReference to the app manager object
mobileTrue if the app is running on a mobile device
prefersHash of user preferences such as the current cloud or device
schemaDevice cloud schema
startedWhen the app session started

Auth State

The Auth state store holds authentication state including:

PropertyDescription
accountAccount object
accountNameAccount name
accountIdAccount ID
authAuthentication tokens
authenticatedBoolean set to true when the user credentials are authenticated
authorizedBoolean set to true when the user is fully logged in
emailUser's login email address
readySet to true when the user is logged in and the app is ready to start
roleUser's authenticated role
userUser object
usernameUser login name
userIdUser ID

Config State

The Config store holds general config state including:

PropertyDescription
apiBackend cloud service API
cognitoCognito config
buildApp build number
profileExecution profile (prod
nameApp name
titleApp display title
versionApp version (x.y.z)

The Context Store

The Context store is a special store that is used to store application specific state and data, including:

PropertyDescription
agentnavigator.userAgent
desktopwindow.innerWidth
heightwindow.innerWidth
inputsWidget input form values
languagenavigator.language
mobilewindow.innerWidth
widthwindow.innerWidth

Alternatives

If you are building your own custom build of the DevCore, you can also use other state stores such as Pinia for your own state management.