machina.js icon indicating copy to clipboard operation
machina.js copied to clipboard

Wait for FSM to be in State

Open MaZderMind opened this issue 2 years ago • 0 comments

I read through the Docs for a way to externally wait for the FSM to reach a specific state (for example wait for an idle-State before tearing the connection down). This would either run directly if the FSM is already in this state or defer for the next transition to that state.

I had to do this in consumer-Code like this:

  function disconnectOnIdle() {
    …
  }

  if(connection.state === 'idle') {
    disconnectOnIdle();
  }
  else {
    console.log('Waiting for Idle-State…');
    connection.on("transition", data => {
      if(data.toState === 'idle') {
        disconnectOnIdle();
      }
    });
  }

But I think this Code is rather convoluted and such a requirement should be supported by the FSM class. by providing a Callback-Method like connection.onState('idle', () = { … }).

MaZderMind avatar Mar 06 '22 10:03 MaZderMind