Struct sauron_core::dom::Program

source ·
pub struct Program<APP, MSG>where
    MSG: 'static,{
    pub app: Rc<RefCell<APP>>,
    pub node_closures: Rc<RefCell<BTreeMap<usize, Vec<(&'static str, Closure<dyn FnMut(Event)>)>>>>,
    /* private fields */
}
Expand description

Program handle the lifecycle of the APP

Fields§

§app: Rc<RefCell<APP>>

holds the user application

§node_closures: Rc<RefCell<BTreeMap<usize, Vec<(&'static str, Closure<dyn FnMut(Event)>)>>>>

The closures that are currently attached to all the nodes used in the Application We keep these around so that they don’t get dropped (and thus stop working);

Implementations§

source§

impl<APP, MSG> Program<APP, MSG>where MSG: 'static, APP: Application<MSG> + 'static,

A node along with all of the closures that were created for that node’s events and all of it’s child node’s events.

source

pub fn create_text_node(txt: &str) -> Text

create a text node

source

pub fn create_dom_node(&self, vnode: &Node<MSG>) -> Node

Create and return a CreatedNode instance (containing a DOM Node together with potentially related closures) for this virtual node.

source

pub fn dispatch_mount_event(node: &Node)

dispatch the mount event, call the listener since browser don’t allow asynchronous execution of dispatching custom events (non-native browser events)

source

pub fn append_child_and_dispatch_mount_event(parent: &Node, child_node: &Node)

a helper method to append a node to its parent and trigger a mount event if there is any

source

pub fn set_element_attributes( &self, element: &Element, attrs: &[&Attribute<MSG>] )

set the element attribute

source

pub fn set_element_attribute(&self, element: &Element, attr: &Attribute<MSG>)

set the element attribute

Note: this is called in a loop, so setting the attributes, and style will not be on the same call, but on a subsequent call to each other. Using the if-else-if here for attributes, style, function_call.

source

pub fn add_event_listeners( &self, target: &EventTarget, event_listeners: Vec<Attribute<MSG>> ) -> Result<(), JsValue>

attach and event listener to an event target

source

pub fn add_event_listener( &self, event_target: &EventTarget, event_name: &str, listener: &Listener<Event, MSG> ) -> Result<Closure<dyn FnMut(Event)>, JsValue>

add a event listener to a target element

source

pub fn remove_element_attribute( element: &Element, attr: &Attribute<MSG> ) -> Result<(), JsValue>

remove element attribute, takes care of special case such as checked

source§

impl<APP, MSG> Program<APP, MSG>where MSG: 'static, APP: Application<MSG> + 'static,

source

pub fn convert_patch( &self, target_element: &Element, patch: &Patch<'_, MSG> ) -> DomPatch<MSG>

convert a virtual DOM Patch into a created DOM node Patch

source§

impl<APP, MSG> Program<APP, MSG>where MSG: 'static, APP: Application<MSG> + 'static,

source

pub fn new( app: APP, mount_node: &Node, action: MountAction, target: MountTarget ) -> Self

Create an Rc wrapped instance of program, initializing DomUpdater with the initial view and root node, but doesn’t mount it yet.

source

pub fn mount_node(&self) -> Node

return the node where the app is mounted into

source

pub fn append_to_mount(app: APP, mount_node: &Node) -> Self

Instantiage an app and append the view to the root_node

Example
struct App{}
let mount = document().query_selector("#app").ok().flatten().unwrap();
Program::append_to_mount(App{}, &mount);
source

pub fn replace_mount(app: APP, mount_node: &Node) -> Self

Creates an Rc wrapped instance of Program and replace the root_node with the app view

Example
struct App{}
let mount = document().query_selector(".container").ok().flatten().unwrap();
Program::replace_mount(App{}, &mount);
source

pub fn clear_append_to_mount(app: APP, mount_node: &Node) -> Self

clear the existing children of the mount first before appending

source

pub fn mount_to_body(app: APP) -> Self

Instantiate the app and then append it to the document body

Example
struct App{}
Program::mount_to_body(App{});
source

pub fn mount(&self)

each element and it’s descendant in the vdom is created into an actual DOM node.

source

pub fn update_dom(&self) -> Result<Measurements, JsValue>

update the browser DOM to reflect the APP’s view

source

pub fn update_dom_with_vdom( &self, new_vdom: Node<MSG> ) -> Result<usize, JsValue>

patch the DOM to reflect the App’s view

source

pub fn set_current_dom(&self, new_vdom: Node<MSG>)

replace the current vdom with the new_vdom.

source

pub fn inject_style_to_mount(&self, style: &str)

inject style element to the mount node

source§

impl<APP, MSG> Program<APP, MSG>where MSG: 'static, APP: Application<MSG> + 'static,

This will be called when the actual event is triggered. Defined in the DomUpdater::create_closure_wrap function

source

pub fn dispatch_multiple(&self, msgs: impl IntoIterator<Item = MSG>)

dispatch multiple MSG

source

pub fn dispatch(&self, msg: MSG)

dispatch a single msg

source§

impl<APP, MSG> Program<APP, MSG>where MSG: 'static, APP: Application<MSG> + 'static,

source

pub fn add_window_event_listeners(&self, event_listeners: Vec<Attribute<MSG>>)

attach event listeners to the window

source

pub fn on_resize<F>(&self, cb: F)where F: FnMut(i32, i32) -> MSG + Clone + 'static,

Creates a Cmd in which the MSG will be emitted whenever the browser is resized

source

pub fn on_resize_task<F>(cb: F) -> Task<MSG>where F: FnMut(i32, i32) -> MSG + Clone + 'static,

TODO: only executed once, since the Task Future is droped once done TODO: this should be a stream, instead of just one-time future a variant of resize task, but instead of returning Cmd, it is returning Task

source

pub fn on_hashchange<F>(&self, cb: F)where F: FnMut(String) -> MSG + 'static,

attached a callback and will be triggered when the hash portion of the window location url is changed

Trait Implementations§

source§

impl<APP, MSG> Clone for Program<APP, MSG>where MSG: 'static,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<APP, MSG> !RefUnwindSafe for Program<APP, MSG>

§

impl<APP, MSG> !Send for Program<APP, MSG>

§

impl<APP, MSG> !Sync for Program<APP, MSG>

§

impl<APP, MSG> Unpin for Program<APP, MSG>

§

impl<APP, MSG> !UnwindSafe for Program<APP, MSG>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.