Activity

An activity is the depiction of a long running process, that happens in the world outside of the statechart. Contrast with action which is similar, but instantaneous.

Activities are mostly tied to the idea of being in a state. When the state machine enters a particular state, any activities that are defined for that state should be started, and likewise, when the state machine exits such a state, they should be stopped.

Activities generally start immediately upon entering the state, and continue for as long as the activity can do so. If the activity stops before the state is exited, the stopping of the activity is often treated as an event that the state machine can react to. It is however not strictly required that the state exits when an activity stops.

Activities being active or not is also a case for a guard condition.

Examples

An example of an activity might be to show a particular user interface component. The user interface component would be visible for a long time, and might generate events that cause state transitions. When the state exits, the user interface component would no longer be shown.

Another example of an activity would be an active HTTP request. This is a long running, and might result in guards functions responding differently, and trigger events that the statechart might be interested in.

Notation

Activities in a state are prefixed with do / as follows:

Diagram depicting do handlers

SCXML

SCXML has no depiction on activities. Instead, use start and stop actions to control long running processes. <invoke> elements are similar to activities in that they allow the control of (and interaction with) a process external to the state machine.

XState

XState has native support for activities. By declaring that an activity should be happening in a particular state, XState will translate this into actions to start and stop activities, and provide a set of booleans indicating which activities should be running. To specify activities, use the activities property:

{
  activities: "http_request"
}

state = machine.transition(...);
state.activities; // { "http_request": true }

When entering a state with an activity, xstate also includes an action of type ‘xstate.start’ to indicate that an activity has just started.

XState also supports the invoke technique with the ability to “invoke” a function providing a callback mechanism, a Promise, or a different machine.

SCION-CORE

SCION core, which is heavily inspired by SCXML also has no depiction of activites. Instead, use start and stop actions, or invoke to control long running processes.