# SPDX-FileCopyrightText: ☭ Emery Hemingway # SPDX-License-Identifier: Unlicense import std/[asyncdispatch, monotimes, times] import preserves import syndicate import ../protocols/timer from syndicate/protocols/dataspace import Observe export timer type Observe = dataspace.Observe proc now: float64 = getTime().toUnixFloat() proc spawnTimers*(turn: var Turn; ds: Cap): Actor {.discardable.} = ## Spawn a timer actor. spawn("timer", turn) do (turn: var Turn): during(turn, ds, inject(grab Observe(pattern: dropType LaterThan), {0: grabLit()})) do (seconds: float): let period = seconds - now() if period < 0.001: discard publish(turn, ds, LaterThan(seconds: seconds)) else: addCallback(sleepAsync(period * 1_000), turn) do (turn: var Turn): discard publish(turn, ds, LaterThan(seconds: seconds)) proc after*(turn: var Turn; ds: Cap; dur: Duration; act: TurnAction) = ## Execute `act` after some duration of time. let later = now() + dur.inMilliseconds.float64 * 1_000.0 onPublish(turn, ds, grab LaterThan(seconds: later)): act(turn) # TODO: periodic timer