From 6bad1fa2e87d7709c15891e06584e9b77a75f675 Mon Sep 17 00:00:00 2001 From: pancsta <155631569+pancsta@users.noreply.github.com> Date: Wed, 10 Jul 2024 22:37:43 +0200 Subject: [PATCH] feat(machine): add global AnyAny negotiation handler (#78) --- pkg/machine/machine_test.go | 5 +++++ pkg/machine/misc.go | 8 ++++++-- pkg/machine/transition.go | 11 +++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/pkg/machine/machine_test.go b/pkg/machine/machine_test.go index 23be82d..44d38d1 100644 --- a/pkg/machine/machine_test.go +++ b/pkg/machine/machine_test.go @@ -2007,3 +2007,8 @@ func TestOnEventCtxDispose(t *testing.T) { m.Dispose() <-m.WhenDisposed() } + +// TODO TestAnyAnyHandler +func TestAnyAnyHandler(t *testing.T) { + t.Skip() +} diff --git a/pkg/machine/misc.go b/pkg/machine/misc.go index b9a604e..b6bf715 100644 --- a/pkg/machine/misc.go +++ b/pkg/machine/misc.go @@ -498,8 +498,12 @@ func (e *emitter) dispose() { // ///// exception support // /////////////// -// Exception is the Exception state name -const Exception = "Exception" +const ( + // Exception is a name the Exception state. + Exception = "Exception" + // Any is a name of a meta state used in catch-all handlers. + Any = "Any" +) // ExceptionArgsPanic is an optional argument ["panic"] for the Exception state // which describes a panic within a Transition handler. diff --git a/pkg/machine/transition.go b/pkg/machine/transition.go index 0a777ea..80f02fb 100644 --- a/pkg/machine/transition.go +++ b/pkg/machine/transition.go @@ -264,7 +264,7 @@ func (t *Transition) emitEnterEvents() Result { // isCalled := slices.Contains(t.CalledStates(), toState) args := t.Mutation.Args - ret := t.emitHandler("Any", toState, "Any"+toState, args) + ret := t.emitHandler(Any, toState, Any+toState, args) if ret == Canceled { return ret } @@ -291,7 +291,7 @@ func (t *Transition) emitExitEvents() Result { return ret } } - ret = t.emitHandler(from, "Any", from+"Any", nil) + ret = t.emitHandler(from, Any, from+Any, nil) if ret == Canceled { return ret } @@ -354,6 +354,7 @@ func (t *Transition) emitEvents() Result { m.emit(EventTransitionStart, txArgs, nil) // NEGOTIATION CALLS PHASE (cancellable) + // FooFoo handlers if result != Canceled && t.Type() != MutationRemove { result = t.emitSelfEvents() @@ -369,6 +370,12 @@ func (t *Transition) emitEvents() Result { result = t.emitEnterEvents() } + // global AnyAny handler + ret := t.emitHandler(Any, Any, Any+Any, t.Mutation.Args) + if ret == Canceled { + return ret + } + // FINAL HANDLERS (non cancellable) if result != Canceled {