Handle CancelledErrors for state.run in one place only
This commit is contained in:
parent
1019d97633
commit
4a4880f8b7
|
@ -59,11 +59,8 @@ proc onError(machine: Machine, error: ref CatchableError): Event =
|
||||||
state.onError(error)
|
state.onError(error)
|
||||||
|
|
||||||
proc run(machine: Machine, state: State) {.async.} =
|
proc run(machine: Machine, state: State) {.async.} =
|
||||||
try:
|
if next =? await state.run(machine):
|
||||||
if next =? await state.run(machine):
|
machine.schedule(Event.transition(state, next))
|
||||||
machine.schedule(Event.transition(state, next))
|
|
||||||
except CancelledError:
|
|
||||||
discard
|
|
||||||
|
|
||||||
proc scheduler(machine: Machine) {.async.} =
|
proc scheduler(machine: Machine) {.async.} =
|
||||||
var running: Future[void]
|
var running: Future[void]
|
||||||
|
@ -76,12 +73,14 @@ proc scheduler(machine: Machine) {.async.} =
|
||||||
let fromState = if machine.state.isNil: "<none>" else: $machine.state
|
let fromState = if machine.state.isNil: "<none>" else: $machine.state
|
||||||
machine.state = next
|
machine.state = next
|
||||||
debug "enter state", state = fromState & " => " & $machine.state
|
debug "enter state", state = fromState & " => " & $machine.state
|
||||||
try:
|
running = machine.run(machine.state)
|
||||||
running = machine.run(machine.state).track(machine)
|
running
|
||||||
except CancelledError as e:
|
.track(machine)
|
||||||
trace("run cancelled in state, swallowing", state = $machine.state)
|
.cancelled(proc() = trace "state.run cancelled, swallowing", state = $machine.state)
|
||||||
except CatchableError as e:
|
.catch(proc(err: ref CatchableError) =
|
||||||
machine.schedule(machine.onError(e))
|
trace "error caught in state.run, calling state.onError", state = $machine.state
|
||||||
|
machine.schedule(machine.onError(err))
|
||||||
|
)
|
||||||
|
|
||||||
proc start*(machine: Machine, initialState: State) =
|
proc start*(machine: Machine, initialState: State) =
|
||||||
if machine.started:
|
if machine.started:
|
||||||
|
|
Loading…
Reference in New Issue