use super::{GroupScopeEntry, reparent_restored_scopes};
use crate::{
AnchorId, Composer, LocalStackSnapshot, NodeId, RecomposeOptions, RecomposeScope,
RetentionMode, ScopeRead, StateId,
slot::{ActiveGroupId, GroupStartKind},
};
pub(crate) enum ScopeFrame {
Made(RecomposeScope),
Deferred(DeferredScope),
}
pub(crate) struct DeferredScope {
pub(crate) group: ActiveGroupId,
anchor: AnchorId,
parent_hint: Option<NodeId>,
locals: LocalStackSnapshot,
pub(crate) recompose: Option<fn(&Composer)>,
#[cfg(feature = "inspection")]
source: Option<std::rc::Rc<[crate::source_trace::SourceLocation]>>,
pub(crate) forced: bool,
pub(crate) composed_once: bool,
pub(crate) made: Option<RecomposeScope>,
}
impl ScopeFrame {
pub(crate) fn scope(&self) -> Option<&RecomposeScope> {
match self {
Self::Made(scope) => Some(scope),
Self::Deferred(deferred) => deferred.made.as_ref(),
}
}
fn has_recompose_callback(&self) -> bool {
match self {
Self::Made(scope) => scope.has_recompose_callback(),
Self::Deferred(deferred) => match &deferred.made {
Some(scope) => scope.has_recompose_callback(),
None => deferred.recompose.is_some(),
},
}
}
}
pub(super) fn may_defer(options: RecomposeOptions, kind: GroupStartKind) -> bool {
!options.force_reuse
&& !options.force_recompose
&& matches!(options.retention, RetentionMode::DisposeWhenInactive)
&& !matches!(kind, GroupStartKind::Restored)
}
#[expect(clippy::multiple_inherent_impl, reason = "one file per concern")]
impl Composer {
pub fn current_recompose_scope(&self) -> Option<RecomposeScope> {
let mut stack = self.core.scope_stack.borrow_mut();
let top = stack.len().checked_sub(1)?;
Some(self.frame_scope(&mut stack, top))
}
pub(crate) fn peek_recompose_scope(&self) -> Option<RecomposeScope> {
self.core
.scope_stack
.borrow()
.last()
.and_then(ScopeFrame::scope)
.cloned()
}
pub(crate) fn note_state_read(
&self,
state_id: Option<StateId>,
) -> Option<(RecomposeScope, ScopeRead)> {
let scope = {
let mut stack = self.core.scope_stack.borrow_mut();
let index = stack
.iter()
.rposition(ScopeFrame::has_recompose_callback)
.or_else(|| stack.len().checked_sub(1))?;
self.frame_scope(&mut stack, index)
};
let read = state_id.map_or(ScopeRead::NotReading, |id| scope.note_read(id));
(!matches!(read, ScopeRead::Watched)).then_some((scope, read))
}
pub(super) fn defers_scopes_here(&self) -> bool {
!self.core.scope_stack.borrow().is_empty()
&& !self.core.shared_state.pausing.is_holding()
&& self
.subcompose_stack()
.last()
.is_none_or(|frame| !frame.scopes.is_empty())
}
pub(super) fn push_deferred_scope(
&self,
(group, anchor): (ActiveGroupId, AnchorId),
kind: GroupStartKind,
parent_hint: Option<NodeId>,
) {
let forced = self.core.locals_changed.get() || self.core.reused_depth.get() > 0;
let frame = DeferredScope {
group,
anchor,
parent_hint,
locals: self.core.local_stack.borrow().clone(),
recompose: None,
#[cfg(feature = "inspection")]
source: None,
forced,
composed_once: !matches!(kind, GroupStartKind::Inserted),
made: None,
};
self.scope_stack().push(ScopeFrame::Deferred(frame));
}
pub(super) fn frame_scope(&self, stack: &mut [ScopeFrame], index: usize) -> RecomposeScope {
let (below, frame) = stack.split_at_mut(index);
let deferred = match &mut frame[0] {
ScopeFrame::Made(scope) => return scope.clone(),
ScopeFrame::Deferred(deferred) => deferred,
};
if let Some(scope) = &deferred.made {
return scope.clone();
}
let owner = below.iter().rev().find_map(ScopeFrame::scope);
let scope = RecomposeScope::new(self.runtime_handle());
match owner {
Some(owner) => scope.set_parent_scope(Some(owner)),
None => scope
.set_lifetime_owner_scope(self.core.subcomposition_owner_scope.borrow().as_ref()),
}
scope.set_group_anchor(deferred.anchor);
scope.snapshot_locals(&deferred.locals);
scope.set_parent_hint(deferred.parent_hint);
if let Some(recompose) = deferred.recompose {
scope.set_recompose_fn(recompose);
#[cfg(feature = "inspection")]
if let Some(source) = deferred.source.take() {
scope.set_recompose_source(source);
}
}
if deferred.composed_once {
scope.mark_composed_once();
}
scope.begin_reads();
if let Some(frame) = self.subcompose_stack().last_mut() {
frame.scopes.push(scope.clone());
}
deferred.made = Some(scope.clone());
scope
}
pub(super) fn enter_group_scope(
&self,
scope_ref: &RecomposeScope,
entry: &GroupScopeEntry<'_>,
) -> bool {
let GroupScopeEntry {
options,
start_kind,
host,
group,
parent_hint,
} = *entry;
let reused = !scope_ref.is_active() && scope_ref.has_composed_once();
scope_ref.reactivate();
{
let mut stack = self.scope_stack();
if matches!(options.retention, RetentionMode::RetainWhenInactive)
&& let Some(top) = stack.len().checked_sub(1)
{
self.frame_scope(&mut stack, top);
}
match stack.iter().rev().find_map(ScopeFrame::scope) {
Some(parent_scope) => scope_ref.set_parent_scope(Some(parent_scope)),
None => scope_ref.set_lifetime_owner_scope(
self.core.subcomposition_owner_scope.borrow().as_ref(),
),
}
stack.push(ScopeFrame::Made(scope_ref.clone()));
}
scope_ref.set_retention_mode(options.retention);
let restored = matches!(start_kind, GroupStartKind::Restored);
let recompose = options.force_recompose | self.core.locals_changed.get() | restored;
let forces_content = reused & !restored & !options.force_reuse;
if recompose | forces_content {
scope_ref.force_recompose();
} else if options.force_reuse {
scope_ref.force_reuse();
}
if forces_content {
let depth = &self.core.reused_depth;
depth.set(depth.get() + 1);
}
scope_ref.set_slots_host(host);
{
let mut stack = self.subcompose_stack();
if let Some(frame) = stack.last_mut() {
frame.scopes.push(scope_ref.clone());
}
}
scope_ref.snapshot_locals(&self.core.local_stack.borrow());
if restored {
reparent_restored_scopes(host, group, scope_ref.parent_hint(), parent_hint);
}
scope_ref.set_parent_hint(parent_hint);
forces_content
}
fn with_last_deferred<R>(&self, read: fn(&mut DeferredScope) -> R) -> Option<R> {
match self.scope_stack().last_mut() {
Some(ScopeFrame::Deferred(deferred)) => Some(read(deferred)),
_ => None,
}
}
pub(super) fn deferred_scope_forced(&self) -> bool {
self.with_last_deferred(|deferred| deferred.forced)
.unwrap_or(false)
}
pub(super) fn deferred_scope_composed_once(&self) -> bool {
self.with_last_deferred(|deferred| deferred.composed_once)
.unwrap_or(false)
}
pub(super) fn set_deferred_recompose_fn(&self, body: fn(&Self)) {
if let Some(ScopeFrame::Deferred(deferred)) = self.scope_stack().last_mut() {
match &deferred.made {
Some(scope) => scope.set_recompose_fn(body),
None => {
deferred.recompose = Some(body);
#[cfg(feature = "inspection")]
{
deferred.source = Some(crate::source_trace::current_source_trace());
}
}
}
}
}
pub(super) fn scope_made_by_body(&self) -> Option<(ActiveGroupId, RecomposeScope)> {
match self.scope_stack().last() {
Some(ScopeFrame::Deferred(deferred)) => deferred
.made
.as_ref()
.map(|scope| (deferred.group, scope.clone())),
_ => None,
}
}
pub(super) fn leave_forced_content(&self) {
let depth = &self.core.reused_depth;
depth.set(depth.get().saturating_sub(1));
}
pub(crate) fn pop_closed_scope(&self) {
if self.scope_stack().pop().is_none() {
log::error!("scope stack underflow while closing a group");
}
}
}