[utils] Add AsyncState to state machine

This commit is contained in:
Mark Spanbroek 2022-09-27 16:36:20 +02:00 committed by Eric Mastro
parent b52d291785
commit 7841a9692c
1 changed files with 16 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import pkg/questionable
import pkg/chronos
import ./optionalcast
## Implementation of the the state pattern:
@ -84,3 +85,18 @@ proc switch*(machine: StateMachine, newState: State) =
proc switch*(oldState, newState: State) =
if context =? oldState.context:
context.switch(newState)
type
AsyncState* = ref object of State
method enterAsync(state: AsyncState) {.base, async.} =
discard
method exitAsync(state: AsyncState) {.base, async.} =
discard
method enter(state: AsyncState) =
asyncSpawn state.enterAsync()
method exit(state: AsyncState) =
asyncSpawn state.exitAsync()