From 187850d55411b22b0b6bb9806a2d2156606fc272 Mon Sep 17 00:00:00 2001 From: Marc Espin Date: Fri, 19 Jan 2024 00:58:47 +0100 Subject: [PATCH] feat: Channel util improvements (#22) * feat: Channel util improvements * clean up * clean up --- examples/channel/src/main.rs | 4 ++-- std/src/i18n/mod.rs | 1 - std/src/utils/channel/use_channel.rs | 21 +++++++++----------- std/src/utils/channel/use_listen_channel.rs | 22 +++++++++------------ 4 files changed, 20 insertions(+), 28 deletions(-) diff --git a/examples/channel/src/main.rs b/examples/channel/src/main.rs index e220685..cc9f7d0 100644 --- a/examples/channel/src/main.rs +++ b/examples/channel/src/main.rs @@ -12,14 +12,14 @@ fn main() { fn app(cx: Scope) -> Element { let channel = use_channel::(cx, 5); - use_listen_channel(cx, &channel, move |message| async move { + use_listen_channel(cx, &channel, |message| async { match message { Ok(value) => log::info!("Incoming message: {value}"), Err(err) => log::info!("Error: {err:?}"), } }); - let send = move |_: MouseEvent| { + let send = |_: MouseEvent| { to_owned![channel]; async move { channel.send("Hello").await.ok(); diff --git a/std/src/i18n/mod.rs b/std/src/i18n/mod.rs index a099c01..dcfa5be 100644 --- a/std/src/i18n/mod.rs +++ b/std/src/i18n/mod.rs @@ -2,6 +2,5 @@ mod tanslate; mod use_i18n; mod use_init_i18n; -pub use self::tanslate::*; pub use self::use_i18n::*; pub use self::use_init_i18n::*; diff --git a/std/src/utils/channel/use_channel.rs b/std/src/utils/channel/use_channel.rs index 407a891..6130dc4 100644 --- a/std/src/utils/channel/use_channel.rs +++ b/std/src/utils/channel/use_channel.rs @@ -38,17 +38,14 @@ impl UseChannel { pub fn use_channel( cx: &ScopeState, size: usize, -) -> UseChannel { - let id = cx.use_hook(Uuid::new_v4); - let (sender, inactive_receiver) = cx.use_hook(|| { +) -> &UseChannel { + cx.use_hook(|| { + let id = Uuid::new_v4(); let (sender, receiver) = broadcast::(size); - - (sender, receiver.deactivate()) - }); - - UseChannel { - id: *id, - sender: sender.clone(), - inactive_receiver: inactive_receiver.clone(), - } + UseChannel { + id, + sender, + inactive_receiver: receiver.deactivate(), + } + }) } diff --git a/std/src/utils/channel/use_listen_channel.rs b/std/src/utils/channel/use_listen_channel.rs index 156a01c..4e2dec7 100644 --- a/std/src/utils/channel/use_listen_channel.rs +++ b/std/src/utils/channel/use_listen_channel.rs @@ -1,7 +1,7 @@ use std::future::Future; use async_broadcast::RecvError; -use dioxus::prelude::{to_owned, use_effect, ScopeState}; +use dioxus::prelude::{use_effect, ScopeState}; use super::UseChannel; @@ -15,19 +15,15 @@ pub fn use_listen_channel( ) where Handler: Future + 'static, { - use_effect(cx, (channel,), move |(channel,)| { - to_owned![channel]; - async move { - let action = Box::new(action); - let mut receiver = channel.receiver(); + use_effect(cx, (channel,), move |(mut channel,)| async move { + let mut receiver = channel.receiver(); - loop { - let message = receiver.recv().await; - let message_err = message.clone().err(); - action(message).await; - if message_err == Some(UseListenChannelError::Closed) { - break; - } + loop { + let message = receiver.recv().await; + let message_err = message.clone().err(); + action(message).await; + if message_err == Some(UseListenChannelError::Closed) { + break; } } });