Skip to main content

Class: State

States are your Robo's personal memory bank.

import { State } from 'robo.js'

// Set a value in the state
State.set('key', 'value')

// Get a value from the state
const value = State.get('key')

States are ephemerally in-memory; data is lost when stopped but not when restarted.

Learn more: State Management

Constructors​

new State()​

new State(prefix, options?): State

Parameters​

ParameterType
prefixstring
options?StateOptions

Returns​

State

Methods​

fork()​

fork(prefix, options?): State

Creates a new state fork.

Parameters​

ParameterTypeDescription
prefixstringFork prefix (e.g. 'polls')
options?StateOptionsOptions for the fork (persisting all state by default)

Returns​

State

  • A new state fork you can deconstruct (e.g. const { getState, setState } = State.fork('polls')

getState()​

getState<T>(key): T

Get a value from the forked state. If the value does not exist, null is returned.

Type Parameters​

Type ParameterDefault type
Tstring

Parameters​

ParameterTypeDescription
keystringThe key to get the value for.

Returns​

T

The value for the given key, or null if the key does not exist.


setState()​

setState<T>(
key,
value,
options?): void

Set a value in the forked state. When the persist option is set to true, the state will be persisted to disk.

Type Parameters​

Type Parameter
T

Parameters​

ParameterTypeDescription
keystringThe key to set the value for.
valueTThe value to set.
options?SetStateOptionsOptions for setting the state. (Persisting to disk)

Returns​

void


fork()​

static fork(prefix, options?): State

Creates a new state fork. This is useful for preventing state collisions between different parts of the Robo.

Parameters​

ParameterTypeDescription
prefixstringFork prefix (e.g. 'polls')
options?StateOptionsOptions for the fork (persisting all state by default)

Returns​

State

A new state fork you can deconstruct (e.g. const { getState, setState } = State.fork('polls')


get()​

static get<T>(key, options?): T

Get a value from the forked state. If the value does not exist, null is returned.

Type Parameters​

Type ParameterDefault type
Tstring

Parameters​

ParameterTypeDescription
keystringThe key to get the value for.
options?GetStateOptions-

Returns​

T

The value for the given key, or null if the key does not exist.


listForks()​

static listForks(): string[]

Returns​

string[]


set()​

static set<T>(
key,
value,
options?): void

Set a value in the forked state. When the persist option is set to true, the state will be persisted to disk.

Type Parameters​

Type Parameter
T

Parameters​

ParameterTypeDescription
keystringThe key to set the value for.
valueTThe value to set.
options?SetStateOptionsOptions for setting the state. (Persisting to disk)

Returns​

void