Clone in a terminal:
Commit 3f8adb6
Johan Mattsson - 2026-06-01T21:21:05+00:00
3f8adb64aa9a8793b84fdb01ca39ca180eadf7a6johan.mattsson.m@gmail.com

Cleanup and documentation

Changed files (3)

Diff

diff --git a/examples/callbacks/src/lib.rs b/examples/callbacks/src/lib.rsindex 4a67b7a..33f42a2 100644--- a/examples/callbacks/src/lib.rs+++ b/examples/callbacks/src/lib.rs@@ -1,22 +1,13 @@ #![cfg(target_arch = "wasm32")] use kaja_web::prelude::*; use serde::Deserialize;-use std::sync::RwLock;--pub struct AppData {-    current_count: isize,-}  #[derive(Deserialize)] pub struct Test1Data {     test_parameter: String, } -pub static APP_DATA: RwLock<AppData> = RwLock::new(AppData { current_count: 0 });--fn update(app_data: &AppData) {-    let count = app_data.current_count;-+fn update() {     let counter = html! {{         <button onclick="test0()">             Test 0@@ -71,8 +62,6 @@ pub fn init() {     std::panic::set_hook(Box::new(console_error_panic_hook::hook));     console_error_panic_hook::set_once(); -    let mut app_data = APP_DATA.read().unwrap();-    update(&app_data);-+    update();     init_callbacks(); }diff --git a/src/lib.rs b/src/lib.rsindex d23b337..917268e 100644--- a/src/lib.rs+++ b/src/lib.rs@@ -1,4 +1,4 @@-use gloo::console::log;+use gloo_console::error; use wasm_bindgen::JsCast; use web_sys::{Element, HtmlElement, NodeList}; @@ -12,13 +12,6 @@ pub struct Error;  pub type Result<T> = std::result::Result<T, Error>; -pub struct JsCallItem {-    pub name: &'static str,-    pub install: fn(),-}--pub struct App {}- pub struct CallbackRegistration {     pub register: fn(), }@@ -27,24 +20,33 @@ unsafe impl Sync for CallbackRegistration {}  inventory::collect!(CallbackRegistration); +/// Register callbacks that can be used in JS to run Rust functions in the WASM bundle.+/// This needs to run when the WASM bundle is loaded.+///+/// ```rust+/// #[wasm_bindgen(start)]+/// pub fn init() {+///     init_callbacks();+/// }+///+/// The Rust functions must be marked with the `#[callback]` attribute to be registered.+/// ```rust+/// #[callback(test)]+/// pub fn test() {+/// }+/// ```+///+/// ```rust+/// let html = html!{{+///     <button onclick="test()">Test</button>+/// }};+/// ``` pub fn init_callbacks() {     for init in inventory::iter::<InitFn> {         (init.0)();     } } -impl App {-    pub fn new() -> Self {-        App {}-    }--    pub fn set_callbacks(&self) {-        for reg in inventory::iter::<CallbackRegistration> {-            (reg.register)();-        }-    }-}- /// Queries the document for a single element matching the given selector. /// Returns `None` if no element is found. Returns first matching element if multiple are found. pub fn get_element(selector: &str) -> Option<HtmlElement> {@@ -60,7 +62,7 @@ pub fn get_element(selector: &str) -> Option<HtmlElement> {     }      if elements.length() > 1 {-        log!(+        error!(             "Expecting a single element, found {} instead.",             elements.length()         );@@ -75,13 +77,6 @@ pub fn get_element(selector: &str) -> Option<HtmlElement> {     None } -pub fn node_list_to_vec(list: NodeList) -> Vec<HtmlElement> {-    (0..list.length())-        .filter_map(|i| list.item(i))-        .filter_map(|node| node.dyn_into::<HtmlElement>().ok())-        .collect()-}- /// Queries the document for all elements matching the given selector. pub fn get_elements(selector: &str) -> Vec<HtmlElement> {     let window = web_sys::window();@@ -99,7 +94,7 @@ pub fn get_elements(selector: &str) -> Vec<HtmlElement> {     let elements = document.unwrap().query_selector_all(selector);      if elements.is_err() {-        log!("Failed to query selector all: {:?}", selector);+        error!("Failed to query selector all: {:?}", selector);         return Vec::new();     } @@ -161,3 +156,10 @@ pub fn try_inner_html(selector: &str, html: &str) { pub fn try_inner_text(selector: &str, text: &str) {     let _ = inner_text(selector, text); }++fn node_list_to_vec(list: NodeList) -> Vec<HtmlElement> {+    (0..list.length())+        .filter_map(|i| list.item(i))+        .filter_map(|node| node.dyn_into::<HtmlElement>().ok())+        .collect()+}diff --git a/src/prelude.rs b/src/prelude.rsindex a434df6..16c57da 100644--- a/src/prelude.rs+++ b/src/prelude.rs@@ -1,8 +1,9 @@ pub use crate::{-    get_element, get_elements, init_callbacks, inner_html, inner_text, node_list_to_vec,-    try_inner_html, try_inner_text, App, CallbackRegistration, Error, InitFn, JsCallItem, Result,+    get_element, get_elements, init_callbacks, inner_html, inner_text, try_inner_html,+    try_inner_text, CallbackRegistration, Error, InitFn, Result, }; +pub use gloo::console::error; pub use gloo::console::log; pub use serde_wasm_bindgen; 

Contact

Email: johan.mattsson.m@gmail.com
Mastodon: typo.social/@birdfont
GitHub: johanmattssonm
LinkedIn: johan-mattsson-2ab324112