usefuture.rs 691 B

12345678910111213141516171819202122232425262728293031323334
  1. use dioxus_core::{ScopeState, TaskId};
  2. use std::{cell::Cell, future::Future};
  3. pub fn use_future<'a, T: 'static, F: Future<Output = T>>(
  4. cx: &'a ScopeState,
  5. f: impl FnOnce() -> F,
  6. ) -> FutureHandle<'a, T> {
  7. cx.use_hook(
  8. |_| {
  9. //
  10. UseFutureInner {
  11. needs_regen: true,
  12. task: None,
  13. }
  14. },
  15. |_| {
  16. //
  17. FutureHandle {
  18. cx,
  19. value: Cell::new(None),
  20. }
  21. },
  22. )
  23. }
  24. struct UseFutureInner {
  25. needs_regen: bool,
  26. task: Option<TaskId>,
  27. }
  28. pub struct FutureHandle<'a, T> {
  29. cx: &'a ScopeState,
  30. value: Cell<Option<T>>,
  31. }