use super::*;
impl Scope {
pub fn run<F, R>(body: F) -> (Self, R)
where
F: FnOnce(Self) -> R,
{
let id: ScopeId = with_runtime(
|rt: &mut ReactiveRuntime| {
let created: ScopeId = rt.create_scope();
rt.get_mut_scope_stack().push(created);
created
},
ScopeId::default(),
);
let scope: Self = Self { id };
let result: R = body(scope);
with_runtime(
|rt: &mut ReactiveRuntime| {
rt.get_mut_scope_stack().pop();
},
(),
);
(scope, result)
}
pub fn enter<F, R>(&self, body: F) -> R
where
F: FnOnce() -> R,
{
let id: ScopeId = self.id();
with_runtime(
|rt: &mut ReactiveRuntime| {
rt.get_mut_scope_stack().push(id);
},
(),
);
let result: R = body();
with_runtime(
|rt: &mut ReactiveRuntime| {
rt.get_mut_scope_stack().pop();
},
(),
);
result
}
#[must_use]
pub fn id(&self) -> ScopeId {
let Self { id } = *self;
id
}
pub fn dispose(&self) {
let id: ScopeId = self.id();
let cleanups: Vec<Box<dyn FnOnce()>> = with_runtime(
|rt: &mut ReactiveRuntime| rt.dispose_scope_collect(id),
Vec::new(),
);
for cleanup in cleanups.into_iter() {
cleanup();
}
}
pub fn freeze(&self) {
let id: ScopeId = self.id();
let _: bool = with_runtime(
|rt: &mut ReactiveRuntime| rt.set_scope_frozen(id, true),
false,
);
}
pub fn resume(&self) {
let id: ScopeId = self.id();
let should_flush: bool = with_runtime(
|rt: &mut ReactiveRuntime| rt.set_scope_frozen(id, false),
false,
);
if should_flush {
flush_effects();
}
}
#[must_use]
pub fn is_frozen(&self) -> bool {
let id: ScopeId = self.id();
with_runtime(|rt: &mut ReactiveRuntime| rt.is_scope_frozen(id), false)
}
}