fsm
fsm copied to clipboard
fsm.Event(ctx, ...) not work properly with context.WithTimeout()
When I use context.WithTimeout(ctx, time.Duration) with fsm.Event(ctx, ...) like:
Callbacks{
"enter_end": func(ctx context.Context, e *Event) {
go func() {
<-ctx.Done()
enterEndAsyncWorkDone = true
}()
},
},
the internal block exec immediately, not after timeout.
Interesting use case. I'll take a look at this. Are you constructing an FSM on the fly? A bit of pseudo code would help.
This is possible. You'll need to make the callback async. The example in https://github.com/looplab/fsm/blob/main/examples/cancel_async_transition.go shows how to use this. That example is geared towards canceling the transition but you can just as easily block on your timeout context and then decide what to do (cancel the transition, trigger an event on the state machine).