euv-core 0.53.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Ownership lifecycle operations for `Scope`.
impl Scope {
    /// Creates a new scope and executes `body` inside it, returning both the scope handle and result.
    ///
    /// # Arguments
    ///
    /// - `CallbackFn` - Closure executed while this scope is active on the scope stack.
    ///
    /// # Returns
    ///
    /// - `(Self, ReturnValue)` - The created scope handle and closure result.
    #[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)
    }

    /// Executes `body` inside an existing scope so newly created signals, memos, and effects attach to it.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Target ownership scope.
    /// - `CallbackFn` - Closure executed under this scope.
    ///
    /// # Returns
    ///
    /// - `ReturnValue` - Value returned by `body`.
    #[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
    }

    /// Returns the underlying `ScopeId`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Scope handle.
    ///
    /// # Returns
    ///
    /// - `ScopeId` - Underlying arena identifier.
    #[must_use]
    #[inline(always)]
    pub fn id(&self) -> ScopeId {
        let Self { id } = *self;
        id
    }

    /// Disposes this scope, all child scopes, owned reactive nodes, and executes all registered cleanups.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Scope handle to dispose.
    #[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();
        }
    }

    /// Freezes all effects owned by this scope and its descendant scopes while tracking cross-page signal dirtiness.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Scope handle to freeze.
    #[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,
        );
    }

    /// Resumes this scope and all descendant scopes, atomically flushing any coalesced cross-page updates once.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Scope handle to resume.
    #[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();
        }
    }

    /// Returns `true` when this scope is currently suspended via `Scope::freeze`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Scope handle to inspect.
    ///
    /// # Returns
    ///
    /// - `bool` - Whether this scope is frozen.
    #[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,
        )
    }
}