Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/facett

facet-wrapped/src/sim.rs

//! **`simview`** — the population Simulate viewer ([`facett_simview::SimView`]).
//!
//! It fetches what it shows itself, from the bench's `/power` and `/sim/api` on the
//! page's own origin (see `facett_simview::contract`). So the fixture IS the live
//! viewer — on wasm over the browser's `fetch`; on a host build (the registry's own
//! tests) over the in-process mock, because there is no server there. Mount it by
//! name: `index.html?component=simview`.
//!
//! The data seam is a deep link into that live view, not a second data path:
//!
//! ```json
//! { "select": 4711 }
//! ```
//! opens the viewer with node `4711`'s ego network asked for in V.G (and lit in 3D
//! when it is a person). Anything else is a refusal.

use facett_core::Facet;
use serde_json::Value;

use crate::registry::{Entry, Registry, GRAPH};

/// Register the Simulate viewer.
pub(crate) fn register(r: &mut Registry) {
    r.register(
        Entry::new(
            "simview",
            "The population Simulate viewer: every person in 3D with the latest round lit, the selected node's ego network in V.G, Generate / Permutate 10% over /power.",
            || Box::new(facett_simview::SimView::live()),
        )
        .in_group(GRAPH)
        .with_data(simview),
    );
}

/// **`simview`** with a starting selection: `{"select": <node id>}`.
#[must_use]
pub fn simview(v: &Value) -> Option<Box<dyn Facet>> {
    let id = v.get("select").and_then(Value::as_i64)?;
    let mut s = facett_simview::SimView::live();
    s.select(id);
    Some(Box::new(s))
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn a_selection_deep_links_and_anything_else_is_refused() {
        let f = simview(&json!({ "select": 7 })).expect("a selection mounts");
        assert_eq!(f.state_json()["selected"], 7);
        assert!(simview(&json!({})).is_none());
        assert!(simview(&json!({ "select": "seven" })).is_none());
    }
}