Update timer driver. This has exposed problems with pattern quoting and schema support.

This commit is contained in:
Tony Garnock-Jones 2021-12-03 15:38:02 +01:00
parent edbe7bcdac
commit 4ad2dd69a6
1 changed files with 25 additions and 40 deletions

View File

@ -1,69 +1,54 @@
/// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones <tonyg@leastfixedpoint.com> /// 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 message type PeriodicTick(intervalMS);
export assertion type TimeLaterThan(deadlineMS); 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 { react {
stop on asserted TimeLaterThan(+(new Date()) + ms) { at ds {
cb(); stop on asserted TimeLaterThan(+(new Date()) + ms) {
cb();
}
}
} }
}
} }
boot { export function boot(ds: Ref) {
spawn named 'timer/PeriodicTick' { spawn named 'timer/PeriodicTick' {
during Observe(PeriodicTick($intervalMS)) => at ds {
spawn named (preserves`PeriodicTick(${intervalMS})`) { during Observe(PeriodicTick($intervalMS)) =>
let handle: any | null = null; spawn linked named (preserves`PeriodicTick(${intervalMS})`) {
let finish: (() => void) | null = thisFacet.actor.dataspace.backgroundTask(); const thisFacet = Turn.activeFacet;
on start { thisFacet.preventInertCheck();
handle = setInterval(thisFacet.wrapExternal((thisFacet) => { const handle = setInterval(() => thisFacet.turn(() => {
send message PeriodicTick(intervalMS); send message PeriodicTick(intervalMS);
}), floatValue(intervalMS as any)); }), floatValue(intervalMS as any));
on stop clearInterval(handle);
} }
on stop { }
if (handle) {
clearInterval(handle);
handle = null;
}
if (finish) {
finish();
finish = null;
}
}
}
} }
spawn named 'timer/TimeLaterThan' { spawn named 'timer/TimeLaterThan' {
during Observe(TimeLaterThan($deadlineMS)) => at ds {
spawn named (preserves`TimeLaterThan(${deadlineMS})`) { during Observe(TimeLaterThan($deadlineMS)) =>
let handle: any | null = null; spawn linked named (preserves`TimeLaterThan(${deadlineMS})`) {
let finish: (() => void) | null = thisFacet.actor.dataspace.backgroundTask(); const thisFacet = Turn.activeFacet;
on start { thisFacet.preventInertCheck();
let delta = floatValue(deadlineMS as any) - (+(new Date())); let delta = floatValue(deadlineMS as any) - (+(new Date()));
handle = setTimeout(thisFacet.wrapExternal((thisFacet) => { let handle: any | null = setTimeout(() => thisFacet.turn(() => {
handle = null; handle = null;
if (finish) finish();
finish = null;
react { react {
assert TimeLaterThan(deadlineMS); assert TimeLaterThan(deadlineMS);
} }
}), delta); }), delta);
} on stop if (handle) {
on stop {
if (handle) {
clearTimeout(handle); clearTimeout(handle);
handle = null; handle = null;
} }
if (finish) {
finish();
finish = null;
}
} }
} }
} }
} }