forked from yewstack/yew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.rs
66 lines (56 loc) · 1.81 KB
/
hooks.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))]
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
mod feat_futures {
use std::fmt;
use std::future::Future;
use std::ops::Deref;
use yew::prelude::*;
use yew::suspense::{Suspension, SuspensionResult};
/// This hook is used to await a future in a suspending context.
///
/// A [Suspension] is created from the passed future and the result of the future
/// is the output of the suspension.
pub struct UseFutureHandle<O> {
inner: UseStateHandle<Option<O>>,
}
impl<O> Deref for UseFutureHandle<O> {
type Target = O;
fn deref(&self) -> &Self::Target {
&*self.inner.as_ref().unwrap()
}
}
impl<T: fmt::Debug> fmt::Debug for UseFutureHandle<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("UseFutureHandle")
.field("value", &format!("{:?}", self.inner))
.finish()
}
}
#[hook]
pub fn use_future<F, T, O>(f: F) -> SuspensionResult<UseFutureHandle<O>>
where
F: FnOnce() -> T + 'static,
T: Future<Output = O> + 'static,
O: 'static,
{
let output = use_state(|| None);
let suspension = {
let output = output.clone();
use_memo(
move |_| {
Suspension::from_future(async move {
output.set(Some(f().await));
})
},
(),
)
};
if suspension.resumed() {
Ok(UseFutureHandle { inner: output })
} else {
Err((*suspension).clone())
}
}
}
#[cfg(any(target_arch = "wasm32", feature = "tokio"))]
pub use feat_futures::*;