Skip to main content

plexus_ui_engine/
module.rs

1//! FRAME-12: **the module contract** — what every profile implements and
2//! the host mounts: `mount(target, { props, bus }) → { update(props),
3//! destroy() }`. In the page a module is an ES module (`esm/1` written by
4//! hand, `svelte/1` from FRAME-13) the host imports from its version's
5//! `components/<name>.js` and wraps ([`crate::entry`]'s `JsModule`); the
6//! rhx runtime (`rhx/1`, the tree and the folds) is the host's own arm of
7//! the same lifecycle in [`crate::swap`] — mounted, updated, swapped,
8//! fallen back and destroyed by the same rules.
9
10use crate::bus::Member;
11use crate::tree::Renderer;
12use serde_json::{Map, Value};
13
14/// The profile of the tree-and-Rhai runtime; a manifest entry that names
15/// no profile is one.
16pub const RHX: &str = "rhx/1";
17
18/// A component's code, ready to mount.
19pub trait Module<R: Renderer> {
20    /// Draws into `target` with `props`, speaking through `bus`; a module
21    /// that throws is refused with its words (and leaves `target` empty).
22    fn mount(&self, target: &R::Node, props: &Map<String, Value>, bus: Member) -> Result<Box<dyn ModuleInstance>, String>;
23    /// FRAME-30: its sheet's text — what the module exports as `css` (a
24    /// svelte/1 module's compiled `<style>`); none when it has none.
25    fn css(&self) -> Option<String> {
26        None
27    }
28}
29
30/// A mounted module.
31pub trait ModuleInstance {
32    /// New props: a route to the same component with other params.
33    fn update(&mut self, props: &Map<String, Value>);
34    /// Takes it out: its DOM, its subscriptions, its listeners.
35    fn destroy(self: Box<Self>);
36}