Skip to content

Commit

Permalink
fix: Reactivity fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Apr 19, 2024
1 parent e51ae0e commit fd834c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
23 changes: 10 additions & 13 deletions src/hooks/use_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CursorReference>,
Expand All @@ -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 {
Expand Down Expand Up @@ -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::<Option<Task>>(|| 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::<CursorLayoutResponse>();

Expand All @@ -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 {
Expand Down Expand Up @@ -523,6 +518,8 @@ pub fn use_edit(
}
});

cursor_receiver_task.set(Some(task));

cursor_reference
}
}));
Expand Down
9 changes: 7 additions & 2 deletions src/hooks/use_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit fd834c7

Please sign in to comment.