diff --git a/packages/timer/src/index.ts b/packages/timer/src/index.ts index a10fb1a..8cfb7a5 100644 --- a/packages/timer/src/index.ts +++ b/packages/timer/src/index.ts @@ -2,6 +2,7 @@ /// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones import { preserves, Observe, floatValue, Turn, Ref } from "@syndicate-lang/core"; +import { QuasiValue as Q } from "@syndicate-lang/core"; export message type PeriodicTick(intervalMS); export assertion type TimeLaterThan(deadlineMS); @@ -19,36 +20,40 @@ export function sleep(ds: Ref, ms: number, cb: () => void): void { export function boot(ds: Ref) { spawn named 'timer/PeriodicTick' { at ds { - during Observe(PeriodicTick($intervalMS)) => - spawn linked named (preserves`PeriodicTick(${intervalMS})`) { - const thisFacet = Turn.activeFacet; - thisFacet.preventInertCheck(); - const handle = setInterval(() => thisFacet.turn(() => { - send message PeriodicTick(intervalMS); - }), floatValue(intervalMS as any)); - on stop clearInterval(handle); - } + during Observe({ + "pattern": :pattern PeriodicTick(\Q.lit($intervalMS)), + "observer": _, + }) => spawn linked named (preserves`PeriodicTick(${intervalMS})`) { + const thisFacet = Turn.activeFacet; + thisFacet.preventInertCheck(); + const handle = setInterval(() => thisFacet.turn(() => { + send message PeriodicTick(intervalMS); + }), floatValue(intervalMS)); + on stop clearInterval(handle); + } } } spawn named 'timer/TimeLaterThan' { at ds { - during Observe(TimeLaterThan($deadlineMS)) => - spawn linked named (preserves`TimeLaterThan(${deadlineMS})`) { - const thisFacet = Turn.activeFacet; - thisFacet.preventInertCheck(); - let delta = floatValue(deadlineMS as any) - (+(new Date())); - let handle: any | null = setTimeout(() => thisFacet.turn(() => { - handle = null; - react { - assert TimeLaterThan(deadlineMS); - } - }), delta); - on stop if (handle) { - clearTimeout(handle); - handle = null; + during Observe({ + "pattern": :pattern TimeLaterThan(\Q.lit($deadlineMS)), + "observer": _, + }) => spawn linked named (preserves`TimeLaterThan(${deadlineMS})`) { + const thisFacet = Turn.activeFacet; + thisFacet.preventInertCheck(); + let delta = floatValue(deadlineMS) - (+(new Date())); + let handle: any | null = setTimeout(() => thisFacet.turn(() => { + handle = null; + react { + assert TimeLaterThan(deadlineMS); } + }), delta); + on stop if (handle) { + clearTimeout(handle); + handle = null; } + } } } }