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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use std::{
future::Ready,
pin::Pin,
task::{Context, Poll},
};
use futures_util::TryFutureExt;
use pin_project_lite::pin_project;
use topcoat_core::error::Result;
use super::yielder::DriveFuture;
use crate::{
EmitToken, RegionId, View, ViewBufferScope, ViewFirst, ViewSwap,
internal::yielder::{poll_first, poll_swap},
};
pin_project! {
/// A `live!` region whose body yields its content through `emit!`.
///
/// The first emission renders in place. If the body has more updates,
/// region markers surround that content and later emissions replace it.
pub struct LiveView<Fut> {
#[pin]
body: Fut,
region: RegionId,
// A swap produced while checking whether the body is still live.
stash: Option<ViewSwap>,
}
}
impl<Fut> LiveView<Fut>
where
Fut: Future<Output = Result<EmitToken>>,
{
/// Creates a live body whose later emissions replace `region`.
#[doc(hidden)]
pub fn new(region: RegionId, body: Fut) -> Self {
Self {
body,
region,
stash: None,
}
}
}
impl LiveView<Ready<Result<EmitToken>>> {
/// Drives an emitted view until it has no more updates.
///
/// Its first content becomes the body's first emission or a replacement
/// of `region`, depending on whether the body has already emitted.
/// Swaps from the emitted view keep their own target regions. The future
/// resolves to an emission token once the view finishes, or propagates
/// its rendering error so the body can handle it.
pub fn drive<V: View>(region: RegionId, view: V) -> impl Future<Output = Result<EmitToken>> {
DriveFuture::new(EmitView::new(region, view)).map_ok(|()| EmitToken)
}
}
impl<Fut> View for LiveView<Fut>
where
Fut: Future<Output = Result<EmitToken>> + Send,
{
fn poll_first(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<ViewFirst>> {
let mut this = self.project();
match poll_first(this.body.as_mut(), cx) {
(Poll::Pending, Some(first)) => {
// The emitted child can be settled while its body still has
// more emissions. Poll the body again to determine liveness.
let (poll, yielded) = poll_swap(this.body, cx);
if let Poll::Ready(Err(error)) = poll {
return Poll::Ready(Err(error));
}
*this.stash = yielded;
let live = poll.is_pending();
let content = if live {
ViewBufferScope::with(|buffer| {
buffer.block(|parts| {
parts.push_region_start(*this.region);
parts.push_view_handle(first.content);
parts.push_region_end(*this.region);
})
})
} else {
first.content
};
Poll::Ready(Ok(ViewFirst { content, live }))
}
(Poll::Pending, None) => Poll::Pending,
(Poll::Ready(_), Some(_)) => {
panic!("live view future yielded without returning pending")
}
(Poll::Ready(Err(e)), None) => Poll::Ready(Err(e)),
(Poll::Ready(Ok(_)), None) => {
panic!("live view future completed without yielding anything")
}
}
}
fn poll_swap(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<Option<ViewSwap>>> {
let this = self.project();
if let Some(swap) = this.stash.take() {
return Poll::Ready(Ok(Some(swap)));
}
match poll_swap(this.body, cx) {
(Poll::Pending, Some(swap)) => Poll::Ready(Ok(Some(swap))),
(Poll::Pending, None) => Poll::Pending,
(Poll::Ready(_), Some(_)) => {
panic!("live view future yielded without returning pending")
}
(Poll::Ready(result), None) => Poll::Ready(result.map(|_| None)),
}
}
}
pin_project! {
/// Adapts a new view's first content to the enclosing region's lifecycle.
///
/// A later emission or error fallback starts during swap polling, so its
/// first content becomes a replacement. Subsequent child swaps pass through.
/// The child itself always receives a first-content poll before any swaps.
///
/// Completion is remembered, including when the child's first content is
/// not live, so further swap polls do not poll a finished child again.
pub struct EmitView<V> {
#[pin]
view: V,
region: RegionId,
first: bool,
done: bool,
}
}
impl<V> EmitView<V> {
/// Wraps a view whose first content can replace `region` during swap polling.
pub fn new(region: RegionId, view: V) -> Self {
Self {
view,
region,
first: true,
done: false,
}
}
}
impl<V> View for EmitView<V>
where
V: View,
{
fn poll_first(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<ViewFirst>> {
let this = self.project();
match this.view.poll_first(cx) {
Poll::Ready(Ok(first)) => {
*this.first = false;
*this.done = !first.live;
Poll::Ready(Ok(first))
}
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();
if *this.done {
return Poll::Ready(Ok(None));
}
if *this.first {
match this.view.poll_first(cx) {
Poll::Ready(Ok(first)) => {
*this.first = false;
*this.done = !first.live;
Poll::Ready(Ok(Some(ViewSwap {
region: *this.region,
replacement: first.content,
})))
}
Poll::Ready(Err(error)) => Poll::Ready(Err(error)),
Poll::Pending => Poll::Pending,
}
} else {
match this.view.poll_swap(cx) {
Poll::Ready(Ok(None)) => {
*this.done = true;
Poll::Ready(Ok(None))
}
poll => poll,
}
}
}
}