euv_ui/hook/suspense/impl.rs
1use super::*;
2
3/// Inherent implementation of [`SuspenseHandle`].
4impl<T: Clone + PartialEq + 'static> SuspenseHandle<T> {
5 /// Creates a new `SuspenseHandle` in the `Pending`
6 /// phase.
7 pub fn new() -> Self {
8 Self {
9 phase: Signal::create(SuspensePhase::Pending),
10 }
11 }
12
13 /// Transitions the phase to `Resolved(value)`. Works
14 /// on every target.
15 ///
16 /// # Arguments
17 ///
18 /// - `T: Clone + PartialEq + 'static` - A generic type parameter.
19 pub fn resolve_sync(&self, value: T) {
20 self.get_phase().set(SuspensePhase::Resolved(value));
21 }
22
23 /// Transitions the phase to `Failed(message)`. Works
24 /// on every target.
25 ///
26 /// # Arguments
27 ///
28 /// - `String` - A `String` parameter.
29 pub fn fail(&self, message: String) {
30 self.get_phase().set(SuspensePhase::Failed(message));
31 }
32
33 /// Transitions the phase back to `Pending`. Useful
34 /// when invalidating the cache (e.g., after a
35 /// mutation that requires refetching).
36 pub fn reset(&self) {
37 self.get_phase().set(SuspensePhase::Pending);
38 }
39}
40
41/// Default-construction for [`SuspenseHandle`].
42impl<T: Clone + PartialEq + 'static> Default for SuspenseHandle<T> {
43 /// Constructs a default [`SuspenseHandle`] value.
44 fn default() -> Self {
45 Self::new()
46 }
47}
48
49/// Debug formatting for [`SuspenseHandle`].
50impl<T: Clone + PartialEq + Debug + 'static> Display for SuspenseHandle<T> {
51 /// Formats the [`SuspenseHandle`] via the supplied formatter.
52 ///
53 /// # Arguments
54 ///
55 /// - `&mut Formatter<'_>` - The formatter receiving the formatted output.
56 ///
57 /// # Returns
58 ///
59 /// - `FmtResult` - Result of the formatting operation.
60 fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
61 write!(formatter, "SuspenseHandle({:?})", self.get_phase().get())
62 }
63}
64
65/// Equality for [`SuspensePhase`].
66impl<T: PartialEq> PartialEq for SuspensePhase<T> {
67 /// Returns `true` when `self` and `other` are equivalent by the [`PartialEq`] contract.
68 ///
69 /// # Arguments
70 ///
71 /// - `&Self` - The other value to compare against `self`.
72 ///
73 /// # Returns
74 ///
75 /// - `bool` - `true` when `self` and `other` are equivalent by the trait contract.
76 fn eq(&self, other: &Self) -> bool {
77 match (self, other) {
78 (SuspensePhase::Pending, SuspensePhase::Pending) => true,
79 (SuspensePhase::Resolved(a), SuspensePhase::Resolved(b)) => a == b,
80 (SuspensePhase::Failed(a), SuspensePhase::Failed(b)) => a == b,
81 _ => false,
82 }
83 }
84}