docs: document all of desktop crate

This commit is contained in:
Jonathan Kelley committed 2022-02-20 23:52:36 -05:00
1 parent e71069883d
commit b2a4d387c7
7 files changed
+138 -108

No files matched your search

+3
View File
@@ -1,3 +1,6 @@
#![deny(missing_docs)]
//! Useful foundational hooks for Dioxus
mod usestate;
pub use usestate::{use_state, UseState};
+33 -9
View File
@@ -1,19 +1,30 @@
#![allow(missing_docs)]
use dioxus_core::{ScopeState, TaskId};
use std::{cell::Cell, future::Future, rc::Rc};
/// A future that resolves to a value.
///
/// This runs the future only once - though the future may be regenerated
/// through the [`UseFuture::restart`] method.
///
/// This is commonly used for components that cannot be rendered until some
/// asynchronous operation has completed.
///
///
///
///
///
pub fn use_future<'a, T: 'static, F: Future<Output = T> + 'static>(
cx: &'a ScopeState,
new_fut: impl FnOnce() -> F,
) -> &'a UseFuture<T> {
let state = cx.use_hook(move |_| {
//
UseFuture {
update: cx.schedule_update(),
needs_regen: Cell::new(true),
slot: Rc::new(Cell::new(None)),
value: None,
task: None,
}
let state = cx.use_hook(move |_| UseFuture {
update: cx.schedule_update(),
needs_regen: Cell::new(true),
slot: Rc::new(Cell::new(None)),
value: None,
task: None,
pending: true,
});
if let Some(value) = state.slot.take() {
@@ -24,6 +35,7 @@ pub fn use_future<'a, T: 'static, F: Future<Output = T> + 'static>(
if state.needs_regen.get() {
// We don't need regen anymore
state.needs_regen.set(false);
state.pending = false;
// Create the new future
let fut = new_fut();
@@ -42,10 +54,17 @@ pub fn use_future<'a, T: 'static, F: Future<Output = T> + 'static>(
state
}
pub enum FutureState<'a, T> {
Pending,
Complete(&'a T),
Regenerating(&'a T), // the old value
}
pub struct UseFuture<T> {
update: Rc<dyn Fn()>,
needs_regen: Cell<bool>,
value: Option<T>,
pending: bool,
slot: Rc<Cell<Option<T>>>,
task: Option<TaskId>,
}
@@ -72,4 +91,9 @@ impl<T> UseFuture<T> {
pub fn value(&self) -> Option<&T> {
self.value.as_ref()
}
pub fn state(&self) -> FutureState<T> {
// self.value.as_ref()
FutureState::Pending
}
}