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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//! The app's JS functions Rust components render with
//! ([`crate::element::Renderer`]): which (prop, key) of which component's host
//! is placed where, the value JS rendered last for each, and the holder
//! view JS rendered it into.
//!
//! A placeholder is a view of a component's view whose element carries a
//! [`RenderPlace`] (`Attrs::render`), registered when the element is
//! applied and forgotten when it changes or goes. After each commit
//! ([`Core::flush_renders`]) the core asks JS to render each (prop, key)
//! placed with a value it did not render yet (`JsEvent::Render`), and
//! tells it when none is placed any more (`JsEvent::Unrender`). JS inserts
//! its holder (`componentRender`) into the component's host; the core
//! places it as the only child of the (prop, key)'s placeholder (the last
//! one placed, when the view places a key twice). Apps that render nothing
//! this way pay nothing: every path starts with a check of an empty map.
use std::{rc::Rc, sync::Arc};
use rustc_hash::FxHashMap;
use super::{Core, Key, NO_KEY};
use crate::{
NodeId, Op, TextEngine,
element::{
JsEvent, RenderValue, SharedString,
renderer::{RenderAt, RenderPlace},
},
};
/// The renders of the components' views.
#[derive(Default)]
pub(super) struct Renders {
/// Placeholders -> what they place.
places: FxHashMap<NodeId, Rc<RenderPlace>>,
/// (the component's host, the prop and key as JS names them) -> its
/// state.
slots: FxHashMap<(NodeId, Arc<str>), RenderSlot>,
/// The holders JS made -> their (prop, key), and the host JS inserted
/// them into (0: none yet).
holders: FxHashMap<NodeId, (Arc<str>, NodeId)>,
/// The slots whose places changed since the last flush.
dirty: Vec<(NodeId, Arc<str>)>,
}
impl Renders {
/// Whether no placeholder is placed.
pub(super) fn is_empty(&self) -> bool {
self.places.is_empty()
}
/// Whether view `id` is a placeholder.
pub(super) fn is_place(&self, id: NodeId) -> bool {
!self.places.is_empty() && self.places.contains_key(&id)
}
}
/// A (prop, key) of a component's host.
struct RenderSlot {
prop: &'static str,
key: SharedString,
/// The placeholders placing it, oldest first: the last one shows it.
places: Vec<NodeId>,
/// The value JS was asked to render last.
sent: Option<Rc<dyn RenderValue>>,
/// JS's holder of what it rendered.
holder: Option<NodeId>,
}
impl<M: TextEngine> Core<M> {
/// Element `id`'s render changed from `old` to `new` (its attributes
/// were applied).
pub(super) fn update_render(
&mut self,
id: NodeId,
old: Option<&RenderAt>,
new: Option<&RenderAt>,
) {
// The same (prop, key) with another value: the holder stays.
if let (Some(old), Some(new)) = (old, new)
&& old.0.host == new.0.host
&& old.0.prop == new.0.prop
&& old.0.key == new.0.key
{
let renders = &mut self.components.renders;
renders.places.insert(id, new.0.clone());
renders.dirty.push((new.0.host, new.0.slot()));
return;
}
if old.is_some() {
self.forget_render_place(id);
}
let Some(new) = new else { return };
let slot = new.0.slot();
let key = (new.0.host, slot.clone());
let renders = &mut self.components.renders;
renders.places.insert(id, new.0.clone());
let entry = renders
.slots
.entry(key.clone())
.or_insert_with(|| RenderSlot {
prop: new.0.prop,
key: new.0.key.clone(),
places: Vec::new(),
sent: None,
holder: None,
});
entry.places.retain(|&p| p != id);
entry.places.push(id);
renders.dirty.push(key);
}
/// Placeholder `id` no longer places its render (it changed or went):
/// the holder leaves it.
pub(super) fn forget_render_place(&mut self, id: NodeId) {
let Some(place) = self.components.renders.places.remove(&id) else {
return;
};
let key = (place.host, place.slot());
let mut leaves = None;
if let Some(slot) = self.components.renders.slots.get(&key)
&& slot.places.last() == Some(&id)
{
leaves = slot.holder;
}
let renders = &mut self.components.renders;
if let Some(slot) = renders.slots.get_mut(&key) {
slot.places.retain(|&p| p != id);
}
renders.dirty.push(key);
if let Some(holder) = leaves {
self.detach_id(holder);
}
}
/// `componentRender` on a view: JS's holder of a render.
pub(super) fn set_render_holder(&mut self, id: NodeId, slot: Arc<str>) {
self.components.renders.holders.insert(id, (slot, 0));
}
/// Whether `id` is a render's holder.
pub(super) fn is_render_holder(&self, id: NodeId) -> bool {
!self.components.renders.holders.is_empty()
&& self.components.renders.holders.contains_key(&id)
}
/// JS inserts holder `holder` into component host `host`: it goes into
/// its placeholder, replacing the holder rendered before; with none
/// placed, it waits out of the tree for JS to release it (an
/// `Unrender` is on its way).
pub(super) fn insert_render_holder(&mut self, host: NodeId, holder: NodeId) {
let renders = &mut self.components.renders;
let Some(entry) = renders.holders.get_mut(&holder) else {
return;
};
entry.1 = host;
let key = (host, entry.0.clone());
let place = self
.components
.renders
.slots
.get(&key)
.and_then(|slot| slot.places.last().copied());
let old = match self.components.renders.slots.get_mut(&key) {
Some(slot) => slot.holder.replace(holder).filter(|&h| h != holder),
None => None,
};
if let Some(old) = old {
self.detach_id(old);
}
match place {
Some(place) => {
let _ = self.apply_op(Op::Insert {
parent: place,
child: holder,
before: None,
});
}
None => self.detach_id(holder),
}
}
/// JS removes holder `holder` from its host (or releases it).
pub(super) fn remove_render_holder(&mut self, holder: NodeId, released: bool) {
let renders = &mut self.components.renders;
let entry = if released {
renders.holders.remove(&holder)
} else {
renders.holders.get(&holder).cloned()
};
if let Some((slot, host)) = entry
&& let Some(slot) = renders.slots.get_mut(&(host, slot))
&& slot.holder == Some(holder)
{
slot.holder = None;
}
if !released {
self.detach_id(holder);
}
}
/// The list cells whose content waits for JS: they (or a cell inside
/// them) hold the placeholder of a render whose holder has not arrived
/// yet, into `out`, sorted. Their size is the size of empty
/// placeholders until it does, so their lists do not measure them.
pub(super) fn cells_awaiting_renders(&self, out: &mut Vec<Key>) {
out.clear();
let renders = &self.components.renders;
if self.lists.is_empty() || renders.places.is_empty() {
return;
}
for slot in renders.slots.values() {
if slot.holder.is_some() {
continue;
}
let Some(mut k) = (slot.places.last()).and_then(|&p| self.key(p).ok()) else {
continue;
};
loop {
let parent = self.nodes[k as usize].parent;
if parent == NO_KEY {
break;
}
// A list's children are its cells.
if self.nodes[parent as usize].list.is_some() {
out.push(k);
}
k = parent;
}
}
out.sort_unstable();
out.dedup();
}
/// After a commit's views: asks JS to render what is placed with a
/// value it has not rendered, puts holders in their placeholders, and
/// tells JS of the renders no longer placed.
pub(super) fn flush_renders(&mut self) {
if self.components.renders.dirty.is_empty() {
return;
}
let dirty = std::mem::take(&mut self.components.renders.dirty);
for key in dirty {
let Some(slot) = self.components.renders.slots.get(&key) else {
continue;
};
let Some(&place) = slot.places.last() else {
let renders = &mut self.components.renders;
let slot = renders.slots.remove(&key).expect("a slot");
if slot.sent.is_some() {
self.events.push(JsEvent::Unrender {
host: key.0,
prop: slot.prop,
key: slot.key,
});
}
if let Some(holder) = slot.holder {
self.detach_id(holder);
}
continue;
};
let renders = &mut self.components.renders;
let slot = renders.slots.get_mut(&key).expect("a slot");
let placed = &renders.places[&place];
let value = placed.value.clone();
if !slot.sent.as_ref().is_some_and(|v| v.same(&*value)) {
slot.sent = Some(value.clone());
self.events.push(JsEvent::Render {
host: key.0,
prop: slot.prop,
key: slot.key.clone(),
value,
func: placed.func.clone(),
});
}
// Its placeholder may be another one now.
if let Some(holder) = slot.holder {
let placed = self.key(holder).is_ok_and(|k| {
let parent = self.nodes[k as usize].parent;
parent != NO_KEY && self.nodes[parent as usize].id == place
});
if !placed {
let _ = self.apply_op(Op::Insert {
parent: place,
child: holder,
before: None,
});
}
}
}
}
}