Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entering next state before leaving previous, in 2 states at once? #91

Open
macasas opened this issue May 21, 2024 · 0 comments · May be fixed by #92
Open

Entering next state before leaving previous, in 2 states at once? #91

macasas opened this issue May 21, 2024 · 0 comments · May be fixed by #92

Comments

@macasas
Copy link

macasas commented May 21, 2024

Hi, trying to use your state package in a dart test project.
Code is below, but first here is the output with added comments as to where things are not as expected.
It's a traffic light in the UK, red, redamber, green, amber, red

Any suggestions as to what I am doing wrong would be grateful.

Entered: red
Exit: red
Entered: redamber
Exit: redamber
Entered: green.      // okay so far
Entered: amber     // but then enters amber before leaving green
Exit: green
Entered: red.         // then enters red before leaving amber
Exit: amber

Code

import 'package:state_machine/state_machine.dart';

init()  {
  StateMachine trafficlight = StateMachine('trafficlight');

  //states
  State red = trafficlight.newState('red');
  State redamber = trafficlight.newState('redamber');
  State amber = trafficlight.newState('amber');
  State green = trafficlight.newState('green');

  //transitions
  StateTransition readyToGo = trafficlight.newStateTransition('readyToGo', [red], redamber);
  StateTransition go = trafficlight.newStateTransition('go', [redamber], green);
  StateTransition readyToStop = trafficlight.newStateTransition('readyToStop', [green], amber);
  StateTransition stop = trafficlight.newStateTransition('stop', [amber], red);


  red.onEnter.listen((StateChange change) { 
    print('Entered: red'); 
    readyToGo();
  });
  red.onLeave.listen((StateChange change) { 
    print('Exit: red'); 
  });
  redamber.onEnter.listen((StateChange change) { 
    print('Entered: redamber'); 
    go();
  });
  redamber.onLeave.listen((StateChange change) { 
    print('Exit: redamber'); 
  });
  green.onEnter.listen((StateChange change) { 
    print('Entered: green'); 
    readyToStop();
  });
  green.onLeave.listen((StateChange change) { 
    print('Exit: green'); 
  });
  amber.onEnter.listen((StateChange change) { 
    print('Entered: amber'); 
    stop();
  });
  amber.onLeave.listen((StateChange change) { 
    print('Exit: amber'); 
  });


  // RUN
  trafficlight.start(red);
}

I also tried this format, but onDone is never called

  red.onEnter.listen(
    (StateChange change) { 
      print('Entered: red'); 
    },
    onDone: () {
      readyToGo();
    }
  );
@k415hu k415hu linked a pull request Jul 30, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant