pub struct Engine { /* private fields */ }Expand description
A live script engine: state in scope, script functions in funcs.
Implementations§
Source§impl Engine
impl Engine
Sourcepub fn eval_value(
&mut self,
src: &str,
locals: &[(String, Value)],
) -> Option<Value>
pub fn eval_value( &mut self, src: &str, locals: &[(String, Value)], ) -> Option<Value>
Evaluate an expression to a Value.
Sourcepub fn eval_display(&mut self, src: &str, locals: &[(String, Value)]) -> String
pub fn eval_display(&mut self, src: &str, locals: &[(String, Value)]) -> String
Evaluate a {{ }} binding to its display string (empty on error).
Sourcepub fn eval_bool(&mut self, src: &str, locals: &[(String, Value)]) -> bool
pub fn eval_bool(&mut self, src: &str, locals: &[(String, Value)]) -> bool
Evaluate a condition (r-if / r-elif / r-show).
Sourcepub fn run_handler(&mut self, src: &str) -> bool
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).
Sourcepub fn eval_value_tracked(
&mut self,
src: &str,
locals: &[(String, Value)],
) -> (Option<Value>, HashSet<String>)
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.
Sourcepub fn eval_display_tracked(
&mut self,
src: &str,
locals: &[(String, Value)],
) -> (String, HashSet<String>)
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).
Sourcepub fn eval_bool_tracked(
&mut self,
src: &str,
locals: &[(String, Value)],
) -> (bool, HashSet<String>)
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).
Sourcepub fn run_handler_tracked(&mut self, src: &str) -> HashSet<String>
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.
Sourcepub fn set_route(&mut self, path: &str) -> bool
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.
Sourcepub fn set_provided(&mut self, name: &str, value: Value) -> bool
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.
Sourcepub fn declares(&self, name: &str) -> bool
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.
Sourcepub fn get_string(&mut self, name: &str) -> String
pub fn get_string(&mut self, name: &str) -> String
Read a signal’s current value as a display string (for input r-model).
Sourcepub fn get_string_in(
&mut self,
expr: &str,
locals: &[(String, Value)],
) -> String
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.
Sourcepub fn set_string(&mut self, name: &str, value: &str)
pub fn set_string(&mut self, name: &str, value: &str)
Set a signal to a string value (from input editing).
Sourcepub fn init_scope(&mut self, script: &str) -> Vec<(String, Value)>
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.
Sourcepub fn run_scoped_handler(
&mut self,
src: &str,
locals: &[(String, Value)],
) -> (Vec<(String, Value)>, HashSet<String>)
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.
Sourcepub fn recompute(&mut self, name: &str, expr: &str) -> (bool, HashSet<String>)
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.
Sourcepub fn run_effect_tracked(
&mut self,
src: &str,
) -> (HashSet<String>, HashSet<String>)
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.
Sourcepub fn assign_string(
&mut self,
target: &str,
value: &str,
locals: &[(String, Value)],
) -> HashSet<String>
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.