ConstraintJS - v0.10.2
    Preparing search index...

    Class FSM

    Note: the preferred way to create an FSM is with cjs.fsm(...stateNames).

    A finite-state machine, to track the state of an interface or component.

    var fsm = cjs.fsm("idle", "active")
    .addTransition("idle", "active", cjs.on("click"))
    .addTransition("active", "idle", cjs.on("timeout", 1000));
    Index
    • Parameters

      • ...stateNames: (string | readonly string[])[]

        The FSM's states. The first one is the starting state (see startsAt).

      Returns FSM

    state: Constraint<string | null>

    A constraint whose value is the name of the current state.

    var my_fsm = cjs.fsm("state1", "state2");
    my_fsm.state.get(); // 'state1'
    • Adds states. The first state added becomes the current state; the last one becomes the state that addTransition starts from when no from state is given.

      Parameters

      • ...names: string[]

      Returns this

      var fsm = cjs.fsm()
      .addState('state1')
      .addState('state2')
      .addTransition('state1', cjs.on('click'));
    • Adds a transition from the last state added (see addState) to to, and returns a function that runs it.

      Parameters

      • to: string

      Returns (...eventArgs: unknown[]) => void

      var x = cjs.fsm("b", "a");
      var run_transition = x.addTransition("b"); // a -> b
      window.addEventListener("click", run_transition);
    • Adds a transition from the last state added (see addState) to to, run by trigger.

      Parameters

      Returns this

      var x = cjs.fsm("b", "a");
      x.addTransition("b", cjs.on('click')); // a -> b when the window is clicked
      x.addTransition("a", function(run_transition) { // b -> a when the window is clicked
      window.addEventListener("click", run_transition);
      });
    • Adds a transition from from to to, and returns a function that runs it.

      Parameters

      • from: string
      • to: string

      Returns (...eventArgs: unknown[]) => void

      var x = cjs.fsm("a", "b");
      var run_transition = x.addTransition("a", "b");
      window.addEventListener("click", run_transition);
    • Adds a transition from from to to, run by trigger.

      Parameters

      Returns this

      var x = cjs.fsm("a", "b");
      x.addTransition("a", "b", cjs.on("click"));
    • Removes every state, transition, and listener. Useful for cleaning up memory.

      Returns void

    • The name of the current state. Constraints that read it are updated when it changes.

      Returns string | null

      var my_fsm = cjs.fsm("state1", "state2");
      my_fsm.getState(); // 'state1'
    • Whether the current state is state. Constraints that call this are updated when the state changes.

      Parameters

      Returns boolean

      var my_fsm = cjs.fsm("a", "b");
      my_fsm.is("a"); // true, because a is the starting state
    • Calls callback when the FSM enters a state or runs a transition. spec can be:

      • '*': any state
      • 'state1': a state named state1 (or a comma-separated list: 'state1, state2')
      • 'state1 -> state2': right after state1 transitions to state2
      • 'state1 >- state2': right before state1 transitions to state2
      • 'state1 <-> state2': right after any transition between state1 and state2
      • 'state1 >-< state2': right before any transition between state1 and state2
      • 'state1 <- state2': right after state2 transitions to state1
      • 'state1 -< state2': right before state2 transitions to state1
      • 'state1 -> *': any transition from state1
      • '* -> state2': any transition to state2

      Parameters

      Returns this

      off

      var x = cjs.fsm("a", "b");
      x.on("a->b", function() {...});
    • Sets the state this FSM starts in (unless it has already transitioned).

      Parameters

      • name: string

      Returns this

      var my_fsm = cjs.fsm("state_a", "state_b");
      my_fsm.startsAt("state_b");