|
1 | 1 | use core::fmt; |
2 | 2 | use dioxus::prelude::*; |
3 | | -use dioxus::{ |
4 | | - hooks::{use_memo, use_reactive}, |
5 | | - signals::CopyValue, |
6 | | -}; |
| 3 | +use dioxus::signals::CopyValue; |
7 | 4 | use dioxus_core::{provide_root_context, spawn_forever, use_drop, ReactiveContext, Task}; |
8 | 5 | use std::{ |
9 | 6 | cell::{Ref, RefCell}, |
@@ -262,7 +259,7 @@ impl<Q: MutationCapability> MutationReader<Q> { |
262 | 259 | } |
263 | 260 |
|
264 | 261 | pub struct UseMutation<Q: MutationCapability> { |
265 | | - mutation: Memo<Mutation<Q>>, |
| 262 | + mutation: Signal<Mutation<Q>>, |
266 | 263 | } |
267 | 264 |
|
268 | 265 | impl<Q: MutationCapability> Clone for UseMutation<Q> { |
@@ -370,29 +367,33 @@ pub fn use_mutation<Q: MutationCapability>(mutation: Mutation<Q>) -> UseMutation |
370 | 367 | None => provide_root_context(MutationsStorage::<Q>::new_in_root()), |
371 | 368 | }; |
372 | 369 |
|
373 | | - let current_mutation = use_hook(|| Rc::new(RefCell::new(None))); |
374 | | - |
375 | | - // Create or update mutation subscription on changes |
376 | | - let mutation = use_memo(use_reactive!(|mutation| { |
| 370 | + let mut make_mutation = |mutation: &Mutation<Q>, mut prev_mutation: Option<Mutation<Q>>| { |
377 | 371 | let _data = storage.insert_or_get_mutation(mutation.clone()); |
378 | 372 |
|
379 | 373 | // Update the mutation tasks if there has been a change in the mutation |
380 | | - if let Some(prev_mutation) = current_mutation.borrow_mut().take() { |
| 374 | + if let Some(prev_mutation) = prev_mutation.take() { |
381 | 375 | storage.update_tasks(prev_mutation); |
382 | 376 | } |
| 377 | + }; |
383 | 378 |
|
384 | | - // Store this new mutation |
385 | | - current_mutation.borrow_mut().replace(mutation.clone()); |
| 379 | + let mut current_mutation = use_hook(|| { |
| 380 | + make_mutation(&mutation, None); |
| 381 | + Signal::new(mutation.clone()) |
| 382 | + }); |
386 | 383 |
|
387 | | - mutation |
388 | | - })); |
| 384 | + if *current_mutation.read() != mutation { |
| 385 | + let prev = mem::replace(&mut *current_mutation.write(), mutation.clone()); |
| 386 | + make_mutation(&mutation, Some(prev)); |
| 387 | + } |
389 | 388 |
|
390 | | - // Update the query tasks when the scope is dropped |
| 389 | + // Update the mutation tasks when the scope is dropped |
391 | 390 | use_drop({ |
392 | 391 | move || { |
393 | | - storage.update_tasks(mutation.peek().clone()); |
| 392 | + storage.update_tasks(current_mutation.peek().clone()); |
394 | 393 | } |
395 | 394 | }); |
396 | 395 |
|
397 | | - UseMutation { mutation } |
| 396 | + UseMutation { |
| 397 | + mutation: current_mutation, |
| 398 | + } |
398 | 399 | } |
0 commit comments