use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::mpsc::{self, Receiver, Sender};
use std::time::{Duration, Instant};
use boa_engine::{Context, JsError};
use crate::{
events::HtmlEvent,
resources::AsyncResult,
tdom::node::{NodeArena, NodeId},
tui::graph_api::{DirtyInfo, RenderCmd},
};
use super::host::HostFn;
pub(crate) struct ScriptState {
pub arena: NodeArena,
pub root_id: NodeId,
pub render_cmds: Vec<RenderCmd>,
pub dirty: DirtyInfo,
pub current_event: Option<HtmlEvent>,
pub host_fns: HashMap<String, HostFn>,
pub timers: Vec<TimerEntry>,
pub next_timer_id: u32,
pub active_fetches: std::collections::HashSet<u32>,
pub next_fetch_id: u32,
pub fetch_rx: Receiver<AsyncResult>,
pub fetch_tx: Sender<AsyncResult>,
}
impl ScriptState {
pub(super) fn fetch_channel() -> (Sender<AsyncResult>, Receiver<AsyncResult>) {
mpsc::channel()
}
}
pub(crate) struct TimerEntry {
pub id: u32,
pub deadline: Instant,
pub period: Duration,
pub repeat: bool,
}
thread_local! {
pub(crate) static ENGINE_STATE: RefCell<Option<Rc<RefCell<ScriptState>>>> = const {RefCell::new(None)} ;
}
pub(super) type EngineParts = (Context, Rc<RefCell<ScriptState>>);
pub(super) type EngineBuildError = (JsError, HashMap<String, HostFn>);