Переглянути джерело

Make UseFuture Clone, factor out dependencies field (#1666)

Exotik850 1 рік тому
батько
коміт
18fa1e4831
1 змінених файлів з 6 додано та 5 видалено
  1. 6 5
      packages/hooks/src/use_future.rs

+ 6 - 5
packages/hooks/src/use_future.rs

@@ -31,13 +31,14 @@ where
 
     let state = cx.use_hook(move || UseFuture {
         update: cx.schedule_update(),
-        needs_regen: Cell::new(true),
+        needs_regen: Rc::new(Cell::new(true)),
         state: val.clone(),
         task: Default::default(),
-        dependencies: Vec::new(),
     });
 
-    if dependencies.clone().apply(&mut state.dependencies) || state.needs_regen.get() {
+    let state_dependencies = cx.use_hook(Vec::new);
+
+    if dependencies.clone().apply(state_dependencies) || state.needs_regen.get() {
         // kill the old one, if it exists
         if let Some(task) = state.task.take() {
             cx.remove_future(task);
@@ -69,11 +70,11 @@ pub enum FutureState<'a, T> {
     Regenerating(&'a T), // the old value
 }
 
+#[derive(Clone)]
 pub struct UseFuture<T: 'static> {
     update: Arc<dyn Fn()>,
-    needs_regen: Cell<bool>,
+    needs_regen: Rc<Cell<bool>>,
     task: Rc<Cell<Option<TaskId>>>,
-    dependencies: Vec<Box<dyn Any>>,
     state: UseState<Option<T>>,
 }