wip: upgrade hooks

This commit is contained in:
Jonathan Kelley committed 2021-12-27 15:03:43 -05:00
1 parent c0e0196a67
commit b3ac2ee3f7
10 files changed
+150 -44

No files matched your search

+32 -10
View File
@@ -1,30 +1,52 @@
use dioxus_core::{ScopeState, TaskId};
use std::{cell::Cell, future::Future};
use std::{cell::Cell, future::Future, rc::Rc};
pub fn use_future<'a, T: 'static, F: Future<Output = T>>(
pub fn use_future<'a, T: 'static, F: Future<Output = T> + 'static>(
cx: &'a ScopeState,
f: impl FnOnce() -> F,
) -> FutureHandle<'a, T> {
) -> (Option<&T>, FutureHandle<'a, T>) {
cx.use_hook(
|_| {
//
let fut = f();
let slot = Rc::new(Cell::new(None));
let updater = cx.schedule_update();
let _slot = slot.clone();
let new_fut = async move {
let res = fut.await;
_slot.set(Some(res));
updater();
};
let task = cx.push_future(new_fut);
UseFutureInner {
needs_regen: true,
task: None,
slot,
value: None,
task: Some(task),
}
},
|_| {
//
FutureHandle {
cx,
value: Cell::new(None),
|state| {
if let Some(value) = state.slot.take() {
state.value = Some(value);
state.task = None;
}
(
state.value.as_ref(),
FutureHandle {
cx,
value: Cell::new(None),
},
)
},
)
}
struct UseFutureInner {
struct UseFutureInner<T> {
needs_regen: bool,
value: Option<T>,
slot: Rc<Cell<Option<T>>>,
task: Option<TaskId>,
}