use std::collections::VecDeque;
use std::sync::Mutex;
use std::time::Duration;
use serde_json::{json, Value};
use crate::automation::now_ms;
pub const EVENT_CAP: usize = 1024;
struct Inner {
next: u64,
log: VecDeque<Value>,
}
pub struct EventLog {
inner: Mutex<Inner>,
#[cfg(not(target_arch = "wasm32"))]
cv: std::sync::Condvar,
started_ms: f64,
}
impl Default for EventLog {
fn default() -> Self {
Self::new()
}
}
impl EventLog {
pub fn new() -> Self {
Self {
inner: Mutex::new(Inner { next: 1, log: VecDeque::new() }),
#[cfg(not(target_arch = "wasm32"))]
cv: std::sync::Condvar::new(),
started_ms: now_ms(),
}
}
pub fn emit(&self, mut event: Value) {
let mut ev = self.inner.lock().unwrap_or_else(|p| p.into_inner());
event["seq"] = json!(ev.next);
event["t_ms"] = json!((now_ms() - self.started_ms).max(0.0) as u64);
ev.next += 1;
ev.log.push_back(event);
while ev.log.len() > EVENT_CAP {
ev.log.pop_front();
}
drop(ev);
#[cfg(not(target_arch = "wasm32"))]
self.cv.notify_all();
}
pub fn since(&self, since: u64, wait: Duration) -> Vec<Value> {
let deadline = now_ms() + wait.as_secs_f64() * 1000.0;
#[cfg_attr(target_arch = "wasm32", allow(unused_mut))]
let mut ev = self.inner.lock().unwrap_or_else(|p| p.into_inner());
loop {
let out: Vec<Value> = ev
.log
.iter()
.filter(|e| e.get("seq").and_then(Value::as_u64).is_some_and(|s| s > since))
.cloned()
.collect();
let now = now_ms();
if !out.is_empty() || now >= deadline || cfg!(target_arch = "wasm32") {
return out;
}
#[cfg(not(target_arch = "wasm32"))]
{
ev = self.cv.wait_timeout(ev, Duration::from_secs_f64((deadline - now) / 1000.0)).unwrap_or_else(|p| p.into_inner()).0;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn seq_cap_and_wait() {
let log = EventLog::new();
assert!(log.since(0, Duration::from_millis(10)).is_empty());
for i in 0..(EVENT_CAP + 5) {
log.emit(json!({"i": i}));
}
let all = log.since(0, Duration::ZERO);
assert_eq!(all.len(), EVENT_CAP);
assert_eq!(all[0]["seq"], json!(6));
assert_eq!(log.since(EVENT_CAP as u64 + 4, Duration::ZERO).len(), 1);
}
}