Skip to content

How to queue/auto-transition after entering state #139

Closed
@angularsen

Description

@angularsen

Hi, one issue I keep bumping into is - how do I queue or auto-transition to a state, from within transition event callback?

There are two related usecases for this:

  1. State machine should transition to the next state in a sequence of states, based on some condition
  2. Writing a unit tests for a state machine that may auto-transition to states, using onTransition callback to trigger transitions before the next queued transition takes effect

My simplified state flow is something like this:

IDLE--start()-->DETECT_FACES--detected(faces)-->IDENTIFY_FACES-->identified(persons)-->DETECT_FACES-->detectedInTheMeantime(faces)-->IDENTIFY_FACES...

DETECT_FACES kicks of a face detector worker that runs asynchronously and triggers face-detected events.
IDENTIFY_FACES starts an asynchronous process to identify faces that can take several seconds.

So far I've accomplished this by storing a buffer of facesDetectedInTheMeantime outside the state machine and using setTimeout() to invoke the transition upon entering DETECT_FACES, but this seems very hackish to me and probably not very deterministic. Ideally, there was a way to queue transitions or a lifecycle event that is invoked after the transition has fully completed so you can enter new states from it, but maybe that would need to use a mechanism similar to setTimeout and not really gain anything.

Am I way off on the way I am thinking here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions