Skip to content

Commit

Permalink
feat: Channel util improvements (#22)
Browse files Browse the repository at this point in the history
* feat: Channel util improvements

* clean up

* clean up
  • Loading branch information
marc2332 authored Jan 18, 2024
1 parent 8db5b1e commit 187850d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
4 changes: 2 additions & 2 deletions examples/channel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ fn main() {
fn app(cx: Scope) -> Element {
let channel = use_channel::<String>(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();
Expand Down
1 change: 0 additions & 1 deletion std/src/i18n/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
21 changes: 9 additions & 12 deletions std/src/utils/channel/use_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ impl<MessageType: Clone> UseChannel<MessageType> {
pub fn use_channel<MessageType: Clone + 'static>(
cx: &ScopeState,
size: usize,
) -> UseChannel<MessageType> {
let id = cx.use_hook(Uuid::new_v4);
let (sender, inactive_receiver) = cx.use_hook(|| {
) -> &UseChannel<MessageType> {
cx.use_hook(|| {
let id = Uuid::new_v4();
let (sender, receiver) = broadcast::<MessageType>(size);

(sender, receiver.deactivate())
});

UseChannel {
id: *id,
sender: sender.clone(),
inactive_receiver: inactive_receiver.clone(),
}
UseChannel {
id,
sender,
inactive_receiver: receiver.deactivate(),
}
})
}
22 changes: 9 additions & 13 deletions std/src/utils/channel/use_listen_channel.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -15,19 +15,15 @@ pub fn use_listen_channel<MessageType: Clone + 'static, Handler>(
) where
Handler: Future<Output = ()> + '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;
}
}
});
Expand Down

0 comments on commit 187850d

Please sign in to comment.