Skip to content

Commit

Permalink
Merge branch 'main' into release/0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 authored Aug 13, 2023
2 parents 8ddc8ce + 6b8ae0f commit 577fe85
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions hooks/src/use_node.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dioxus_core::{AttributeValue, ScopeState};
use dioxus_hooks::{use_effect, use_ref, use_state, UseRef};
use dioxus_hooks::{use_ref, use_state, UseRef, to_owned};
use freya_common::NodeReferenceLayout;
use freya_node_state::{CustomAttributeValues, NodeReference};
use tokio::sync::mpsc::unbounded_channel;
Expand All @@ -8,29 +8,23 @@ use tokio::sync::mpsc::unbounded_channel;
pub fn use_node(cx: &ScopeState) -> (AttributeValue, NodeReferenceLayout) {
let status = use_state::<NodeReferenceLayout>(cx, NodeReferenceLayout::default);

let channel = cx.use_hook(|| {
let (tx, rx) = unbounded_channel::<NodeReferenceLayout>();
(tx, Some(rx))
});

let node_ref = NodeReference(channel.0.clone());
let tx = cx.use_hook(|| {
let (tx, mut rx) = unbounded_channel::<NodeReferenceLayout>();

use_effect(cx, (), move |_| {
let rx = channel.1.take();
let status = status.clone();
to_owned![status];
cx.spawn(async move {
let mut rx = rx.unwrap();
while let Some(new_status) = rx.recv().await {
if status.current().as_ref() != &new_status {
status.set(new_status);
}
}
});
async move {}

tx
});

(
cx.any_value(CustomAttributeValues::Reference(node_ref)),
cx.any_value(CustomAttributeValues::Reference(NodeReference(tx.clone()))),
status.get().clone(),
)
}
Expand All @@ -39,29 +33,23 @@ pub fn use_node(cx: &ScopeState) -> (AttributeValue, NodeReferenceLayout) {
pub fn use_node_ref(cx: &ScopeState) -> (AttributeValue, &UseRef<NodeReferenceLayout>) {
let status = use_ref::<NodeReferenceLayout>(cx, NodeReferenceLayout::default);

let channel = cx.use_hook(|| {
let (tx, rx) = unbounded_channel::<NodeReferenceLayout>();
(tx, Some(rx))
});

let node_ref = NodeReference(channel.0.clone());
let tx = cx.use_hook(|| {
let (tx, mut rx) = unbounded_channel::<NodeReferenceLayout>();

use_effect(cx, (), move |_| {
let rx = channel.1.take();
let status = status.clone();
to_owned![status];
cx.spawn(async move {
let mut rx = rx.unwrap();
while let Some(new_status) = rx.recv().await {
if *status.read() != new_status {
*status.write_silent() = new_status;
}
}
});
async move {}

tx
});

(
cx.any_value(CustomAttributeValues::Reference(node_ref)),
cx.any_value(CustomAttributeValues::Reference(NodeReference(tx.clone()))),
status,
)
}
Expand Down

0 comments on commit 577fe85

Please sign in to comment.