ifandelse / machina.js

js ex machina - finite state machines in JavaScript
http://machina-js.org/
Other
1.93k stars 147 forks source link

Wait for FSM to be in State #172

Open MaZderMind opened 2 years ago

MaZderMind commented 2 years ago

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', () = { … }).