Use the new pattern quasiquotation support

This commit is contained in:
Tony Garnock-Jones 2021-12-09 18:55:53 +01:00
parent f81cf11ebd
commit a37879695e
1 changed files with 28 additions and 23 deletions

View File

@ -2,6 +2,7 @@
/// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
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;
}
}
}
}
}