dioxus_core/
reactive_context.rs

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
use crate::{
    prelude::{current_scope_id, ScopeId},
    scope_context::Scope,
    tasks::SchedulerMsg,
    Runtime,
};
use futures_channel::mpsc::UnboundedReceiver;
use generational_box::{BorrowMutError, GenerationalBox, SyncStorage};
use std::{
    cell::RefCell,
    collections::HashSet,
    hash::Hash,
    sync::{Arc, Mutex},
};

#[doc = include_str!("../docs/reactivity.md")]
#[derive(Clone, Copy)]
pub struct ReactiveContext {
    scope: ScopeId,
    inner: GenerationalBox<Inner, SyncStorage>,
}

impl PartialEq for ReactiveContext {
    fn eq(&self, other: &Self) -> bool {
        self.inner.ptr_eq(&other.inner)
    }
}

impl Eq for ReactiveContext {}

thread_local! {
    static CURRENT: RefCell<Vec<ReactiveContext>> = const { RefCell::new(vec![]) };
}

impl std::fmt::Display for ReactiveContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        #[cfg(debug_assertions)]
        {
            if let Ok(read) = self.inner.try_read() {
                if let Some(scope) = read.scope {
                    return write!(f, "ReactiveContext(for scope: {:?})", scope);
                }
                return write!(f, "ReactiveContext created at {}", read.origin);
            }
        }
        write!(f, "ReactiveContext")
    }
}

impl ReactiveContext {
    /// Create a new reactive context
    #[track_caller]
    pub fn new() -> (Self, UnboundedReceiver<()>) {
        Self::new_with_origin(std::panic::Location::caller())
    }

    /// Create a new reactive context with a location for debugging purposes
    /// This is useful for reactive contexts created within closures
    pub fn new_with_origin(
        origin: &'static std::panic::Location<'static>,
    ) -> (Self, UnboundedReceiver<()>) {
        let (tx, rx) = futures_channel::mpsc::unbounded();
        let callback = move || {
            // If there is already an update queued, we don't need to queue another
            if !tx.is_empty() {
                return;
            }
            let _ = tx.unbounded_send(());
        };
        let _self = Self::new_with_callback(
            callback,
            current_scope_id().unwrap_or_else(|e| panic!("{}", e)),
            origin,
        );
        (_self, rx)
    }

    /// Create a new reactive context that may update a scope. When any signal that this context subscribes to changes, the callback will be run
    pub fn new_with_callback(
        callback: impl FnMut() + Send + Sync + 'static,
        scope: ScopeId,
        #[allow(unused)] origin: &'static std::panic::Location<'static>,
    ) -> Self {
        let inner = Inner {
            self_: None,
            update: Box::new(callback),
            subscribers: Default::default(),
            #[cfg(debug_assertions)]
            origin,
            #[cfg(debug_assertions)]
            scope: None,
        };

        let owner = scope.owner();

        let self_ = Self {
            scope,
            inner: owner.insert(inner),
        };

        self_.inner.write().self_ = Some(self_);

        self_
    }

    /// Get the current reactive context from the nearest reactive hook or scope
    pub fn current() -> Option<Self> {
        CURRENT.with(|current| current.borrow().last().cloned())
    }

    /// Create a reactive context for a scope id
    pub(crate) fn new_for_scope(scope: &Scope, runtime: &Runtime) -> Self {
        let id = scope.id;
        let sender = runtime.sender.clone();
        let update_scope = move || {
            tracing::trace!("Marking scope {:?} as dirty", id);
            sender.unbounded_send(SchedulerMsg::Immediate(id)).unwrap();
        };

        // Otherwise, create a new context at the current scope
        let inner = Inner {
            self_: None,
            update: Box::new(update_scope),
            subscribers: Default::default(),
            #[cfg(debug_assertions)]
            origin: std::panic::Location::caller(),
            #[cfg(debug_assertions)]
            scope: Some(id),
        };

        let owner = scope.owner();

        let self_ = Self {
            scope: id,
            inner: owner.insert(inner),
        };

        self_.inner.write().self_ = Some(self_);

        self_
    }

    /// Clear all subscribers to this context
    pub fn clear_subscribers(&self) {
        // The key type is mutable, but the hash is stable through mutations because we hash by pointer
        #[allow(clippy::mutable_key_type)]
        let old_subscribers = std::mem::take(&mut self.inner.write().subscribers);
        for subscriber in old_subscribers {
            subscriber.0.lock().unwrap().remove(self);
        }
    }

    /// Update the subscribers
    pub(crate) fn update_subscribers(&self) {
        #[allow(clippy::mutable_key_type)]
        let subscribers = &self.inner.read().subscribers;
        for subscriber in subscribers.iter() {
            subscriber.0.lock().unwrap().insert(*self);
        }
    }

    /// Reset the reactive context and then run the callback in the context. This can be used to create custom reactive hooks like `use_memo`.
    ///
    /// ```rust, no_run
    /// # use dioxus::prelude::*;
    /// # use futures_util::StreamExt;
    /// fn use_simplified_memo(mut closure: impl FnMut() -> i32 + 'static) -> Signal<i32> {
    ///     use_hook(|| {
    ///         // Create a new reactive context and channel that will receive a value every time a value the reactive context subscribes to changes
    ///         let (reactive_context, mut changed) = ReactiveContext::new();
    ///         // Compute the value of the memo inside the reactive context. This will subscribe the reactive context to any values you read inside the closure
    ///         let value = reactive_context.reset_and_run_in(&mut closure);
    ///         // Create a new signal with the value of the memo
    ///         let mut signal = Signal::new(value);
    ///         // Create a task that reruns the closure when the reactive context changes
    ///         spawn(async move {
    ///             while changed.next().await.is_some() {
    ///                 // Since we reset the reactive context as we run the closure, our memo will only subscribe to the new values that are read in the closure
    ///                 let new_value = reactive_context.run_in(&mut closure);
    ///                 if new_value != value {
    ///                     signal.set(new_value);
    ///                 }
    ///             }
    ///         });
    ///         signal
    ///     })
    /// }
    ///
    /// let mut boolean = use_signal(|| false);
    /// let mut count = use_signal(|| 0);
    /// // Because we use `reset_and_run_in` instead of just `run_in`, our memo will only subscribe to the signals that are read this run of the closure (initially just the boolean)
    /// let memo = use_simplified_memo(move || if boolean() { count() } else { 0 });
    /// println!("{memo}");
    /// // Because the count signal is not read in this run of the closure, the memo will not rerun
    /// count += 1;
    /// println!("{memo}");
    /// // Because the boolean signal is read in this run of the closure, the memo will rerun
    /// boolean.toggle();
    /// println!("{memo}");
    /// // If we toggle the boolean again, and the memo unsubscribes from the count signal
    /// boolean.toggle();
    /// println!("{memo}");
    /// ```
    pub fn reset_and_run_in<O>(&self, f: impl FnOnce() -> O) -> O {
        self.clear_subscribers();
        self.run_in(f)
    }

    /// Run this function in the context of this reactive context
    ///
    /// This will set the current reactive context to this context for the duration of the function.
    /// You can then get information about the current subscriptions.
    pub fn run_in<O>(&self, f: impl FnOnce() -> O) -> O {
        CURRENT.with(|current| current.borrow_mut().push(*self));
        let out = f();
        CURRENT.with(|current| current.borrow_mut().pop());
        self.update_subscribers();
        out
    }

    /// Marks this reactive context as dirty
    ///
    /// If there's a scope associated with this context, then it will be marked as dirty too
    ///
    /// Returns true if the context was marked as dirty, or false if the context has been dropped
    pub fn mark_dirty(&self) -> bool {
        if let Ok(mut self_write) = self.inner.try_write() {
            #[cfg(debug_assertions)]
            {
                tracing::trace!(
                    "Marking reactive context created at {} as dirty",
                    self_write.origin
                );
            }

            (self_write.update)();

            true
        } else {
            false
        }
    }

    /// Subscribe to this context. The reactive context will automatically remove itself from the subscriptions when it is reset.
    pub fn subscribe(&self, subscriptions: Arc<Mutex<HashSet<ReactiveContext>>>) {
        match self.inner.try_write() {
            Ok(mut inner) => {
                subscriptions.lock().unwrap().insert(*self);
                inner.subscribers.insert(PointerHash(subscriptions));
            }
            // If the context was dropped, we don't need to subscribe to it anymore
            Err(BorrowMutError::Dropped(_)) => {}
            Err(expect) => {
                panic!(
                    "Expected to be able to write to reactive context to subscribe, but it failed with: {expect:?}"
                );
            }
        }
    }

    /// Get the scope that inner CopyValue is associated with
    pub fn origin_scope(&self) -> ScopeId {
        self.scope
    }
}

impl Hash for ReactiveContext {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.inner.id().hash(state);
    }
}

struct PointerHash<T>(Arc<T>);

impl<T> Hash for PointerHash<T> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        std::sync::Arc::<T>::as_ptr(&self.0).hash(state);
    }
}

impl<T> PartialEq for PointerHash<T> {
    fn eq(&self, other: &Self) -> bool {
        std::sync::Arc::<T>::as_ptr(&self.0) == std::sync::Arc::<T>::as_ptr(&other.0)
    }
}

impl<T> Eq for PointerHash<T> {}

impl<T> Clone for PointerHash<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

type SubscriberMap = Mutex<HashSet<ReactiveContext>>;

struct Inner {
    self_: Option<ReactiveContext>,

    // Futures will call .changed().await
    update: Box<dyn FnMut() + Send + Sync>,

    // Subscribers to this context
    subscribers: HashSet<PointerHash<SubscriberMap>>,

    // Debug information for signal subscriptions
    #[cfg(debug_assertions)]
    origin: &'static std::panic::Location<'static>,

    #[cfg(debug_assertions)]
    // The scope that this reactive context is associated with
    scope: Option<ScopeId>,
}

impl Drop for Inner {
    fn drop(&mut self) {
        let Some(self_) = self.self_.take() else {
            return;
        };

        for subscriber in std::mem::take(&mut self.subscribers) {
            if let Ok(mut subscriber) = subscriber.0.lock() {
                subscriber.remove(&self_);
            }
        }
    }
}