feat: useeffect
This commit is contained in:
1 parent
64edc6e1b1
commit
4ad4b18e4b
1 file changed
+27
-16
@@ -10,20 +10,39 @@ use crate::UseFutureDep;
|
|||||||
/// will be allowed to continue
|
/// will be allowed to continue
|
||||||
///
|
///
|
||||||
/// - dependencies: a tuple of references to values that are PartialEq + Clone
|
/// - dependencies: a tuple of references to values that are PartialEq + Clone
|
||||||
pub fn use_effect<'a, T: 'static, F: Future<Output = T> + 'static, D: UseFutureDep>(
|
///
|
||||||
cx: &'a ScopeState,
|
/// ## Examples
|
||||||
dependencies: D,
|
///
|
||||||
future: impl FnOnce(D::Out) -> F,
|
/// ```rust, ignore
|
||||||
) -> &'a UseEffect {
|
///
|
||||||
|
/// #[inline_props]
|
||||||
|
/// fn app(cx: Scope, name: &str) -> Element {
|
||||||
|
/// use_effect(&cx, (name,), |(name,)| async move {
|
||||||
|
/// set_title(name);
|
||||||
|
/// }))
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn use_effect<T, F, D>(cx: &ScopeState, dependencies: D, future: impl FnOnce(D::Out) -> F)
|
||||||
|
where
|
||||||
|
T: 'static,
|
||||||
|
F: Future<Output = T> + 'static,
|
||||||
|
D: UseFutureDep,
|
||||||
|
{
|
||||||
|
struct UseEffect {
|
||||||
|
needs_regen: bool,
|
||||||
|
task: Cell<Option<TaskId>>,
|
||||||
|
dependencies: Vec<Box<dyn Any>>,
|
||||||
|
}
|
||||||
|
|
||||||
let state = cx.use_hook(move |_| UseEffect {
|
let state = cx.use_hook(move |_| UseEffect {
|
||||||
needs_regen: Cell::new(true),
|
needs_regen: true,
|
||||||
task: Cell::new(None),
|
task: Cell::new(None),
|
||||||
dependencies: Vec::new(),
|
dependencies: Vec::new(),
|
||||||
});
|
});
|
||||||
|
|
||||||
if dependencies.clone().apply(&mut state.dependencies) || state.needs_regen.get() {
|
if dependencies.clone().apply(&mut state.dependencies) || state.needs_regen {
|
||||||
// We don't need regen anymore
|
// We don't need regen anymore
|
||||||
state.needs_regen.set(false);
|
state.needs_regen = false;
|
||||||
|
|
||||||
// Create the new future
|
// Create the new future
|
||||||
let fut = future(dependencies.out());
|
let fut = future(dependencies.out());
|
||||||
@@ -32,14 +51,6 @@ pub fn use_effect<'a, T: 'static, F: Future<Output = T> + 'static, D: UseFutureD
|
|||||||
fut.await;
|
fut.await;
|
||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
state
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct UseEffect {
|
|
||||||
needs_regen: Cell<bool>,
|
|
||||||
task: Cell<Option<TaskId>>,
|
|
||||||
dependencies: Vec<Box<dyn Any>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in new issue
Block a user