vstirbu/fsm-as-promised

Cancelling a transition

Closed this issue · 3 comments

Hi,

Great library so far! I just have some doubts about the best way to cancel a transition from within the transition event(I.e: stay in the current state and do not enter the new one)? Will throwing an exception be enough? I got a bit mislead by the following documentation line, and would like to confirm.

⚠️ Unhandled errors may lead to inconsistent state machine.

Ie:

function cancelTransition(message) {
    throw new TransitionCancelled(message);
}
function onSomeTransition(options) {
    if (maxAllowed) {
        cancelTransition();
    }
    return options;
}

It all depends on what has happened in the promise chain before the error is thrown, and in which state the callback is executed.

For example, if some resource have been reserved on in onleaveFromState and the onSomeTransition throws error, you'll have to release the resources in the catch. This is just a naive example, but the idea behind "Unhandled errors may lead to inconsistent state machine" statement is that the developer has to handle those situation as the library does not do anything but flow control.

If this can be explained better, I'm all ears... :)

Got you, thanks! Somehow the comment made me think the internal logic of the library would be wrong if we dont handle the exceptions. Which is not the case. If I understood correctly your comment, then it is my own implemented logic that could result in a wrong state.

As a result of the unhandled exception the machine will always remain in the current state and wont perform the transition.

Yes, that is exactly the situation.