1use super::{GroupScopeEntry, reparent_restored_scopes};
5use crate::{
6 AnchorId, Composer, LocalStackSnapshot, NodeId, RecomposeOptions, RecomposeScope,
7 RetentionMode, ScopeRead, StateId,
8 slot::{ActiveGroupId, GroupStartKind},
9};
10
11pub(crate) enum ScopeFrame {
15 Made(RecomposeScope),
16 Deferred(DeferredScope),
17}
18
19pub(crate) struct DeferredScope {
22 pub(crate) group: ActiveGroupId,
23 anchor: AnchorId,
24 parent_hint: Option<NodeId>,
25 locals: LocalStackSnapshot,
26 pub(crate) recompose: Option<fn(&Composer)>,
27 #[cfg(feature = "inspection")]
30 source: Option<std::rc::Rc<[crate::source_trace::SourceLocation]>>,
31 pub(crate) forced: bool,
34 pub(crate) composed_once: bool,
35 pub(crate) made: Option<RecomposeScope>,
37}
38
39impl ScopeFrame {
40 pub(crate) fn scope(&self) -> Option<&RecomposeScope> {
41 match self {
42 Self::Made(scope) => Some(scope),
43 Self::Deferred(deferred) => deferred.made.as_ref(),
44 }
45 }
46
47 fn has_recompose_callback(&self) -> bool {
51 match self {
52 Self::Made(scope) => scope.has_recompose_callback(),
53 Self::Deferred(deferred) => match &deferred.made {
54 Some(scope) => scope.has_recompose_callback(),
55 None => deferred.recompose.is_some(),
56 },
57 }
58 }
59}
60
61pub(super) fn may_defer(options: RecomposeOptions, kind: GroupStartKind) -> bool {
64 !options.force_reuse
65 && !options.force_recompose
66 && matches!(options.retention, RetentionMode::DisposeWhenInactive)
67 && !matches!(kind, GroupStartKind::Restored)
68}
69
70#[expect(clippy::multiple_inherent_impl, reason = "one file per concern")]
71impl Composer {
72 pub fn current_recompose_scope(&self) -> Option<RecomposeScope> {
74 let mut stack = self.core.scope_stack.borrow_mut();
75 let top = stack.len().checked_sub(1)?;
76 Some(self.frame_scope(&mut stack, top))
77 }
78
79 pub(crate) fn peek_recompose_scope(&self) -> Option<RecomposeScope> {
82 self.core
83 .scope_stack
84 .borrow()
85 .last()
86 .and_then(ScopeFrame::scope)
87 .cloned()
88 }
89
90 pub(crate) fn note_state_read(
91 &self,
92 state_id: Option<StateId>,
93 ) -> Option<(RecomposeScope, ScopeRead)> {
94 let scope = {
95 let mut stack = self.core.scope_stack.borrow_mut();
96 let index = stack
97 .iter()
98 .rposition(ScopeFrame::has_recompose_callback)
99 .or_else(|| stack.len().checked_sub(1))?;
100 self.frame_scope(&mut stack, index)
101 };
102 let read = state_id.map_or(ScopeRead::NotReading, |id| scope.note_read(id));
103 (!matches!(read, ScopeRead::Watched)).then_some((scope, read))
104 }
105
106 pub(super) fn defers_scopes_here(&self) -> bool {
110 !self.core.scope_stack.borrow().is_empty()
111 && !self.core.shared_state.pausing.is_holding()
112 && self
113 .subcompose_stack()
114 .last()
115 .is_none_or(|frame| !frame.scopes.is_empty())
116 }
117
118 pub(super) fn push_deferred_scope(
121 &self,
122 (group, anchor): (ActiveGroupId, AnchorId),
123 kind: GroupStartKind,
124 parent_hint: Option<NodeId>,
125 ) {
126 let forced = self.core.locals_changed.get() || self.core.reused_depth.get() > 0;
127 let frame = DeferredScope {
128 group,
129 anchor,
130 parent_hint,
131 locals: self.core.local_stack.borrow().clone(),
132 recompose: None,
133 #[cfg(feature = "inspection")]
134 source: None,
135 forced,
136 composed_once: !matches!(kind, GroupStartKind::Inserted),
137 made: None,
138 };
139 self.scope_stack().push(ScopeFrame::Deferred(frame));
140 }
141
142 pub(super) fn frame_scope(&self, stack: &mut [ScopeFrame], index: usize) -> RecomposeScope {
145 let (below, frame) = stack.split_at_mut(index);
146 let deferred = match &mut frame[0] {
147 ScopeFrame::Made(scope) => return scope.clone(),
148 ScopeFrame::Deferred(deferred) => deferred,
149 };
150 if let Some(scope) = &deferred.made {
151 return scope.clone();
152 }
153 let owner = below.iter().rev().find_map(ScopeFrame::scope);
154 let scope = RecomposeScope::new(self.runtime_handle());
155 match owner {
156 Some(owner) => scope.set_parent_scope(Some(owner)),
157 None => scope
158 .set_lifetime_owner_scope(self.core.subcomposition_owner_scope.borrow().as_ref()),
159 }
160 scope.set_group_anchor(deferred.anchor);
163 scope.snapshot_locals(&deferred.locals);
164 scope.set_parent_hint(deferred.parent_hint);
165 if let Some(recompose) = deferred.recompose {
166 scope.set_recompose_fn(recompose);
167 #[cfg(feature = "inspection")]
168 if let Some(source) = deferred.source.take() {
169 scope.set_recompose_source(source);
170 }
171 }
172 if deferred.composed_once {
173 scope.mark_composed_once();
174 }
175 scope.begin_reads();
176 if let Some(frame) = self.subcompose_stack().last_mut() {
177 frame.scopes.push(scope.clone());
178 }
179 deferred.made = Some(scope.clone());
180 scope
181 }
182
183 pub(super) fn enter_group_scope(
187 &self,
188 scope_ref: &RecomposeScope,
189 entry: &GroupScopeEntry<'_>,
190 ) -> bool {
191 let GroupScopeEntry {
192 options,
193 start_kind,
194 host,
195 group,
196 parent_hint,
197 } = *entry;
198 let reused = !scope_ref.is_active() && scope_ref.has_composed_once();
203 scope_ref.reactivate();
204 {
205 let mut stack = self.scope_stack();
206 if matches!(options.retention, RetentionMode::RetainWhenInactive)
209 && let Some(top) = stack.len().checked_sub(1)
210 {
211 self.frame_scope(&mut stack, top);
212 }
213 match stack.iter().rev().find_map(ScopeFrame::scope) {
214 Some(parent_scope) => scope_ref.set_parent_scope(Some(parent_scope)),
215 None => scope_ref.set_lifetime_owner_scope(
216 self.core.subcomposition_owner_scope.borrow().as_ref(),
217 ),
218 }
219 stack.push(ScopeFrame::Made(scope_ref.clone()));
220 }
221 scope_ref.set_retention_mode(options.retention);
222
223 let restored = matches!(start_kind, GroupStartKind::Restored);
226 let recompose = options.force_recompose | self.core.locals_changed.get() | restored;
227 let forces_content = reused & !restored & !options.force_reuse;
230 if recompose | forces_content {
231 scope_ref.force_recompose();
232 } else if options.force_reuse {
233 scope_ref.force_reuse();
234 }
235 if forces_content {
236 let depth = &self.core.reused_depth;
237 depth.set(depth.get() + 1);
238 }
239
240 scope_ref.set_slots_host(host);
241
242 {
243 let mut stack = self.subcompose_stack();
244 if let Some(frame) = stack.last_mut() {
245 frame.scopes.push(scope_ref.clone());
246 }
247 }
248
249 scope_ref.snapshot_locals(&self.core.local_stack.borrow());
250 if restored {
251 reparent_restored_scopes(host, group, scope_ref.parent_hint(), parent_hint);
252 }
253 scope_ref.set_parent_hint(parent_hint);
254 forces_content
255 }
256
257 fn with_last_deferred<R>(&self, read: fn(&mut DeferredScope) -> R) -> Option<R> {
259 match self.scope_stack().last_mut() {
260 Some(ScopeFrame::Deferred(deferred)) => Some(read(deferred)),
261 _ => None,
262 }
263 }
264
265 pub(super) fn deferred_scope_forced(&self) -> bool {
268 self.with_last_deferred(|deferred| deferred.forced)
269 .unwrap_or(false)
270 }
271
272 pub(super) fn deferred_scope_composed_once(&self) -> bool {
275 self.with_last_deferred(|deferred| deferred.composed_once)
276 .unwrap_or(false)
277 }
278
279 pub(super) fn set_deferred_recompose_fn(&self, body: fn(&Self)) {
282 if let Some(ScopeFrame::Deferred(deferred)) = self.scope_stack().last_mut() {
283 match &deferred.made {
284 Some(scope) => scope.set_recompose_fn(body),
285 None => {
286 deferred.recompose = Some(body);
287 #[cfg(feature = "inspection")]
288 {
289 deferred.source = Some(crate::source_trace::current_source_trace());
290 }
291 }
292 }
293 }
294 }
295
296 pub(super) fn scope_made_by_body(&self) -> Option<(ActiveGroupId, RecomposeScope)> {
298 match self.scope_stack().last() {
299 Some(ScopeFrame::Deferred(deferred)) => deferred
300 .made
301 .as_ref()
302 .map(|scope| (deferred.group, scope.clone())),
303 _ => None,
304 }
305 }
306
307 pub(super) fn leave_forced_content(&self) {
309 let depth = &self.core.reused_depth;
310 depth.set(depth.get().saturating_sub(1));
311 }
312
313 pub(crate) fn pop_closed_scope(&self) {
314 if self.scope_stack().pop().is_none() {
315 log::error!("scope stack underflow while closing a group");
316 }
317 }
318}