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,
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.
sourcepub fn create_text_node(txt: &str) -> Text
pub fn create_text_node(txt: &str) -> Text
create a text node
sourcepub fn create_dom_node(&self, vnode: &Node<MSG>) -> Node
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.
sourcepub fn dispatch_mount_event(node: &Node)
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)
sourcepub fn append_child_and_dispatch_mount_event(parent: &Node, child_node: &Node)
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
sourcepub fn set_element_attributes(
&self,
element: &Element,
attrs: &[&Attribute<MSG>]
)
pub fn set_element_attributes( &self, element: &Element, attrs: &[&Attribute<MSG>] )
set the element attribute
sourcepub fn set_element_attribute(&self, element: &Element, attr: &Attribute<MSG>)
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.
sourcepub fn add_event_listeners(
&self,
target: &EventTarget,
event_listeners: Vec<Attribute<MSG>>
) -> Result<(), JsValue>
pub fn add_event_listeners( &self, target: &EventTarget, event_listeners: Vec<Attribute<MSG>> ) -> Result<(), JsValue>
attach and event listener to an event target
source§impl<APP, MSG> Program<APP, MSG>where
MSG: 'static,
APP: Application<MSG> + 'static,
impl<APP, MSG> Program<APP, MSG>where MSG: 'static, APP: Application<MSG> + 'static,
sourcepub fn convert_patch(
&self,
target_element: &Element,
patch: &Patch<'_, MSG>
) -> DomPatch<MSG>
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,
impl<APP, MSG> Program<APP, MSG>where MSG: 'static, APP: Application<MSG> + 'static,
sourcepub fn new(
app: APP,
mount_node: &Node,
action: MountAction,
target: MountTarget
) -> Self
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.
sourcepub fn mount_node(&self) -> Node
pub fn mount_node(&self) -> Node
return the node where the app is mounted into
sourcepub fn append_to_mount(app: APP, mount_node: &Node) -> Self
pub fn append_to_mount(app: APP, mount_node: &Node) -> Self
sourcepub fn replace_mount(app: APP, mount_node: &Node) -> Self
pub fn replace_mount(app: APP, mount_node: &Node) -> Self
sourcepub fn clear_append_to_mount(app: APP, mount_node: &Node) -> Self
pub fn clear_append_to_mount(app: APP, mount_node: &Node) -> Self
clear the existing children of the mount first before appending
sourcepub fn mount_to_body(app: APP) -> Self
pub fn mount_to_body(app: APP) -> Self
sourcepub fn mount(&self)
pub fn mount(&self)
each element and it’s descendant in the vdom is created into an actual DOM node.
sourcepub fn update_dom(&self) -> Result<Measurements, JsValue>
pub fn update_dom(&self) -> Result<Measurements, JsValue>
update the browser DOM to reflect the APP’s view
sourcepub fn update_dom_with_vdom(
&self,
new_vdom: Node<MSG>
) -> Result<usize, JsValue>
pub fn update_dom_with_vdom( &self, new_vdom: Node<MSG> ) -> Result<usize, JsValue>
patch the DOM to reflect the App’s view
sourcepub fn set_current_dom(&self, new_vdom: Node<MSG>)
pub fn set_current_dom(&self, new_vdom: Node<MSG>)
replace the current vdom with the new_vdom
.
sourcepub fn inject_style_to_mount(&self, style: &str)
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,
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
sourcepub fn dispatch_multiple(&self, msgs: impl IntoIterator<Item = MSG>)
pub fn dispatch_multiple(&self, msgs: impl IntoIterator<Item = MSG>)
dispatch multiple MSG
source§impl<APP, MSG> Program<APP, MSG>where
MSG: 'static,
APP: Application<MSG> + 'static,
impl<APP, MSG> Program<APP, MSG>where MSG: 'static, APP: Application<MSG> + 'static,
sourcepub fn add_window_event_listeners(&self, event_listeners: Vec<Attribute<MSG>>)
pub fn add_window_event_listeners(&self, event_listeners: Vec<Attribute<MSG>>)
attach event listeners to the window
sourcepub fn on_resize<F>(&self, cb: F)where
F: FnMut(i32, i32) -> MSG + Clone + 'static,
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
sourcepub fn on_resize_task<F>(cb: F) -> Task<MSG>where
F: FnMut(i32, i32) -> MSG + Clone + 'static,
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
sourcepub fn on_hashchange<F>(&self, cb: F)where
F: FnMut(String) -> MSG + 'static,
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