eonarheim / TypeState

A strongly typed finite state machine for TypeScript
http://eonarheim.github.io/TypeState/example/
BSD 2-Clause "Simplified" License
272 stars 28 forks source link

onEnter default return value to true #31

Closed stefnotch closed 5 years ago

stefnotch commented 5 years ago

onEnter expects a boolean return value. If it's false, TypeState does not enter the state. If it's true TypeState enters the state.

However, if it's undefined, which happens if the user simply didn't return anything, TypeState will interpret it as false and thus, not enter the state. This is usually not what the user intended to happen.

Example:

fsm.onEnter(Elevator.DoorsClosed, (from: Elevator)=>{
   setupGroovyElevatorMusic();
   // Since this doesn't return true, the doors will never close
});

My suggestion would be to default to true if the return value is undefined. However, in some cases this could also result in undesired behaviour.

eonarheim commented 5 years ago

I think the default of true here is probably more desired given your example. Another option may be to require an explicit return of boolean from the callback to warn users at compile time, which avoid the change in default behavior but gives the developer warning.