syndicate-nim/src/syndicate/drivers/timers.nim

24 lines
753 B
Nim

# SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/[asyncdispatch, monotimes, times]
import preserves, preserves/records
import syndicate, syndicate/assertions
import ../../syndicate/protocols/schemas/timer
syndicate timerDriver:
spawn "timer":
during(observe(laterThan(?msecs))) do (msecs: float64):
let
now = getTime().toUnixFloat() * 1_000.0
period = msecs - now
if period > 0:
getCurrentFacet().beginExternalTask()
addTimer(period.int, oneshot = true) do (fd: AsyncFD) -> bool:
react: publish: laterThan(deadline)
getCurrentFacet().endExternalTask()
true
else:
react: publish: prsTimeLaterThan(deadline)