From 91fea15cdcb3b1795aaf4abc55853b15cfdbc409 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Mon, 5 Feb 2024 08:13:52 -0600 Subject: [PATCH] don't expose the signal scope subscriber API publicly --- packages/hooks/src/use_effect.rs | 2 +- packages/hooks/src/use_memo.rs | 4 ++-- packages/hooks/src/use_resource.rs | 2 +- packages/signals/src/reactive_context.rs | 17 ++++++++++++++--- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/hooks/src/use_effect.rs b/packages/hooks/src/use_effect.rs index 0e2ce36c0..a3d16f420 100644 --- a/packages/hooks/src/use_effect.rs +++ b/packages/hooks/src/use_effect.rs @@ -11,7 +11,7 @@ pub fn use_effect(mut callback: impl FnMut() + 'static) { use_hook(|| { spawn(async move { - let rc = ReactiveContext::new(None); + let rc = ReactiveContext::new(); loop { // Wait for the dom the be finished with sync work diff --git a/packages/hooks/src/use_memo.rs b/packages/hooks/src/use_memo.rs index 0d6d2e8e2..7912259a0 100644 --- a/packages/hooks/src/use_memo.rs +++ b/packages/hooks/src/use_memo.rs @@ -49,7 +49,7 @@ pub fn use_maybe_sync_memo>>( ) -> ReadOnlySignal { use_hook(|| { // Get the current reactive context - let rc = ReactiveContext::new(None); + let rc = ReactiveContext::new(); // Create a new signal in that context, wiring up its dependencies and subscribers let mut state: Signal = rc.run_in(|| Signal::new_maybe_sync(f())); @@ -129,7 +129,7 @@ where let selector = use_hook(|| { // Get the current reactive context - let rc = ReactiveContext::new(None); + let rc = ReactiveContext::new(); // Create a new signal in that context, wiring up its dependencies and subscribers let mut state: Signal = diff --git a/packages/hooks/src/use_resource.rs b/packages/hooks/src/use_resource.rs index 2124a7289..4ac74fa44 100644 --- a/packages/hooks/src/use_resource.rs +++ b/packages/hooks/src/use_resource.rs @@ -20,7 +20,7 @@ where { let mut value = use_signal(|| None); let mut state = use_signal(|| UseResourceState::Pending); - let rc = use_hook(|| ReactiveContext::new(None)); + let rc = use_hook(ReactiveContext::new); let mut cb = use_callback(move || { // Create the user's task diff --git a/packages/signals/src/reactive_context.rs b/packages/signals/src/reactive_context.rs index e45a4f8cc..0e62f0dd9 100644 --- a/packages/signals/src/reactive_context.rs +++ b/packages/signals/src/reactive_context.rs @@ -22,9 +22,20 @@ thread_local! { static CURRENT: RefCell> = const { RefCell::new(vec![]) }; } +impl Default for ReactiveContext { + fn default() -> Self { + Self::new_for_scope(None) + } +} + impl ReactiveContext { /// Create a new reactive context - pub fn new(scope: Option) -> Self { + pub fn new() -> Self { + Self::default() + } + + /// Create a new reactive context that may update a scope + pub(crate) fn new_for_scope(scope: Option) -> Self { let (tx, rx) = flume::unbounded(); let mut scope_subscribers = FxHashSet::default(); @@ -56,7 +67,7 @@ impl ReactiveContext { /// /// If this was set manually, then that value will be returned. /// - /// If there's no current reactive context, then a new one will be created at the current scope and returned. + /// If there's no current reactive context, then a new one will be created for the current scope and returned. pub fn current() -> Self { let cur = CURRENT.with(|current| current.borrow().last().cloned()); @@ -71,7 +82,7 @@ impl ReactiveContext { } // Otherwise, create a new context at the current scope - provide_context(ReactiveContext::new(current_scope_id())) + provide_context(ReactiveContext::new_for_scope(current_scope_id())) } /// Run this function in the context of this reactive context