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