Skip to main content

Engine

Struct Engine 

Source
pub struct Engine { /* private fields */ }
Expand description

A live script engine: state in scope, script functions in funcs.

Implementations§

Source§

impl Engine

Source

pub fn eval_value( &mut self, src: &str, locals: &[(String, Value)], ) -> Option<Value>

Evaluate an expression to a Value.

Source

pub fn eval_display(&mut self, src: &str, locals: &[(String, Value)]) -> String

Evaluate a {{ }} binding to its display string (empty on error).

Source

pub fn eval_bool(&mut self, src: &str, locals: &[(String, Value)]) -> bool

Evaluate a condition (r-if / r-elif / r-show).

Source

pub fn run_handler(&mut self, src: &str) -> bool

Run an @tap handler (statements or a function call). Returns whether it ran without error (assumed to have changed state).

Source

pub fn eval_value_tracked( &mut self, src: &str, locals: &[(String, Value)], ) -> (Option<Value>, HashSet<String>)

Evaluate an expression and report which signals it read, the binding’s dependency set. Only top-level signal names are returned; loop-locals and function parameters are filtered out. This is the read half of fine-grained reactivity: a binding subscribes to exactly the signals it touches.

Source

pub fn eval_display_tracked( &mut self, src: &str, locals: &[(String, Value)], ) -> (String, HashSet<String>)

Evaluate a {{ }} binding to its display string and report its signal deps (the tracked twin of eval_display).

Source

pub fn eval_bool_tracked( &mut self, src: &str, locals: &[(String, Value)], ) -> (bool, HashSet<String>)

Evaluate a condition and report its signal deps (the tracked twin of eval_bool).

Source

pub fn run_handler_tracked(&mut self, src: &str) -> HashSet<String>

Run an @tap handler and report which signals it changed, the write half. Detected by diffing the signal values across the run, so it needs no cooperation from the handler source (which is arbitrary rhai). Returns an empty set if the handler errored or changed nothing.

Source

pub fn set_route(&mut self, path: &str) -> bool

Put the current path in scope as the route signal.

A signal rather than anything router-shaped, so {{ route }}, r-if, :class and the change diff all understand navigation with no knowledge of the router at all. It is added to the signal set as well as the scope, or dependency tracking would filter reads of it out as a stray local and nothing would subscribe.

Returns whether the value actually moved, which is what tells the runtime there is anything to repaint.

Source

pub fn set_provided(&mut self, name: &str, value: Value) -> bool

Put one of the router’s other provided values in scope, the same way Self::set_route does with the path.

Returns whether it moved, so the runtime can skip a repaint nothing asked for: can_go_forward in particular is false through most of a session and would otherwise report a change on every navigation.

Source

pub fn declares(&self, name: &str) -> bool

Whether the script declared one of the router’s names itself, so the runtime can say so rather than silently overwriting it. Asked before the setters, which would otherwise make the answer always yes.

Source

pub fn get_string(&mut self, name: &str) -> String

Read a signal’s current value as a display string (for input r-model).

Source

pub fn get_string_in( &mut self, expr: &str, locals: &[(String, Value)], ) -> String

The same, with a row’s loop variables in scope.

An r-model is recorded as written, so one inside an r-for can mention the loop variable (items[item.at].note). Read without it, that is not an expression at all, and the field comes back empty.

Source

pub fn set_string(&mut self, name: &str, value: &str)

Set a signal to a string value (from input editing).

Source

pub fn init_scope(&mut self, script: &str) -> Vec<(String, Value)>

Run a component’s own top-level script in a scope of its own, and hand back the variables it declared: one instance’s private state.

The document’s script is not visible, which is the point. A component that could read the app’s signals by name would be coupled to the app it was first written for, and could not be used twice.

Source

pub fn run_scoped_handler( &mut self, src: &str, locals: &[(String, Value)], ) -> (Vec<(String, Value)>, HashSet<String>)

Run a handler inside a component instance, whose state is locals.

Returns the instance’s variables as they stand afterwards, and which of the document’s signals changed. Both matter: a handler in a component may touch its own state, a prop’s underlying signal, or both.

Reading the locals back before the scope is rewound is what makes a component’s state writable at all. Ordinary evaluation drops them, which is right for a {{ }} binding and wrong for a @tap.

Source

pub fn recompute(&mut self, name: &str, expr: &str) -> (bool, HashSet<String>)

Re-evaluate a computed’s expression and store the result under its name.

Returns whether the value actually changed, and what it read. Only a real change is reported, so a computed that lands on the same answer does not invalidate the bindings that read it: recomputing is cheap, rebuilding a subtree is not.

A computed is a signal like any other, because it is declared as a plain let in the script handed to rhai. That is what makes {{ total }} track it without anything else knowing computeds exist.

Source

pub fn run_effect_tracked( &mut self, src: &str, ) -> (HashSet<String>, HashSet<String>)

Run an effect body, reporting what it read and what it wrote.

Both halves are needed and neither can be inferred from the other: the reads say when to run it again, and the writes say what its running has invalidated. A handler only needs the writes, which is why this is not run_handler_tracked.

Source

pub fn assign_string( &mut self, target: &str, value: &str, locals: &[(String, Value)], ) -> HashSet<String>

Write a string into whatever an r-model names, and report which signals that changed.

An assignment rather than set_string, which can only set a scope variable called name: for anything but a bare signal (user.name, items[0].note) that quietly created a variable with a punctuation-filled name and left the real target untouched. Running it as script is also what lets a row’s loop variable be in scope.

Auto Trait Implementations§

§

impl !Freeze for Engine

§

impl !RefUnwindSafe for Engine

§

impl !Send for Engine

§

impl !Sync for Engine

§

impl !UnwindSafe for Engine

§

impl Unpin for Engine

§

impl UnsafeUnpin for Engine

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.