1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use cfg_if::cfg_if;

use crate::{hydration::SharedContext, EffectId, ResourceId, Runtime, SignalId};
use crate::{PinnedFuture, SuspenseContext};
use futures::stream::FuturesUnordered;
use std::collections::HashMap;
use std::fmt::Debug;
use std::{future::Future, pin::Pin};

#[must_use = "Scope will leak memory if the disposer function is never called"]
/// Creates a child reactive scope and runs the function within it. This is useful for applications
/// like a list or a router, which may want to create child scopes and dispose of them when
/// they are no longer needed (e.g., a list item has been destroyed or the user has navigated away
/// from the route.)
pub fn create_scope(f: impl FnOnce(Scope) + 'static) -> ScopeDisposer {
    let runtime = Box::leak(Box::new(Runtime::new()));
    runtime.run_scope_undisposed(f, None).2
}

/// Creates a temporary scope, runs the given function, disposes of the scope,
/// and returns the value returned from the function. This is very useful for short-lived
/// applications like SSR, where actual reactivity is not required beyond the end
/// of the synchronous operation.
pub fn run_scope<T>(f: impl FnOnce(Scope) -> T + 'static) -> T {
    // TODO this leaks the runtime — should unsafely upgrade the lifetime, and then drop it after the scope is run
    let runtime = Box::leak(Box::new(Runtime::new()));
    runtime.run_scope(f, None)
}

#[must_use = "Scope will leak memory if the disposer function is never called"]
/// Creates a temporary scope and run the given function without disposing of the scope.
/// If you do not dispose of the scope on your own, memory will leak.
pub fn run_scope_undisposed<T>(
    f: impl FnOnce(Scope) -> T + 'static,
) -> (T, ScopeId, ScopeDisposer) {
    // TODO this leaks the runtime — should unsafely upgrade the lifetime, and then drop it after the scope is run
    let runtime = Box::leak(Box::new(Runtime::new()));
    runtime.run_scope_undisposed(f, None)
}

/// A Each scope can have
/// child scopes, and may in turn have a parent.
///
/// Scopes manage memory within the reactive system. When a scope is disposed, its
/// cleanup functions run and the signals, effects, memos, resources, and contexts
/// associated with it no longer exist and should no longer be accessed.
///
/// You generally won’t need to create your own scopes when writing application code.
/// However, they’re very useful for managing control flow within an application or library.
/// For example, if you are writing a keyed list component, you will want to create a child scope
/// for each row in the list so that you can dispose of its associated signals, etc.
/// when it is removed from the list.
///
/// Every other function in this crate takes a `Scope` as its first argument. Since `Scope`
/// is [Copy] and `'static` this does not add much overhead or lifetime complexity.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Scope {
    pub(crate) runtime: &'static Runtime,
    pub(crate) id: ScopeId,
}

impl Scope {
    /// The unique identifier for this scope.
    pub fn id(&self) -> ScopeId {
        self.id
    }

    /// Creates a child scope and runs the given function within it.
    ///
    /// The child scope has its own lifetime and disposer, but will be disposed when the parent is
    /// disposed, if it has not been already.
    pub fn child_scope(self, f: impl FnOnce(Scope)) -> ScopeDisposer {
        let (_, child_id, disposer) = self.runtime.run_scope_undisposed(f, Some(self));
        let mut children = self.runtime.scope_children.borrow_mut();
        children
            .entry(self.id)
            .expect("trying to add a child to a Scope that has already been disposed")
            .or_default()
            .push(child_id);
        disposer
    }

    /// Suspends reactive tracking while running the given function.
    ///
    /// This can be used to isolate parts of the reactive graph from one another.
    ///
    /// ```
    /// # use leptos_reactive::*;
    /// # run_scope(|cx| {
    /// let (a, set_a) = create_signal(cx, 0);
    /// let (b, set_b) = create_signal(cx, 0);
    /// let c = create_memo(cx, move |_| {
    ///     // this memo will *only* update when `a` changes
    ///     a() + cx.untrack(move || b())
    /// });
    ///
    /// assert_eq!(c(), 0);
    /// set_a(1);
    /// assert_eq!(c(), 1);
    /// set_b(1);
    /// // hasn't updated, because we untracked before reading b
    /// assert_eq!(c(), 1);
    /// set_a(2);
    /// assert_eq!(c(), 3);
    ///
    /// # });
    /// ```
    pub fn untrack<T>(&self, f: impl FnOnce() -> T) -> T {
        let prev_observer = self.runtime.observer.take();
        let untracked_result = f();
        self.runtime.observer.set(prev_observer);
        untracked_result
    }
}

// Internals

impl Scope {
    pub(crate) fn dispose(self) {
        // dispose of all child scopes
        let children = {
            let mut children = self.runtime.scope_children.borrow_mut();
            children.remove(self.id)
        };

        if let Some(children) = children {
            for id in children {
                Scope {
                    runtime: self.runtime,
                    id,
                }
                .dispose();
            }
        }

        // run cleanups
        if let Some(cleanups) = self.runtime.scope_cleanups.borrow_mut().remove(self.id) {
            for cleanup in cleanups {
                cleanup();
            }
        }

        // remove everything we own and run cleanups
        let owned = {
            let owned = self.runtime.scopes.borrow_mut().remove(self.id);
            owned.map(|owned| owned.take())
        };
        if let Some(owned) = owned {
            for property in owned {
                match property {
                    ScopeProperty::Signal(id) => {
                        // remove the signal
                        self.runtime.signals.borrow_mut().remove(id);
                        let subs = self.runtime.signal_subscribers.borrow_mut().remove(id);

                        // each of the subs needs to remove the signal from its dependencies
                        // so that it doesn't try to read the (now disposed) signal
                        if let Some(subs) = subs {
                            let source_map = self.runtime.effect_sources.borrow();
                            for effect in subs.borrow().iter() {
                                if let Some(effect_sources) = source_map.get(*effect) {
                                    effect_sources.borrow_mut().remove(&id);
                                }
                            }
                        }
                    }
                    ScopeProperty::Effect(id) => {
                        self.runtime.effects.borrow_mut().remove(id);
                        self.runtime.effect_sources.borrow_mut().remove(id);
                    }
                    ScopeProperty::Resource(id) => {
                        self.runtime.resources.borrow_mut().remove(id);
                    }
                }
            }
        }
    }

    pub(crate) fn with_scope_property(&self, f: impl FnOnce(&mut Vec<ScopeProperty>)) {
        let scopes = self.runtime.scopes.borrow();
        let scope = scopes
            .get(self.id)
            .expect("tried to add property to a scope that has been disposed");
        f(&mut *scope.borrow_mut());
    }
}

/// Creates a cleanup function, which will be run when a [Scope] is disposed.
///
/// It runs after child scopes have been disposed, but before signals, effects, and resources
/// are invalidated.
pub fn on_cleanup(cx: Scope, cleanup_fn: impl FnOnce() + 'static) {
    let mut cleanups = cx.runtime.scope_cleanups.borrow_mut();
    let cleanups = cleanups
        .entry(cx.id)
        .expect("trying to clean up a Scope that has already been disposed")
        .or_insert_with(Default::default);
    cleanups.push(Box::new(cleanup_fn));
}

slotmap::new_key_type! {
    /// Unique ID assigned to a [Scope](crate::Scope).
    pub struct ScopeId;
}

#[derive(Debug)]
pub(crate) enum ScopeProperty {
    Signal(SignalId),
    Effect(EffectId),
    Resource(ResourceId),
}

/// Creating a [Scope](crate::Scope) gives you a disposer, which can be called
/// to dispose of that reactive scope.
///
/// This will
/// 1. dispose of all child `Scope`s
/// 2. run all cleanup functions defined for this scope by [on_cleanup](crate::on_cleanup).
/// 3. dispose of all signals, effects, and resources owned by this `Scope`.
pub struct ScopeDisposer(pub(crate) Box<dyn FnOnce()>);

impl ScopeDisposer {
    /// Disposes of a reactive [Scope](crate::Scope).
    ///
    /// This will
    /// 1. dispose of all child `Scope`s
    /// 2. run all cleanup functions defined for this scope by [on_cleanup](crate::on_cleanup).
    /// 3. dispose of all signals, effects, and resources owned by this `Scope`.
    pub fn dispose(self) {
        (self.0)()
    }
}

impl Scope {
    // hydration-specific code
    cfg_if! {
        if #[cfg(any(feature = "hydrate", doc))] {
            /// `hydrate` only: Whether we're currently hydrating the page.
            pub fn is_hydrating(&self) -> bool {
                self.runtime.shared_context.borrow().is_some()
            }

            /// `hydrate` only: Begins the hydration process.
            pub fn start_hydration(&self, element: &web_sys::Element) {
                self.runtime.start_hydration(element);
            }

            /// `hydrate` only: Ends the hydration process.
            pub fn end_hydration(&self) {
                self.runtime.end_hydration();
            }

            /// `hydrate` only: Gets the next element in the hydration queue, either from the
            /// server-rendered DOM or from the template.
            pub fn get_next_element(&self, template: &web_sys::Element) -> web_sys::Element {
                use wasm_bindgen::{JsCast, UnwrapThrowExt};

                let cloned_template = |t: &web_sys::Element| {
                    let t = t
                        .unchecked_ref::<web_sys::HtmlTemplateElement>()
                        .content()
                        .clone_node_with_deep(true)
                        .expect_throw("(get_next_element) could not clone template")
                        .unchecked_into::<web_sys::Element>()
                        .first_element_child()
                        .expect_throw("(get_next_element) could not get first child of template");
                    t
                };

                if let Some(ref mut shared_context) = &mut *self.runtime.shared_context.borrow_mut() {
                    if shared_context.context.is_some() {
                        let key = shared_context.next_hydration_key();
                        let node = shared_context.registry.remove(&key);

                        //log::debug!("(hy) searching for {key}");

                        if let Some(node) = node {
                            //log::debug!("(hy) found {key}");
                            shared_context.completed.push(node.clone());
                            node
                        } else {
                            //log::debug!("(hy) did NOT find {key}");
                            cloned_template(template)
                        }
                    } else {
                        cloned_template(template)
                    }
                } else {
                    cloned_template(template)
                }
            }
        }
    }

    /// `hydrate` only: Given the current node, gets the span of the next component that has
    /// been marked for hydration, returning its starting node and the set of all its nodes.
    #[cfg(any(feature = "csr", feature = "hydrate", doc))]
    pub fn get_next_marker(&self, start: &web_sys::Node) -> (web_sys::Node, Vec<web_sys::Node>) {
        let mut end = Some(start.clone());
        let mut count = 0;
        let mut current = Vec::new();
        let mut start = start.clone();

        if self
            .runtime
            .shared_context
            .borrow()
            .as_ref()
            .map(|sc| sc.context.as_ref())
            .is_some()
        {
            while let Some(curr) = end {
                start = curr.clone();
                if curr.node_type() == 8 {
                    // COMMENT
                    let v = curr.node_value();
                    if v == Some("#".to_string()) {
                        count += 1;
                    } else if v == Some("/".to_string()) {
                        count -= 1;
                        if count == 0 {
                            current.push(curr.clone());
                            return (curr, current);
                        }
                    }
                }
                current.push(curr.clone());
                end = curr.next_sibling();
            }
        }

        (start, current)
    }

    /// On either the server side or the browser side, generates the next key in the hydration process.
    pub fn next_hydration_key(&self) -> String {
        let mut sc = self.runtime.shared_context.borrow_mut();
        if let Some(ref mut sc) = *sc {
            sc.next_hydration_key()
        } else {
            let mut new_sc = SharedContext::default();
            let id = new_sc.next_hydration_key();
            *sc = Some(new_sc);
            id
        }
    }

    /// Runs the given function with the next hydration context.
    pub fn with_next_context<T>(&self, f: impl FnOnce() -> T) -> T {
        if self
            .runtime
            .shared_context
            .borrow()
            .as_ref()
            .and_then(|sc| sc.context.as_ref())
            .is_some()
        {
            let c = {
                if let Some(ref mut sc) = *self.runtime.shared_context.borrow_mut() {
                    if let Some(ref mut context) = sc.context {
                        let next = context.next_hydration_context();
                        Some(std::mem::replace(context, next))
                    } else {
                        None
                    }
                } else {
                    None
                }
            };

            let res = self.untrack(f);

            if let Some(ref mut sc) = *self.runtime.shared_context.borrow_mut() {
                sc.context = c;
            }
            res
        } else {
            self.untrack(f)
        }
    }

    /// Returns IDs for all [Resource](crate::Resource)s found on any scope.
    pub fn all_resources(&self) -> Vec<ResourceId> {
        self.runtime.all_resources()
    }

    /// The current key for an HTML fragment created by server-rendering a `<Suspense/>` component.
    pub fn current_fragment_key(&self) -> String {
        self.runtime
            .shared_context
            .borrow()
            .as_ref()
            .map(|context| context.current_fragment_key())
            .unwrap_or_else(|| String::from("0f"))
    }

    /// Returns IDs for all [Resource](crate::Resource)s found on any scope.
    pub fn serialization_resolvers(&self) -> FuturesUnordered<PinnedFuture<(ResourceId, String)>> {
        self.runtime.serialization_resolvers()
    }

    /// Registers the given [SuspenseContext](crate::SuspenseContext) with the current scope,
    /// calling the `resolver` when its resources are all resolved.
    pub fn register_suspense(
        &self,
        context: SuspenseContext,
        key: &str,
        resolver: impl FnOnce() -> String + 'static,
    ) {
        use crate::create_isomorphic_effect;
        use futures::StreamExt;

        if let Some(ref mut shared_context) = *self.runtime.shared_context.borrow_mut() {
            let (mut tx, mut rx) = futures::channel::mpsc::channel::<()>(1);

            create_isomorphic_effect(*self, move |_| {
                let pending = context.pending_resources.try_with(|n| *n).unwrap_or(0);
                if pending == 0 {
                    _ = tx.try_send(());
                }
            });

            shared_context.pending_fragments.insert(
                key.to_string(),
                Box::pin(async move {
                    rx.next().await;
                    resolver()
                }),
            );
        }
    }

    /// The set of all HTML fragments current pending, by their keys (see [Self::current_fragment_key]).
    pub fn pending_fragments(&self) -> HashMap<String, Pin<Box<dyn Future<Output = String>>>> {
        if let Some(ref mut shared_context) = *self.runtime.shared_context.borrow_mut() {
            std::mem::take(&mut shared_context.pending_fragments)
        } else {
            HashMap::new()
        }
    }
}

impl Debug for ScopeDisposer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("ScopeDisposer").finish()
    }
}