Clone in a terminal:
Commit 67b708d
Johan Mattsson - 2026-07-11T20:50:53+00:00
67b708d29658d24f4ad04357cbd163fc65dd4c6djohan.mattsson.m@gmail.com

Less locking

Changed files (1)

Diff

diff --git a/examples/threads2/src/main_component.rs b/examples/threads2/src/main_component.rsindex 3c867af..4ea862e 100644--- a/examples/threads2/src/main_component.rs+++ b/examples/threads2/src/main_component.rs@@ -1,14 +1,16 @@ use arc_swap::ArcSwap; use kaja_web::prelude::*;+use std::cell::RefCell; use std::collections::HashMap; use std::sync::Arc;-use std::sync::LazyLock; use std::sync::RwLock; use std::time::Duration;  static THREAD_ID: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);-static THREAD_COMPONENT_MAP: LazyLock<RwLock<HashMap<String, ThreadComponent>>> =-    LazyLock::new(|| RwLock::new(HashMap::new()));+std::thread_local! {+    static THREAD_COMPONENT_MAP: RefCell<HashMap<String, ThreadComponent>> =+        RefCell::new(HashMap::new());+}  struct ThreadComponent {     id: u32,@@ -34,10 +36,10 @@ impl ThreadComponent {             bg: Arc::new(RwLock::new(bg)),         }; -        THREAD_COMPONENT_MAP-            .write()-            .unwrap()-            .insert(component_id.to_string(), thread_component.shallow_clone());+        THREAD_COMPONENT_MAP.with(|map| {+            map.borrow_mut()+                .insert(component_id.to_string(), thread_component.shallow_clone());+        });         thread_component     } @@ -124,20 +126,26 @@ impl Component for Main {             return;         }; -        let map = THREAD_COMPONENT_MAP.read().unwrap();+        let Some(component) = THREAD_COMPONENT_MAP.with(|map| {+            let map = map.borrow();++            let Some(thread_component) = map.get(id) else {+                return None;+            };++            let component = thread_component.ui.load_full();++            thread_component.async_update(|component| {+                component.test = !component.test;+                component.test_message = "test message".to_string();+            }); -        let Some(thread_component) = map.get(id) else {+            Some(component)+        }) else {             error!("Thread component not found:", id);             return;         }; -        let component = thread_component.ui.load_full();--        thread_component.async_update(|component| {-            component.test = !component.test;-            component.test_message = "test message".to_string();-        });-         log!("Rendering component, loaded read only:", id);         component.render(&element);     }

Contact

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