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