This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
forked from looplab/fsm
-
Notifications
You must be signed in to change notification settings - Fork 1
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
annismckenzie
commented
Dec 12, 2021
This was referenced Dec 12, 2021
annismckenzie
force-pushed
the
allow-transitions-in-callbacks
branch
from
December 12, 2021 14:15
8ece084
to
e3dd346
Compare
annismckenzie
commented
Dec 12, 2021
annismckenzie
force-pushed
the
allow-transitions-in-callbacks
branch
from
December 12, 2021 15:03
7e09989
to
75ef044
Compare
annismckenzie
commented
Dec 12, 2021
annismckenzie
force-pushed
the
allow-transitions-in-callbacks
branch
2 times, most recently
from
December 12, 2021 15:34
1d81a5a
to
d8a0147
Compare
annismckenzie
force-pushed
the
allow-transitions-in-callbacks
branch
2 times, most recently
from
December 12, 2021 15:51
d8a0147
to
adf7a7d
Compare
This adds the possibility of "starting" a state machine and have it execute multiple state transitions in succession, given that no errors occur. The equivalent code without this change: ```go var errTransition error for errTransition == nil { transitions := request.FSM.AvailableTransitions() if len(transitions) == 0 { break } if len(transitions) > 1 { errTransition = errors.New("only 1 transition should be available") } errTransition = request.FSM.Event(transitions[0]) } if errTransition != nil { fmt.Println(errTransition) } ``` Arguably, that’s bad because of several reasons: 1. The state machine is used like a puppet. 2. The state transitions that make up the "happy path" are encoded outside the state machine. 3. The code really isn’t good. 4. There’s no way to intervene or make different decisions on which state to transition to next (reinforces bullet point 2). 5. There’s no way to add proper error handling. It is possible to fix a certain number of those problems but not all of them, especially 2 and 4 but also 1. The added test is green and uses both an enter state and an after event callback. No other test case was touched in any way (besides enhancing the context one that was added in the previous commit).
This adds a context and cancelation facility to the type `AsyncError`. Async state transitions can now be canceled by calling `CancelTransition` on the AsyncError returned by `fsm.Event`. The context on that error can also be handed off as described in looplab#77 (comment).
annismckenzie
force-pushed
the
allow-transitions-in-callbacks
branch
from
July 29, 2022 14:11
751a073
to
94fb353
Compare
Pull Request Test Coverage Report for Build 2761186635
💛 - Coveralls |
With looplab#82 merged I can now open a new PR against upstream – closing. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes looplab#43 and looplab#61.
This contains breaking changes, namely:
fsm.Event()
fromname string, ...args
toctx context.Context, name string, ...args
e *fsm.Event
toctx context.Context, e *fsm.Event
(refs Context in event handlers looplab/fsm#61)This allows state transitions to happen in enter state and after event callbacks:
fsm/fsm_test.go
Lines 672 to 711 in d8a0147