Files
logos-storage-nim/storage/utils/asyncstatemachine.nim
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
2.6 KiB
Nim
Raw Normal View History

import pkg/questionable
import pkg/chronos
2024-01-23 18:35:03 +11:00
import ../logutils
import ./trackedfutures
{.push raises: [].}
type
Machine* = ref object of RootObj
state: State
running: Future[void]
scheduled: AsyncQueue[Event]
started: bool
trackedFutures: TrackedFutures
2025-01-21 21:54:46 +01:00
State* = ref object of RootObj
2023-06-05 10:48:06 +02:00
Query*[T] = proc(state: State): T
Event* = proc(state: State): ?State {.gcsafe, raises: [].}
logScope:
topics = "statemachine"
proc new*[T: Machine](_: type T): T =
T(trackedFutures: TrackedFutures.new())
2025-01-10 15:12:37 +01:00
method `$`*(state: State): string {.base, gcsafe.} =
raiseAssert "not implemented"
proc transition(_: type Event, previous, next: State): Event =
return proc(state: State): ?State =
if state == previous:
return some next
2023-06-05 10:48:06 +02:00
proc query*[T](machine: Machine, query: Query[T]): ?T =
if machine.state.isNil:
2023-06-05 10:48:06 +02:00
none T
else:
some query(machine.state)
proc schedule*(machine: Machine, event: Event) =
if not machine.started:
return
try:
machine.scheduled.putNoWait(event)
except AsyncQueueFullError:
raiseAssert "unlimited queue is full?!"
method run*(
state: State, machine: Machine
): Future[?State] {.base, async: (raises: []).} =
discard
proc run(machine: Machine, state: State) {.async: (raises: []).} =
if next =? await state.run(machine):
machine.schedule(Event.transition(state, next))
proc scheduler(machine: Machine) {.async: (raises: []).} =
var running: Future[void].Raising([])
while machine.started:
try:
let event = await machine.scheduled.get()
if next =? event(machine.state):
if not running.isNil and not running.finished:
trace "cancelling current state", state = $machine.state
await running.cancelAndWait()
let fromState =
if machine.state.isNil:
"<none>"
else:
$machine.state
machine.state = next
debug "enter state", state = fromState & " => " & $machine.state
running = machine.run(machine.state)
machine.trackedFutures.track(running)
except CancelledError:
break # do not propagate bc it is asyncSpawned
proc start*(machine: Machine, initialState: State) =
if machine.started:
return
if machine.scheduled.isNil:
machine.scheduled = newAsyncQueue[Event]()
machine.started = true
let fut = machine.scheduler()
machine.trackedFutures.track(fut)
machine.schedule(Event.transition(machine.state, initialState))
2025-05-29 08:57:05 +02:00
proc stop*(machine: Machine) {.async: (raises: []).} =
if not machine.started:
return
trace "stopping state machine"
machine.started = false
await machine.trackedFutures.cancelTracked()
2023-06-05 10:48:06 +02:00
machine.state = nil