From fd834c7d8bfd1bfb6a78eaa7d605413344b8a70e Mon Sep 17 00:00:00 2001 From: marc2332 Date: Sat, 20 Apr 2024 00:41:42 +0200 Subject: [PATCH] fix: Reactivity fixes --- src/hooks/use_edit.rs | 23 ++++++++++------------- src/hooks/use_metrics.rs | 9 +++++++-- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/hooks/use_edit.rs b/src/hooks/use_edit.rs index 8e8f282..8b0b054 100644 --- a/src/hooks/use_edit.rs +++ b/src/hooks/use_edit.rs @@ -316,7 +316,7 @@ impl TextEditor for EditorData { } /// Manage an editable content. -#[derive(Clone, Copy)] +#[derive(Clone, Copy, PartialEq)] pub struct UseEdit { pub(crate) radio: RadioAppState, pub(crate) cursor_reference: Memo, @@ -327,16 +327,6 @@ pub struct UseEdit { pub(crate) metrics: UseMetrics, } -impl PartialEq for UseEdit { - fn eq(&self, other: &Self) -> bool { - self.radio == other.radio - && self.cursor_reference == other.cursor_reference - && self.selecting_text_with_mouse == other.selecting_text_with_mouse - && self.pane_index == other.pane_index - && self.editor_index == other.editor_index - } -} - impl UseEdit { /// Create a cursor attribute. pub fn cursor_attr(&self) -> AttributeValue { @@ -454,10 +444,15 @@ pub fn use_edit( ) -> UseEdit { let selecting_text_with_mouse = use_signal(|| None); let platform = use_platform(); + let mut cursor_receiver_task = use_signal::>(|| None); let cursor_reference = use_memo(use_reactive(&(pane_index, editor_index), { to_owned![radio]; - move |_| { + move |(pane_index, editor_index)| { + if let Some(cursor_receiver_task) = cursor_receiver_task.write_unchecked().take() { + cursor_receiver_task.cancel(); + } + let text_id = Uuid::new_v4(); let (cursor_sender, mut cursor_receiver) = unbounded_channel::(); @@ -469,7 +464,7 @@ pub fn use_edit( cursor_selections: Arc::new(Mutex::new(None)), }; - spawn({ + let task = spawn({ to_owned![cursor_reference]; async move { while let Some(message) = cursor_receiver.recv().await { @@ -523,6 +518,8 @@ pub fn use_edit( } }); + cursor_receiver_task.set(Some(task)); + cursor_reference } })); diff --git a/src/hooks/use_metrics.rs b/src/hooks/use_metrics.rs index 7db4e27..414e05f 100644 --- a/src/hooks/use_metrics.rs +++ b/src/hooks/use_metrics.rs @@ -97,11 +97,16 @@ impl UseMetrics { pub fn use_metrics(radio: &RadioAppState, pane_index: usize, editor_index: usize) -> UseMetrics { let metrics_ref = use_signal::<(SyntaxBlocks, f32)>(|| (SyntaxBlocks::default(), 0.0)); - use_hook(|| { + let mut metrics = use_hook(|| { let mut metrics = UseMetrics::new(*radio, metrics_ref, pane_index, editor_index); metrics.run_metrics(); metrics - }) + }); + + metrics.pane_index = pane_index; + metrics.editor_index = editor_index; + + metrics }