From 7841a9692c2228d0b8fcd2cae1ed21de21fa9820 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 27 Sep 2022 16:36:20 +0200 Subject: [PATCH] [utils] Add AsyncState to state machine --- codex/utils/statemachine.nim | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/codex/utils/statemachine.nim b/codex/utils/statemachine.nim index 12898e5f..9b7a4309 100644 --- a/codex/utils/statemachine.nim +++ b/codex/utils/statemachine.nim @@ -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()