on master help sign in
Move the `on` event listener constructor to the `builder` module
commitaa8d0f952cbea37329096008436f8ad3ae968bb0 authorNick Fitzgerald <fitzgen@gmail.com> dateWed 13 Feb 2019, 15:35 (8 years ago) parent07dca7e
3 files changed+31−21
src/builder.rs+27−1 src/lib.rs+2−2 src/node.rs+2−18
src/builder.rs+27−1
@@ -39,7 +39,7 @@ where
3939 /// # Example
4040 ///
4141 /// ```no_run
42− /// use dodrio::{builder::*, bumpalo::Bump, on};
42+ /// use dodrio::{builder::*, bumpalo::Bump};
4343 ///
4444 /// let b = Bump::new();
4545 ///
@@ -856,3 +856,29 @@ pub fn text<'a>(contents: &'a str) -> Node<'a> {
856856pub fn attr<'a>(name: &'a str, value: &'a str) -> Attribute<'a> {
857857 Attribute { name, value }
858858}
859+
860+/// Create an event listener.
861+///
862+/// `event` is the type of event to listen for, e.g. `"click"`. The `callback`
863+/// is the function that will be invoked when the event occurs.
864+///
865+/// # Example
866+///
867+/// ```no_run
868+/// use dodrio::{builder::*, bumpalo::Bump};
869+///
870+/// let b = Bump::new();
871+///
872+/// let listener = on(&b, "click", |root, vdom, event| {
873+/// // do something when a click happens...
874+/// });
875+/// ```
876+pub fn on<'a, F>(bump: &'a Bump, event: &'a str, callback: F) -> Listener<'a>
877+where
878+ F: Fn(&mut dyn RootRender, VdomWeak, web_sys::Event) + 'static,
879+{
880+ Listener {
881+ event,
882+ callback: bump.alloc(callback),
883+ }
884+}
src/lib.rs+2−2
@@ -3,7 +3,7 @@
33//! ## Example
44//!
55//! ```no_run
6−//! use dodrio::{on, bumpalo::Bump, Attribute, Node, Render};
6+//! use dodrio::{bumpalo::Bump, Attribute, Node, Render};
77//! use wasm_bindgen::UnwrapThrowExt;
88//!
99//! /// A component that greets someone.
@@ -67,6 +67,6 @@ pub mod builder;
6767
6868// Re-export items at the top level.
6969pub use self::cached::Cached;
70−pub use self::node::{on, Attribute, ElementNode, Listener, ListenerCallback, Node, TextNode};
70+pub use self::node::{Attribute, ElementNode, Listener, ListenerCallback, Node, TextNode};
7171pub use self::render::{Render, RootRender};
7272pub use self::vdom::{Vdom, VdomWeak};
src/node.rs+2−18
@@ -43,9 +43,9 @@ pub type ListenerCallback<'a> =
4343/// An event listener.
4444pub struct Listener<'a> {
4545 /// The type of event to listen for.
46− pub event: &'a str,
46+ pub(crate) event: &'a str,
4747 /// The callback to invoke when the event happens.
48− pub callback: ListenerCallback<'a>,
48+ pub(crate) callback: ListenerCallback<'a>,
4949}
5050
5151/// An attribute on a DOM node, such as `id="my-thing"` or
@@ -196,19 +196,3 @@ impl Listener<'_> {
196196 }
197197 }
198198}
199−
200−/// Utility function for creating event listeners and downcasting the component
201−/// to its `RootRender` concrete type.
202−pub fn on<'a, F>(bump: &'a Bump, event: &'a str, callback: F) -> Listener<'a>
203−where
204− F: Fn(&mut dyn RootRender, VdomWeak, web_sys::Event) + 'static,
205−{
206− Listener {
207− event,
208− callback: bump.alloc(
209− move |component: &mut dyn RootRender, vdom: VdomWeak, event: web_sys::Event| {
210− callback(component, vdom, event);
211− },
212− ),
213− }
214−}