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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::{
pin::Pin,
task::{Context, Poll},
};
use pin_project_lite::pin_project;
use topcoat_core::error::Result;
use crate::{RegionId, View, ViewBufferScope, ViewFirst, ViewSwap, internal::ScopeView};
pin_project! {
/// A [`View`] that shows a fallback until its child content is ready.
///
/// Polls the child first. If its initial content is ready, it renders
/// directly. Otherwise, a live region shows the fallback until the
/// child's content replaces it.
///
/// With `wait` enabled, the boundary waits for the child's first content
/// and renders it in place. It never polls the fallback or creates a
/// region of its own.
///
/// The fallback can stream updates while the child is pending. Once the
/// child resolves, only its updates pass through. Errors from either view
/// propagate to the caller.
pub struct SuspenseView<F, C> {
#[pin]
fallback: F,
#[pin]
child: ScopeView<C>,
region: RegionId,
wait: bool,
state: State,
}
}
/// What a [`SuspenseView`] has shown so far.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
/// Nothing has resolved yet.
Start,
/// The fallback went out and the child is still pending. The flag says
/// whether the fallback still yields swaps of its own.
Fallback { live: bool },
/// The child's first content went out and its swaps pass through.
Child,
/// The child has no more updates.
Done,
}
impl<F, C> SuspenseView<F, C> {
/// Creates a boundary whose fallback is replaced at `region` when needed.
///
/// Set `wait` to delay the boundary's first content until the child is
/// ready, without showing the fallback.
#[doc(hidden)]
pub fn new(region: RegionId, fallback: F, child: C, wait: bool) -> Self {
Self {
fallback,
// The child can finish after the surrounding first content
// has gone out, so it must keep its own rendering buffer.
child: ScopeView::self_contained(|| child),
region,
wait,
state: State::Start,
}
}
}
impl<F, C> View for SuspenseView<F, C>
where
F: View,
C: View,
{
fn poll_first(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<ViewFirst>> {
let this = self.project();
assert!(
*this.state == State::Start,
"polled a suspense view's first content after it resolved"
);
match this.child.poll_first(cx) {
Poll::Ready(Ok(first)) => {
*this.state = if first.live {
State::Child
} else {
State::Done
};
return Poll::Ready(Ok(first));
}
Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
Poll::Pending if *this.wait => return Poll::Pending,
Poll::Pending => {}
}
match this.fallback.poll_first(cx) {
Poll::Ready(Ok(first)) => {
*this.state = State::Fallback { live: first.live };
let content = ViewBufferScope::with(|buffer| {
buffer.block(|parts| {
parts.push_region_start(*this.region);
parts.push_view_handle(first.content);
parts.push_region_end(*this.region);
})
});
Poll::Ready(Ok(ViewFirst {
content,
live: true,
}))
}
Poll::Ready(Err(error)) => Poll::Ready(Err(error)),
Poll::Pending => Poll::Pending,
}
}
fn poll_swap(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<Option<ViewSwap>>> {
let this = self.project();
let live = match *this.state {
State::Start => {
panic!("polled a suspense view for swaps before its first content resolved")
}
State::Child => {
return match this.child.poll_swap(cx) {
Poll::Ready(Ok(None)) => {
*this.state = State::Done;
Poll::Ready(Ok(None))
}
poll => poll,
};
}
State::Done => return Poll::Ready(Ok(None)),
State::Fallback { live } => live,
};
// Only replace the suspense region when its fallback went out.
match this.child.poll_first(cx) {
Poll::Ready(Ok(first)) => {
*this.state = if first.live {
State::Child
} else {
State::Done
};
return Poll::Ready(Ok(Some(ViewSwap {
region: *this.region,
replacement: first.content,
})));
}
Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
Poll::Pending => {}
}
// The child is still pending, so the fallback can keep streaming.
if live {
match this.fallback.poll_swap(cx) {
Poll::Ready(Ok(None)) => {
*this.state = State::Fallback { live: false };
Poll::Pending
}
swap => swap,
}
} else {
Poll::Pending
}
}
}