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