Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

JS: State Machine

State Machine

Each gclass declares a finite state machine: a list of states, and for each state a table of rows with the form (event, action, next state).

Source code: src/gobj.js

const st_idle = [
    ["EV_CONNECT",    ac_connect,    "ST_CONNECTED"],
    ["EV_TIMEOUT",    ac_timeout,    null],           // null = stay in this state
];
const st_connected = [
    ["EV_DISCONNECT", ac_disconnect, "ST_IDLE"],
    ["EV_MESSAGE",    ac_message,    null],
];

const states = [
    ["ST_IDLE",      st_idle],
    ["ST_CONNECTED", st_connected],
];

When an event arrives, the framework reads the state that is active, finds the row of the event, calls the action, and goes to the next state. With a next state of null the gobj stays where it is.


gobj_current_state(gobj)

Gives the name of the state that is active.

gobj_change_state(gobj, state_name)

Changes the state without an event. Returns true when the state changes.

gobj_has_event(gobj, event, event_flag)

Tells if the gclass declares an event. Give 0 in event_flag to accept every flag.

gobj_has_output_event(gobj, event, event_flag)

Tells if the gclass declares an event as an output event.


event_flag_t

The flags of an event, in the table event_types of the gclass.

FlagDescription
EVF_NO_WARN_SUBSA publication with no subscriber writes no warning.
EVF_OUTPUT_EVENTThe gclass publishes this event.
EVF_SYSTEM_EVENTAn event of the framework.
const event_types = [
    ["EV_TIMEOUT_PERIODIC", event_flag_t.EVF_OUTPUT_EVENT|event_flag_t.EVF_NO_WARN_SUBS],
    [null, 0]
];

Use EVF_NO_WARN_SUBS when a subscriber is optional. It is the annotation that says “no subscriber is not a fault”, and it is not a way to make a message quiet. Keep the warning when the gclass really needs somebody to answer.


An event that the state does not declare

The framework writes an error. That is deliberate.

A state is also the way to say that something is not ready. A view whose whole life happens in one state, with a guard such as if(!priv.x) return, hides the fault: the button does nothing and nobody hears about it. Model the situation as a state, and the wrong event fails with the name of its sender.