can always advance time to next timeout

This commit is contained in:
Joeri Exelmans 2025-10-13 12:00:36 +02:00
parent e233fa9166
commit 3cb3ef91d2

View file

@ -133,18 +133,25 @@ export function App() {
}, 20); }, 20);
let timeout: NodeJS.Timeout | undefined; let timeout: NodeJS.Timeout | undefined;
if (time.kind === "realtime" && rtIdx !== null) { if (rtIdx !== null) {
console.log('checking timers...');
const currentRt = rt[rtIdx]!; const currentRt = rt[rtIdx]!;
const timers = currentRt.environment.get("_timers") || []; const timers = currentRt.environment.get("_timers") || [];
if (timers.length > 0) { if (timers.length > 0) {
const [nextInterrupt, timeElapsedEvent] = timers[0]; const [nextInterrupt, timeElapsedEvent] = timers[0];
const wallclkDelay = getWallClkDelay(time, nextInterrupt, performance.now()); const raiseTimeEvent = () => {
console.log('scheduling timeout after', wallclkDelay);
timeout = setTimeout(() => {
const nextConfig = handleInputEvent(nextInterrupt, timeElapsedEvent, ast, currentRt); const nextConfig = handleInputEvent(nextInterrupt, timeElapsedEvent, ast, currentRt);
appendNewConfig('<timer>', nextInterrupt, nextConfig); appendNewConfig('<timer>', nextInterrupt, nextConfig);
}, wallclkDelay); }
if (time.kind === "realtime") {
const wallclkDelay = getWallClkDelay(time, nextInterrupt, performance.now());
// console.log('scheduling timeout after', wallclkDelay);
timeout = setTimeout(raiseTimeEvent, wallclkDelay);
}
else if (time.kind === "paused") {
if (nextInterrupt <= time.simtime) {
raiseTimeEvent();
}
}
} }
} }
@ -202,7 +209,7 @@ export function App() {
&emsp; &emsp;
{ast.inputEvents && {ast.inputEvents &&
<>raise&nbsp; <>raise&nbsp;
{[...ast.inputEvents].map(event => <button disabled={rtIdx===null} onClick={() => raise(event)}>{event}</button>)} {[...ast.inputEvents].map(event => <button title="raise input event" disabled={rtIdx===null} onClick={() => raise(event)}>{event}</button>)}
&emsp;</> &emsp;</>
} }
<input type="radio" name="paused" id="radio-paused" checked={time.kind==="paused"} disabled={rtIdx===null} onChange={e => onChangePaused(e.target.checked, performance.now())}/> <input type="radio" name="paused" id="radio-paused" checked={time.kind==="paused"} disabled={rtIdx===null} onChange={e => onChangePaused(e.target.checked, performance.now())}/>
@ -211,19 +218,30 @@ export function App() {
<label htmlFor="radio-realtime">real time</label> <label htmlFor="radio-realtime">real time</label>
&emsp; &emsp;
<label htmlFor="number-timescale">timescale</label>&nbsp; <label htmlFor="number-timescale">timescale</label>&nbsp;
<input type="number" min={0} id="number-timescale" disabled={rtIdx===null} value={timescale} style={{width:40}} onChange={e => onTimeScaleChange(e.target.value, performance.now())}/> <input title="controls how fast the simulation should run in real time mode - larger than 1 means: faster than wall-clock time" type="number" min={0} id="number-timescale" disabled={rtIdx===null} value={timescale} style={{width:40}} onChange={e => onTimeScaleChange(e.target.value, performance.now())}/>
&emsp; &emsp;
<label htmlFor="time">time (s)</label>&nbsp; <label htmlFor="time">time (s)</label>&nbsp;
<input id="time" disabled={rtIdx===null} value={displayTime} readOnly={true} className="readonlyTextBox" /> <input title="the current simulated time" id="time" disabled={rtIdx===null} value={displayTime} readOnly={true} className="readonlyTextBox" />
{nextTimedTransition && {nextTimedTransition &&
<> <>
&emsp; &emsp;
next timeout (s): <label htmlFor="next-timeout">next timeout (s)</label>&nbsp;
<input id="time" disabled={rtIdx===null} value={formatTime(nextTimedTransition[0])} readOnly={true} className="readonlyTextBox"/> <input id="next-timeout" disabled={rtIdx===null} value={formatTime(nextTimedTransition[0])} readOnly={true} className="readonlyTextBox"/>
<button>advance</button> <button title="advance time to the next timer elapse" onClick={() => {
const now = performance.now();
setTime(time => {
if (time.kind === "paused") {
return {kind: "paused", simtime: nextTimedTransition[0]};
}
else {
return {kind: "realtime", scale: time.scale, since: {simtime: nextTimedTransition[0], wallclktime: now}};
}
});
}}>advance</button>
</> </>
} }
</div> </div>
<div className="layout"> <div className="layout">
<main className="content"> <main className="content">
<VisualEditor {...{ast, setAST, rt: rt.at(rtIdx!), setRT, errors, setErrors}}/> <VisualEditor {...{ast, setAST, rt: rt.at(rtIdx!), setRT, errors, setErrors}}/>