2021-12-01 16:24:29 +00:00
|
|
|
/// SPDX-License-Identifier: GPL-3.0-or-later
|
2024-02-03 14:59:22 +00:00
|
|
|
/// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2023-12-01 19:53:18 +00:00
|
|
|
import { preserves, DoubleFloat, Observe, floatValue, Turn, Ref, Schemas, Dataspace } from "@syndicate-lang/core";
|
2021-12-09 17:55:53 +00:00
|
|
|
import { QuasiValue as Q } from "@syndicate-lang/core";
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2023-02-06 15:08:12 +00:00
|
|
|
export message type PeriodicTick(interval: DoubleFloat);
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2023-02-06 15:08:12 +00:00
|
|
|
export const LaterThan = Schemas.timer.LaterThan;
|
|
|
|
export type LaterThan = Schemas.timer.LaterThan;
|
|
|
|
|
|
|
|
export function sleep(ds: Ref, seconds: number, cb: () => void): void {
|
2021-12-03 14:38:02 +00:00
|
|
|
react {
|
|
|
|
at ds {
|
2023-02-06 15:08:12 +00:00
|
|
|
const deadline = +(new Date()) / 1000.0 + seconds;
|
|
|
|
stop on asserted LaterThan(deadline) {
|
2021-12-03 14:38:02 +00:00
|
|
|
cb();
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 08:26:27 +00:00
|
|
|
export function boot(ds = Dataspace.local) {
|
2021-01-29 18:56:12 +00:00
|
|
|
spawn named 'timer/PeriodicTick' {
|
2021-12-03 14:38:02 +00:00
|
|
|
at ds {
|
2021-12-09 17:55:53 +00:00
|
|
|
during Observe({
|
2023-02-06 15:08:12 +00:00
|
|
|
"pattern": :pattern PeriodicTick(\Q.lit($interval: DoubleFloat))
|
|
|
|
}) => spawn named (preserves`PeriodicTick(${interval})`) {
|
2021-12-09 17:55:53 +00:00
|
|
|
const thisFacet = Turn.activeFacet;
|
|
|
|
thisFacet.preventInertCheck();
|
|
|
|
const handle = setInterval(() => thisFacet.turn(() => {
|
2023-02-06 15:08:12 +00:00
|
|
|
send message PeriodicTick(interval);
|
|
|
|
}), floatValue(interval) * 1000.0);
|
2021-12-09 17:55:53 +00:00
|
|
|
on stop clearInterval(handle);
|
|
|
|
}
|
2021-12-03 14:38:02 +00:00
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 15:08:12 +00:00
|
|
|
spawn named 'timer/LaterThan' {
|
2021-12-03 14:38:02 +00:00
|
|
|
at ds {
|
2021-12-09 17:55:53 +00:00
|
|
|
during Observe({
|
2023-02-06 15:08:12 +00:00
|
|
|
"pattern": :pattern LaterThan(\Q.lit($deadline: DoubleFloat))
|
|
|
|
}) => spawn named (preserves`LaterThan(${deadline})`) {
|
2021-12-09 17:55:53 +00:00
|
|
|
const thisFacet = Turn.activeFacet;
|
|
|
|
thisFacet.preventInertCheck();
|
2023-02-06 15:08:12 +00:00
|
|
|
let delta = floatValue(deadline) * 1000.0 - (+(new Date()));
|
2021-12-09 17:55:53 +00:00
|
|
|
let handle: any | null = setTimeout(() => thisFacet.turn(() => {
|
|
|
|
handle = null;
|
|
|
|
react {
|
2023-02-06 15:08:12 +00:00
|
|
|
assert LaterThan(floatValue(deadline));
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
2021-12-09 17:55:53 +00:00
|
|
|
}), delta);
|
|
|
|
on stop if (handle) {
|
|
|
|
clearTimeout(handle);
|
|
|
|
handle = null;
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
2021-12-09 17:55:53 +00:00
|
|
|
}
|
2021-12-03 14:38:02 +00:00
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
|
|
|
}
|