plexus-ui-engine 0.1.3

The plexus-ui engine — signals, live() over Plexus streams, the view tree over a renderer, Rhai folds, sections, hot-swap
Documentation
//! FRAME-17: **the host's marks** — its steps on the page's clock, each a
//! `performance.mark` named `plexus-ui:<step>` (CONTRACT.md's host
//! chapter), read by a probe with `performance.getEntriesByType("mark")`:
//!
//! | mark | when |
//! |---|---|
//! | `plexus-ui:components-parsed` | the manifest and `components.json` read and handed to the registry |
//! | `plexus-ui:first-call` | the first snapshot a `live` asks |
//! | `plexus-ui:handlers-compiled:<component>` | a component's handlers compiled — when it is mounted, not before |
//! | `plexus-ui:section-mounted:<slot>` | a section of the route's layout filled at boot |
//! | `plexus-ui:layout-mounted` | the route's component drawn at boot, its sections filled (FRAME-50: the last in its turn) |
//! | `plexus-ui:first-answer` | the first snapshot answered whole (its `done`) |
//! | `plexus-ui:first-row` | the first answered rows on the page |
//! | `plexus-ui:answered:<snapshot>`, `plexus-ui:drawn:<snapshot>` | each snapshot method's first answer with rows, and its rows on the page — `drawn` − `answered` is the fold and the draw |
//! | `plexus-ui:walk:<layout>` | FRAME-18: a module layout's element walked for its section marks — after its mount, after each `update`, and on each change its observer saw outside its filled sections |
//!
//! Natively (the tests) a mark is kept in a list ([`taken`]).

use std::cell::RefCell;
use std::collections::BTreeSet;

thread_local! {
    static ONCE: RefCell<BTreeSet<String>> = const { RefCell::new(BTreeSet::new()) };
    static TAKEN: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
}

/// Marks `plexus-ui:<step>` now.
pub fn mark(step: &str) {
    let name = format!("plexus-ui:{step}");
    #[cfg(all(target_arch = "wasm32", feature = "dom"))]
    if let Some(p) = web_sys::window().and_then(|w| w.performance()) {
        let _ = p.mark(&name);
    }
    TAKEN.with(|t| t.borrow_mut().push(name));
}

/// The page's clock (`performance.now()`); natively a count.
pub fn now() -> f64 {
    #[cfg(all(target_arch = "wasm32", feature = "dom"))]
    if let Some(p) = web_sys::window().and_then(|w| w.performance()) {
        return p.now();
    }
    TAKEN.with(|t| t.borrow().len() as f64)
}

/// [`once`], dated `at` on the page's clock (`performance.mark(name, {
/// startTime })`): what happened before the step that knows it happened.
pub fn once_at(step: &str, at: f64) {
    if !ONCE.with(|o| o.borrow_mut().insert(step.to_string())) {
        return;
    }
    let name = format!("plexus-ui:{step}");
    #[cfg(all(target_arch = "wasm32", feature = "dom"))]
    if let Some(p) = web_sys::window().and_then(|w| w.performance()) {
        let opts = js_sys::Object::new();
        let _ = js_sys::Reflect::set(&opts, &"startTime".into(), &at.into());
        if let Ok(f) = js_sys::Reflect::get(&p, &"mark".into()) {
            let _ = js_sys::Function::from(f).call2(&p, &name.clone().into(), &opts);
        }
    }
    let _ = at;
    TAKEN.with(|t| t.borrow_mut().push(name));
}

/// Marks `plexus-ui:<step>` the first time only (a page's first call,
/// answer, row; a boot's sections).
pub fn once(step: &str) {
    if ONCE.with(|o| o.borrow_mut().insert(step.to_string())) {
        mark(step);
    }
}

/// Whether `plexus-ui:<step>` was marked by [`once`].
pub fn marked(step: &str) -> bool {
    ONCE.with(|o| o.borrow().contains(step))
}

/// Every mark taken on this thread, in order.
pub fn taken() -> Vec<String> {
    TAKEN.with(|t| t.borrow().clone())
}