tangkhaiphuong/stateless

On firing undefined trigger: Trigger ... is valid for transition ... but a guard conditions are not met.

Closed this issue · 6 comments

IMO it seems to be a bug in defaultUnhandledTriggerAction as I'd rather expect

No valid leaving transitions are permitted from state

example code:

import { StateMachine } from 'stateless';

const transactionState = new StateMachine<string, string>('new');

transactionState.configure('new')
    .permit('init', 'ready');

transactionState.configure('ready')
    .permit('stop', 'stopped');

const withTransactionState = async () => {
    console.log('started withTransactionState', transactionState.state);
    await transactionState.fire('start')
        .catch(err => console.error('error firing transition init:', err));
};

withTransactionState().then(() => {
   console.log('executed withTransactionState');
});

BTW I like the simplicity of this project! :)

@ciekawy The exception throws from the invalid state from trigger "start" which doesn't define the transition from configure.
The initial state is "new" so you only fire the trigger "init" to move to state "ready" then fire trigger "stop" to move "stopped" state.

thats why I'd expect an exception reflecting what you stated - message stating Trigger 'start' is valid for transition from state 'new' suggests for me that there is nothing wrong with firing trigger start unless I am missing something.

@ciekawy I copy & try run your example and the program throw exception:

started withTransactionState new
error firing transition init: Error: Trigger 'start' is valid for transition from state 'new' but a guard conditions are not met. Guard descriptions: ''.
    at StateMachine.defaultUnhandledTriggerAction (d:\stateless-demo\node_modules\stateless\state-machine.js:662:19)
    at UnhandledTriggerAction.<anonymous> (d:\stateless-demo\node_modules\stateless\unhandled-trigger-action.js:59:93)
    at step (d:\stateless-demo\node_modules\stateless\unhandled-trigger-action.js:32:23)
    at Object.next (d:\stateless-demo\node_modules\stateless\unhandled-trigger-action.js:13:53)
    at d:\stateless-demo\node_modules\stateless\unhandled-trigger-action.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (d:\stateless-demo\node_modules\stateless\unhandled-trigger-action.js:3:12)
    at UnhandledTriggerAction.execute (d:\stateless-demo\node_modules\stateless\unhandled-trigger-action.js:53:16)
    at StateMachine.<anonymous> (d:\stateless-demo\node_modules\stateless\state-machine.js:497:75)
    at step (d:\stateless-demo\node_modules\stateless\state-machine.js:45:23)
executed withTransactionState

This is the correct behavior of stateless. If you don't want to get an exception. You can try to register an unhandled trigger

transactionState.onUnhandledTrigger((state, trigger, _) => { });

it definitely makes sens to see an error here. The only thing I am trying to say is that for me the error is misleading as it suggest the transition is defined but guard condition failed.

I would expect the other error which I found three lines later in the source code.

it definitely makes sens to see an error here. The only thing I am trying to say is that for me the error is misleading as it suggest the transition is defined but guard condition failed.

I would expect the other error which I found three lines later in the source code.

This porting from stateless C# so I tried to keep porting version same with original as well as possible.

got it. May be worth to verify the C# version and fill the bug there then...