2023-03-09 00:34:26 +11:00
|
|
|
import pkg/questionable
|
|
|
|
|
import pkg/chronos
|
2024-01-23 18:35:03 +11:00
|
|
|
import ../logutils
|
|
|
|
|
import ./trackedfutures
|
2023-03-09 00:34:26 +11:00
|
|
|
|
2024-12-13 09:35:39 +07:00
|
|
|
{.push raises: [].}
|
2023-03-09 00:34:26 +11:00
|
|
|
|
|
|
|
|
type
|
|
|
|
|
Machine* = ref object of RootObj
|
|
|
|
|
state: State
|
|
|
|
|
running: Future[void]
|
|
|
|
|
scheduled: AsyncQueue[Event]
|
|
|
|
|
started: bool
|
2023-07-31 15:09:34 +10:00
|
|
|
trackedFutures: TrackedFutures
|
2025-01-21 21:54:46 +01:00
|
|
|
|
2023-03-09 00:34:26 +11:00
|
|
|
State* = ref object of RootObj
|
2023-06-05 10:48:06 +02:00
|
|
|
Query*[T] = proc(state: State): T
|
2024-12-13 09:35:39 +07:00
|
|
|
Event* = proc(state: State): ?State {.gcsafe, raises: [].}
|
2023-03-09 00:34:26 +11:00
|
|
|
|
2023-04-04 17:05:16 +10:00
|
|
|
logScope:
|
|
|
|
|
topics = "statemachine"
|
|
|
|
|
|
2023-07-31 15:09:34 +10:00
|
|
|
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.} =
|
2023-04-04 17:05:16 +10:00
|
|
|
raiseAssert "not implemented"
|
|
|
|
|
|
2023-03-09 00:34:26 +11:00
|
|
|
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 =
|
2023-12-07 01:16:36 +00:00
|
|
|
if machine.state.isNil:
|
2023-06-05 10:48:06 +02:00
|
|
|
none T
|
|
|
|
|
else:
|
|
|
|
|
some query(machine.state)
|
|
|
|
|
|
2023-03-09 00:34:26 +11:00
|
|
|
proc schedule*(machine: Machine, event: Event) =
|
|
|
|
|
if not machine.started:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
machine.scheduled.putNoWait(event)
|
|
|
|
|
except AsyncQueueFullError:
|
|
|
|
|
raiseAssert "unlimited queue is full?!"
|
|
|
|
|
|
2025-02-19 11:18:45 +11:00
|
|
|
method run*(
|
|
|
|
|
state: State, machine: Machine
|
|
|
|
|
): Future[?State] {.base, async: (raises: []).} =
|
2023-03-09 00:34:26 +11:00
|
|
|
discard
|
|
|
|
|
|
2024-12-13 09:35:39 +07:00
|
|
|
proc run(machine: Machine, state: State) {.async: (raises: []).} =
|
2025-02-19 11:18:45 +11:00
|
|
|
if next =? await state.run(machine):
|
|
|
|
|
machine.schedule(Event.transition(state, next))
|
2023-03-09 00:34:26 +11:00
|
|
|
|
2024-12-13 09:35:39 +07:00
|
|
|
proc scheduler(machine: Machine) {.async: (raises: []).} =
|
|
|
|
|
var running: Future[void].Raising([])
|
2024-10-30 21:40:17 +11:00
|
|
|
while machine.started:
|
2024-12-13 09:35:39 +07:00
|
|
|
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)
|
2024-12-18 14:39:03 +07:00
|
|
|
machine.trackedFutures.track(running)
|
2024-12-13 09:35:39 +07:00
|
|
|
except CancelledError:
|
|
|
|
|
break # do not propagate bc it is asyncSpawned
|
2023-03-09 00:34:26 +11:00
|
|
|
|
|
|
|
|
proc start*(machine: Machine, initialState: State) =
|
|
|
|
|
if machine.started:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if machine.scheduled.isNil:
|
|
|
|
|
machine.scheduled = newAsyncQueue[Event]()
|
2023-07-31 15:09:34 +10:00
|
|
|
|
2023-03-09 00:34:26 +11:00
|
|
|
machine.started = true
|
2024-12-18 14:39:03 +07:00
|
|
|
let fut = machine.scheduler()
|
|
|
|
|
machine.trackedFutures.track(fut)
|
2024-12-13 09:35:39 +07:00
|
|
|
machine.schedule(Event.transition(machine.state, initialState))
|
2023-03-09 00:34:26 +11:00
|
|
|
|
2025-05-29 08:57:05 +02:00
|
|
|
proc stop*(machine: Machine) {.async: (raises: []).} =
|
2023-03-09 00:34:26 +11:00
|
|
|
if not machine.started:
|
|
|
|
|
return
|
|
|
|
|
|
2023-09-29 14:33:08 +10:00
|
|
|
trace "stopping state machine"
|
|
|
|
|
|
2023-07-31 15:09:34 +10:00
|
|
|
machine.started = false
|
|
|
|
|
await machine.trackedFutures.cancelTracked()
|
2023-03-09 00:34:26 +11:00
|
|
|
|
2023-06-05 10:48:06 +02:00
|
|
|
machine.state = nil
|