euv_core/reactive/scope/impl.rs
1use super::*;
2
3/// Ownership lifecycle operations for `Scope`.
4impl Scope {
5 /// Creates a new scope and executes `body` inside it, returning both the scope handle and result.
6 ///
7 /// # Arguments
8 ///
9 /// - `F` - Closure executed while this scope is active on the scope stack.
10 ///
11 /// # Returns
12 ///
13 /// - `(Self, R)` - The created scope handle and closure result.
14 #[inline]
15 pub fn run<F, R>(body: F) -> (Self, R)
16 where
17 F: FnOnce(Self) -> R,
18 {
19 let id: ScopeId = with_runtime(
20 |rt: &mut ReactiveRuntime| {
21 let created: ScopeId = rt.create_scope();
22 rt.get_mut_scope_stack().push(created);
23 created
24 },
25 ScopeId::default(),
26 );
27 let scope: Self = Self { id };
28 let result: R = body(scope);
29 with_runtime(
30 |rt: &mut ReactiveRuntime| {
31 rt.get_mut_scope_stack().pop();
32 },
33 (),
34 );
35 (scope, result)
36 }
37
38 /// Executes `body` inside an existing scope so newly created signals, memos, and effects attach to it.
39 ///
40 /// # Arguments
41 ///
42 /// - `&Self` - Target ownership scope.
43 /// - `F` - Closure executed under this scope.
44 ///
45 /// # Returns
46 ///
47 /// - `R` - Value returned by `body`.
48 #[inline]
49 pub fn enter<F, R>(&self, body: F) -> R
50 where
51 F: FnOnce() -> R,
52 {
53 let id: ScopeId = self.id();
54 with_runtime(
55 |rt: &mut ReactiveRuntime| {
56 rt.get_mut_scope_stack().push(id);
57 },
58 (),
59 );
60 let result: R = body();
61 with_runtime(
62 |rt: &mut ReactiveRuntime| {
63 rt.get_mut_scope_stack().pop();
64 },
65 (),
66 );
67 result
68 }
69
70 /// Returns the underlying `ScopeId`.
71 ///
72 /// # Arguments
73 ///
74 /// - `&Self` - Scope handle.
75 ///
76 /// # Returns
77 ///
78 /// - `ScopeId` - Underlying arena identifier.
79 #[must_use]
80 #[inline(always)]
81 pub fn id(&self) -> ScopeId {
82 let Self { id } = *self;
83 id
84 }
85
86 /// Disposes this scope, all child scopes, owned reactive nodes, and executes all registered cleanups.
87 ///
88 /// # Arguments
89 ///
90 /// - `&Self` - Scope handle to dispose.
91 #[inline]
92 pub fn dispose(&self) {
93 let id: ScopeId = self.id();
94 let cleanups: Vec<Box<dyn FnOnce()>> = with_runtime(
95 |rt: &mut ReactiveRuntime| rt.dispose_scope_collect(id),
96 Vec::new(),
97 );
98 for cleanup in cleanups.into_iter() {
99 cleanup();
100 }
101 }
102
103 /// Freezes all effects owned by this scope and its descendant scopes while tracking cross-page signal dirtiness.
104 ///
105 /// # Arguments
106 ///
107 /// - `&Self` - Scope handle to freeze.
108 #[inline(always)]
109 pub fn freeze(&self) {
110 let id: ScopeId = self.id();
111 let _: bool = with_runtime(
112 |rt: &mut ReactiveRuntime| rt.set_scope_frozen(id, true),
113 false,
114 );
115 }
116
117 /// Resumes this scope and all descendant scopes, atomically flushing any coalesced cross-page updates once.
118 ///
119 /// # Arguments
120 ///
121 /// - `&Self` - Scope handle to resume.
122 #[inline(always)]
123 pub fn resume(&self) {
124 let id: ScopeId = self.id();
125 let should_flush: bool = with_runtime(
126 |rt: &mut ReactiveRuntime| rt.set_scope_frozen(id, false),
127 false,
128 );
129 if should_flush {
130 flush_effects();
131 }
132 }
133
134 /// Returns `true` when this scope is currently suspended via `Scope::freeze`.
135 ///
136 /// # Arguments
137 ///
138 /// - `&Self` - Scope handle to inspect.
139 ///
140 /// # Returns
141 ///
142 /// - `bool` - Whether this scope is frozen.
143 #[must_use]
144 #[inline(always)]
145 pub fn is_frozen(&self) -> bool {
146 let id: ScopeId = self.id();
147 with_runtime(|rt: &mut ReactiveRuntime| rt.is_scope_frozen(id), false)
148 }
149}