Explorar el Código

don't expose the signal scope subscriber API publicly

Evan Almloff hace 1 año
padre
commit
91fea15cdc

+ 1 - 1
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

+ 2 - 2
packages/hooks/src/use_memo.rs

@@ -49,7 +49,7 @@ pub fn use_maybe_sync_memo<R: PartialEq, S: Storage<SignalData<R>>>(
 ) -> ReadOnlySignal<R, S> {
     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<R, S> = 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<R, S> =

+ 1 - 1
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

+ 14 - 3
packages/signals/src/reactive_context.rs

@@ -22,9 +22,20 @@ thread_local! {
     static CURRENT: RefCell<Vec<ReactiveContext>> = 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<ScopeId>) -> 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<ScopeId>) -> 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