qmuntal / stateless

Go library for creating finite state machines
https://wikipedia.org/wiki/Finite-state_machine
BSD 2-Clause "Simplified" License
972 stars 53 forks source link

No valid leaving transitions are permitted from state ... for trigger ... #63

Closed heiningair closed 1 year ago

heiningair commented 1 year ago

Hi,

So far, I followed the instructions on how to use this lib and cant get it running.

this is how i define the states and triggers.

    const (
        initial         = "initial"
        processing      = "processing"
        done            = "done"
    )
    const (
        TriggerProcessing        = "triggerProcessing"
        TriggerDone              = "triggerDone"
    )

this is how i configure the state machine...

                        stateMachine := stateless.NewStateMachine(initial)

            stateMachine.Configure(initial).Permit(TriggerProcessing, processing)

            stateMachine.Configure(processing).
                OnEntryFrom(err, func(ctx context.Context, args ...any) error {
                    // signal that the processing has started
                    println("processing started after error")
                    return nil
                }).
                OnEntryFrom(TriggerProcessing, func(ctx context.Context, args ...any) error {
                    // signal that the processing has started
                    println("processing started on uploading data")
                    return nil
                }).
                OnExit(func(ctx context.Context, args ...any) error {
                    // signal that the processing has finished
                    println("processing finished")
                    return nil
                }).
                Permit(TriggerDone, done)

                       stateMachine.Configure(done).
                OnEntry(func(ctx context.Context, args ...any) error {
                    // signal done
                    println("done")
                    return nil
                }).
                OnExit(func(ctx context.Context, args ...any) error {
                    // signal that it is leaving the done state
                    println("leaving done state")
                    return nil
                }).
                Permit(TriggerProcessing, processing)

when i then fire a trigger "TriggerProcessing" on the stateMachine, i get the following error:

unexpected error: stateless: No valid leaving transitions are permitted from state 'StateMachine {{ State = initial, PermittedTriggers = [triggerProcessing] }}' for trigger 'triggerProcessing', consider ignoring the trigger

Right now i really cant tell what goes wrong and i would appreciate your help very much. I simply followed the concept from the example..

I tried out multiple things to get rid of this issue, but it didnt help (Ignore, Guards, etc. )

heiningair commented 1 year ago

Sorry.. my bad.. I just used the stateless.NewStateMachineWithExternalStorage incorrectly