Skip to main content

plexus_ui_engine/
marks.rs

1//! FRAME-17: **the host's marks** — its steps on the page's clock, each a
2//! `performance.mark` named `plexus-ui:<step>` (CONTRACT.md's host
3//! chapter), read by a probe with `performance.getEntriesByType("mark")`:
4//!
5//! | mark | when |
6//! |---|---|
7//! | `plexus-ui:components-parsed` | the manifest and `components.json` read and handed to the registry |
8//! | `plexus-ui:first-call` | the first snapshot a `live` asks |
9//! | `plexus-ui:handlers-compiled:<component>` | a component's handlers compiled — when it is mounted, not before |
10//! | `plexus-ui:section-mounted:<slot>` | a section of the route's layout filled at boot |
11//! | `plexus-ui:layout-mounted` | the route's component drawn at boot, its sections filled (FRAME-50: the last in its turn) |
12//! | `plexus-ui:first-answer` | the first snapshot answered whole (its `done`) |
13//! | `plexus-ui:first-row` | the first answered rows on the page |
14//! | `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 |
15//! | `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 |
16//!
17//! Natively (the tests) a mark is kept in a list ([`taken`]).
18
19use std::cell::RefCell;
20use std::collections::BTreeSet;
21
22thread_local! {
23    static ONCE: RefCell<BTreeSet<String>> = const { RefCell::new(BTreeSet::new()) };
24    static TAKEN: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
25}
26
27/// Marks `plexus-ui:<step>` now.
28pub fn mark(step: &str) {
29    let name = format!("plexus-ui:{step}");
30    #[cfg(all(target_arch = "wasm32", feature = "dom"))]
31    if let Some(p) = web_sys::window().and_then(|w| w.performance()) {
32        let _ = p.mark(&name);
33    }
34    TAKEN.with(|t| t.borrow_mut().push(name));
35}
36
37/// The page's clock (`performance.now()`); natively a count.
38pub fn now() -> f64 {
39    #[cfg(all(target_arch = "wasm32", feature = "dom"))]
40    if let Some(p) = web_sys::window().and_then(|w| w.performance()) {
41        return p.now();
42    }
43    TAKEN.with(|t| t.borrow().len() as f64)
44}
45
46/// [`once`], dated `at` on the page's clock (`performance.mark(name, {
47/// startTime })`): what happened before the step that knows it happened.
48pub fn once_at(step: &str, at: f64) {
49    if !ONCE.with(|o| o.borrow_mut().insert(step.to_string())) {
50        return;
51    }
52    let name = format!("plexus-ui:{step}");
53    #[cfg(all(target_arch = "wasm32", feature = "dom"))]
54    if let Some(p) = web_sys::window().and_then(|w| w.performance()) {
55        let opts = js_sys::Object::new();
56        let _ = js_sys::Reflect::set(&opts, &"startTime".into(), &at.into());
57        if let Ok(f) = js_sys::Reflect::get(&p, &"mark".into()) {
58            let _ = js_sys::Function::from(f).call2(&p, &name.clone().into(), &opts);
59        }
60    }
61    let _ = at;
62    TAKEN.with(|t| t.borrow_mut().push(name));
63}
64
65/// Marks `plexus-ui:<step>` the first time only (a page's first call,
66/// answer, row; a boot's sections).
67pub fn once(step: &str) {
68    if ONCE.with(|o| o.borrow_mut().insert(step.to_string())) {
69        mark(step);
70    }
71}
72
73/// Whether `plexus-ui:<step>` was marked by [`once`].
74pub fn marked(step: &str) -> bool {
75    ONCE.with(|o| o.borrow().contains(step))
76}
77
78/// Every mark taken on this thread, in order.
79pub fn taken() -> Vec<String> {
80    TAKEN.with(|t| t.borrow().clone())
81}