Update timer driver. This has exposed problems with pattern quoting and schema support.
This commit is contained in:
parent
edbe7bcdac
commit
4ad2dd69a6
|
@ -1,68 +1,53 @@
|
|||
/// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
/// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
|
||||
|
||||
import { preserves, Observe, Dataspace, floatValue, Facet } from "@syndicate-lang/core";
|
||||
import { preserves, Observe, floatValue, Turn, Ref } from "@syndicate-lang/core";
|
||||
|
||||
export message type PeriodicTick(intervalMS);
|
||||
export assertion type TimeLaterThan(deadlineMS);
|
||||
|
||||
export function sleep<T>(thisFacet: Facet<T>, ms: number, cb: () => void): void {
|
||||
export function sleep(ds: Ref, ms: number, cb: () => void): void {
|
||||
react {
|
||||
at ds {
|
||||
stop on asserted TimeLaterThan(+(new Date()) + ms) {
|
||||
cb();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boot {
|
||||
export function boot(ds: Ref) {
|
||||
spawn named 'timer/PeriodicTick' {
|
||||
at ds {
|
||||
during Observe(PeriodicTick($intervalMS)) =>
|
||||
spawn named (preserves`PeriodicTick(${intervalMS})`) {
|
||||
let handle: any | null = null;
|
||||
let finish: (() => void) | null = thisFacet.actor.dataspace.backgroundTask();
|
||||
on start {
|
||||
handle = setInterval(thisFacet.wrapExternal((thisFacet) => {
|
||||
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 {
|
||||
if (handle) {
|
||||
clearInterval(handle);
|
||||
handle = null;
|
||||
}
|
||||
if (finish) {
|
||||
finish();
|
||||
finish = null;
|
||||
}
|
||||
on stop clearInterval(handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
spawn named 'timer/TimeLaterThan' {
|
||||
at ds {
|
||||
during Observe(TimeLaterThan($deadlineMS)) =>
|
||||
spawn named (preserves`TimeLaterThan(${deadlineMS})`) {
|
||||
let handle: any | null = null;
|
||||
let finish: (() => void) | null = thisFacet.actor.dataspace.backgroundTask();
|
||||
on start {
|
||||
spawn linked named (preserves`TimeLaterThan(${deadlineMS})`) {
|
||||
const thisFacet = Turn.activeFacet;
|
||||
thisFacet.preventInertCheck();
|
||||
let delta = floatValue(deadlineMS as any) - (+(new Date()));
|
||||
handle = setTimeout(thisFacet.wrapExternal((thisFacet) => {
|
||||
let handle: any | null = setTimeout(() => thisFacet.turn(() => {
|
||||
handle = null;
|
||||
if (finish) finish();
|
||||
finish = null;
|
||||
react {
|
||||
assert TimeLaterThan(deadlineMS);
|
||||
}
|
||||
}), delta);
|
||||
}
|
||||
on stop {
|
||||
if (handle) {
|
||||
on stop if (handle) {
|
||||
clearTimeout(handle);
|
||||
handle = null;
|
||||
}
|
||||
if (finish) {
|
||||
finish();
|
||||
finish = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue