From 7623b5becd3073902e07f74b14656206af02d983 Mon Sep 17 00:00:00 2001 From: serxa Date: Wed, 20 Nov 2024 19:15:51 +0000 Subject: [PATCH] add realtime timers to EventSimulator --- utils/merge-selector-lab/EventSimulator.js | 39 ++++++++++++++++++++++ utils/merge-selector-lab/customScenario.js | 5 +++ 2 files changed, 44 insertions(+) diff --git a/utils/merge-selector-lab/EventSimulator.js b/utils/merge-selector-lab/EventSimulator.js index 5ad90ce0a16..551b4623624 100644 --- a/utils/merge-selector-lab/EventSimulator.js +++ b/utils/merge-selector-lab/EventSimulator.js @@ -52,6 +52,9 @@ export class EventSimulator this.ready_events = []; // Events ready to be executed this.pending_events = new Heap((a, b) => a.time - b.time); // Use min Heap for pending events this.time = 0; + this.timers = []; + this.last_timer = 0; + this.min_timer_interval_ms = 100; // At least check every 100 ms } // Schedule new event to be executed at specific time @@ -83,11 +86,47 @@ export class EventSimulator this.scheduleAt(0, name, callback, []); } + // Register a callback to be called every `interval` ms in realtime (not simulation) + addTimer(interval_ms, callback) + { + this.timers.push({ + last: performance.now(), + interval_ms, + callback, + }); + this.min_timer_interval_ms = Math.min(this.min_timer_interval_ms, interval_ms); + } + + // Handle timers + async #handleTimers() + { + if (this.timers.length > 0) + { + const now = performance.now(); + if (this.last_timer + this.min_timer_interval_ms < now) + { + this.last_timer = now; + for (const timer of this.timers) + { + if (timer.last + timer.interval_ms < now) + { + timer.last = now; + await timer.callback(); + } + } + } + } + } + // Run the simulation async run() { + const startTime = performance.now(); while (this.ready_events.length > 0 || this.pending_events.size() > 0) + { await this.#executeNextEvent(); + await this.#handleTimers(); + } } // All dependencies of an event are now satisfied diff --git a/utils/merge-selector-lab/customScenario.js b/utils/merge-selector-lab/customScenario.js index 1ce66f56aae..9b490168b60 100644 --- a/utils/merge-selector-lab/customScenario.js +++ b/utils/merge-selector-lab/customScenario.js @@ -20,6 +20,11 @@ export async function customScenario(scenario, signals) await inserter.start(); await merger.start(); + // Make timer + const {every_real_second: on_every_real_second} = signals; + if (on_every_real_second) + sim.addTimer(1000, async () => await on_every_real_second({mt, sim})); + // Run the simulation await sim.run();