1
0

use_future.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #![allow(missing_docs)]
  2. use dioxus_core::{ScopeState, Task};
  3. use dioxus_signals::{use_effect, use_signal, Signal};
  4. use std::{any::Any, cell::Cell, future::Future, rc::Rc, sync::Arc};
  5. /// A future that resolves to a value.
  6. ///
  7. /// This runs the future only once - though the future may be regenerated
  8. /// through the [`UseFuture::restart`] method.
  9. ///
  10. /// This is commonly used for components that cannot be rendered until some
  11. /// asynchronous operation has completed.
  12. ///
  13. /// Whenever the hooks dependencies change, the future will be re-evaluated.
  14. /// If a future is pending when the dependencies change, the previous future
  15. /// will be canceled before the new one is started.
  16. ///
  17. /// - dependencies: a tuple of references to values that are PartialEq + Clone
  18. pub fn use_future<T, F>(future: impl FnMut() -> F) -> UseFuture<T>
  19. where
  20. T: 'static,
  21. F: Future<Output = T> + 'static,
  22. {
  23. let task = use_signal(|| None);
  24. use_effect(|| {
  25. // task.set();
  26. });
  27. //
  28. UseFuture {
  29. value: todo!(),
  30. task,
  31. state: todo!(),
  32. }
  33. }
  34. pub struct UseFuture<T: 'static> {
  35. value: Signal<T>,
  36. task: Signal<Option<Task>>,
  37. state: Signal<UseFutureState<T>>,
  38. }
  39. impl<T> UseFuture<T> {
  40. /// Restart the future with new dependencies.
  41. ///
  42. /// Will not cancel the previous future, but will ignore any values that it
  43. /// generates.
  44. pub fn restart(&self) {
  45. // self.needs_regen.set(true);
  46. // (self.update)();
  47. }
  48. /// Forcefully cancel a future
  49. pub fn cancel(&self) {
  50. // if let Some(task) = self.task.take() {
  51. // cx.remove_future(task);
  52. // }
  53. }
  54. // Manually set the value in the future slot without starting the future over
  55. pub fn set(&self, new_value: T) {
  56. // self.state.set(Some(new_value));
  57. }
  58. /// Return any value, even old values if the future has not yet resolved.
  59. ///
  60. /// If the future has never completed, the returned value will be `None`.
  61. pub fn value(&self) -> Signal<Option<T>> {
  62. todo!()
  63. // self.state.current_val.as_ref().as_ref()
  64. }
  65. /// Get the ID of the future in Dioxus' internal scheduler
  66. pub fn task(&self) -> Option<Task> {
  67. todo!()
  68. // self.task.get()
  69. }
  70. /// Get the current state of the future.
  71. pub fn state(&self) -> UseFutureState<T> {
  72. todo!()
  73. // match (&self.task.get(), &self.value()) {
  74. // // If we have a task and an existing value, we're reloading
  75. // (Some(_), Some(val)) => UseFutureState::Reloading(val),
  76. // // no task, but value - we're done
  77. // (None, Some(val)) => UseFutureState::Complete(val),
  78. // // no task, no value - something's wrong? return pending
  79. // (None, None) => UseFutureState::Pending,
  80. // // Task, no value - we're still pending
  81. // (Some(_), None) => UseFutureState::Pending,
  82. // }
  83. }
  84. }
  85. pub enum UseFutureState<T: 'static> {
  86. Pending,
  87. Complete(Signal<T>),
  88. Regenerating(Signal<T>), // the old value
  89. }