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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
use crate::{default_impl, fmt_impls, write_impls};
use crate::{
read::Readable, write::Writable, CopyValue, GlobalMemo, GlobalSignal, ReactiveContext,
ReadableRef,
};
use crate::{Memo, WritableRef};
use dioxus_core::IntoDynNode;
use dioxus_core::{prelude::IntoAttributeValue, ScopeId};
use generational_box::{AnyStorage, Storage, SyncStorage, UnsyncStorage};
use std::{
any::Any,
collections::HashSet,
ops::{Deref, DerefMut},
sync::Mutex,
};
/// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
///
/// ```rust
/// use dioxus::prelude::*;
/// use dioxus_signals::*;
///
/// #[component]
/// fn App() -> Element {
/// let mut count = use_signal(|| 0);
///
/// // Because signals have automatic dependency tracking, if you never read them in a component, that component will not be re-rended when the signal is updated.
/// // The app component will never be rerendered in this example.
/// rsx! { Child { state: count } }
/// }
///
/// #[component]
/// fn Child(mut state: Signal<u32>) -> Element {
/// use_future(move || async move {
/// // Because the signal is a Copy type, we can use it in an async block without cloning it.
/// state += 1;
/// });
///
/// rsx! {
/// button {
/// onclick: move |_| state += 1,
/// "{state}"
/// }
/// }
/// }
/// ```
pub struct Signal<T: 'static, S: Storage<SignalData<T>> = UnsyncStorage> {
pub(crate) inner: CopyValue<SignalData<T>, S>,
}
/// A signal that can safely shared between threads.
pub type SyncSignal<T> = Signal<T, SyncStorage>;
/// The data stored for tracking in a signal.
pub struct SignalData<T> {
pub(crate) subscribers: Mutex<HashSet<ReactiveContext>>,
pub(crate) value: T,
}
impl<T: 'static> Signal<T> {
/// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
#[track_caller]
pub fn new(value: T) -> Self {
Self::new_maybe_sync(value)
}
/// Create a new signal with a custom owner scope. The signal will be dropped when the owner scope is dropped instead of the current scope.
#[track_caller]
pub fn new_in_scope(value: T, owner: ScopeId) -> Self {
Self::new_maybe_sync_in_scope(value, owner)
}
/// Creates a new global Signal that can be used in a global static.
#[track_caller]
pub const fn global(constructor: fn() -> T) -> GlobalSignal<T> {
GlobalSignal::new(constructor)
}
}
impl<T: PartialEq + 'static> Signal<T> {
/// Creates a new global Signal that can be used in a global static.
#[track_caller]
pub const fn global_memo(constructor: fn() -> T) -> GlobalMemo<T> {
GlobalMemo::new(constructor)
}
/// Creates a new unsync Selector. The selector will be run immediately and whenever any signal it reads changes.
///
/// Selectors can be used to efficiently compute derived data from signals.
#[track_caller]
pub fn memo(f: impl FnMut() -> T + 'static) -> Memo<T> {
Memo::new(f)
}
/// Creates a new unsync Selector with an explicit location. The selector will be run immediately and whenever any signal it reads changes.
///
/// Selectors can be used to efficiently compute derived data from signals.
pub fn memo_with_location(
f: impl FnMut() -> T + 'static,
location: &'static std::panic::Location<'static>,
) -> Memo<T> {
Memo::new_with_location(f, location)
}
}
impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
/// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
#[track_caller]
#[tracing::instrument(skip(value))]
pub fn new_maybe_sync(value: T) -> Self {
Self {
inner: CopyValue::<SignalData<T>, S>::new_maybe_sync(SignalData {
subscribers: Default::default(),
value,
}),
}
}
/// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
pub fn new_with_caller(
value: T,
#[cfg(debug_assertions)] caller: &'static std::panic::Location<'static>,
) -> Self {
Self {
inner: CopyValue::new_with_caller(
SignalData {
subscribers: Default::default(),
value,
},
#[cfg(debug_assertions)]
caller,
),
}
}
/// Create a new signal with a custom owner scope. The signal will be dropped when the owner scope is dropped instead of the current scope.
#[track_caller]
#[tracing::instrument(skip(value))]
pub fn new_maybe_sync_in_scope(value: T, owner: ScopeId) -> Self {
Self {
inner: CopyValue::<SignalData<T>, S>::new_maybe_sync_in_scope(
SignalData {
subscribers: Default::default(),
value,
},
owner,
),
}
}
/// Drop the value out of the signal, invalidating the signal in the process.
pub fn manually_drop(&self) -> Option<T> {
self.inner.manually_drop().map(|i| i.value)
}
/// Get the scope the signal was created in.
pub fn origin_scope(&self) -> ScopeId {
self.inner.origin_scope()
}
fn update_subscribers(&self) {
{
let inner = self.inner.read();
// We cannot hold the subscribers lock while calling mark_dirty, because mark_dirty can run user code which may cause a new subscriber to be added. If we hold the lock, we will deadlock.
let mut subscribers = std::mem::take(&mut *inner.subscribers.lock().unwrap());
subscribers.retain(|reactive_context| reactive_context.mark_dirty());
// Extend the subscribers list instead of overwriting it in case a subscriber is added while reactive contexts are marked dirty
inner.subscribers.lock().unwrap().extend(subscribers);
}
}
/// Get the generational id of the signal.
pub fn id(&self) -> generational_box::GenerationalBoxId {
self.inner.id()
}
/// **This pattern is no longer recommended. Prefer [`peek`](Signal::peek) or creating new signals instead.**
///
/// This function is the equivalent of the [write_silent](https://docs.rs/dioxus/latest/dioxus/prelude/struct.UseRef.html#method.write_silent) method on use_ref.
///
/// ## What you should use instead
///
/// ### Reading and Writing to data in the same scope
///
/// Reading and writing to the same signal in the same scope will cause that scope to rerun forever:
/// ```rust, no_run
/// # use dioxus::prelude::*;
/// let mut signal = use_signal(|| 0);
/// // This makes the scope rerun whenever we write to the signal
/// println!("{}", *signal.read());
/// // This will rerun the scope because we read the signal earlier in the same scope
/// *signal.write() += 1;
/// ```
///
/// You may have used the write_silent method to avoid this infinite loop with use_ref like this:
/// ```rust, no_run
/// # use dioxus::prelude::*;
/// let signal = use_signal(|| 0);
/// // This makes the scope rerun whenever we write to the signal
/// println!("{}", *signal.read());
/// // Write silent will not rerun any subscribers
/// *signal.write_silent() += 1;
/// ```
///
/// Instead you can use the [`peek`](Signal::peek) and [`write`](Signal::write) methods instead. The peek method will not subscribe to the current scope which will avoid an infinite loop if you are reading and writing to the same signal in the same scope.
/// ```rust, no_run
/// # use dioxus::prelude::*;
/// let mut signal = use_signal(|| 0);
/// // Peek will read the value but not subscribe to the current scope
/// println!("{}", *signal.peek());
/// // Write will update any subscribers which does not include the current scope
/// *signal.write() += 1;
/// ```
///
/// ### Reading and Writing to different data
///
///
///
/// ## Why is this pattern no longer recommended?
///
/// This pattern is no longer recommended because it is very easy to allow your state and UI to grow out of sync. `write_silent` globally opts out of automatic state updates which can be difficult to reason about.
///
///
/// Lets take a look at an example:
/// main.rs:
/// ```rust, no_run
/// # use dioxus::prelude::*;
/// # fn Child() -> Element { todo!() }
/// fn app() -> Element {
/// let signal = use_context_provider(|| Signal::new(0));
///
/// // We want to log the value of the signal whenever the app component reruns
/// println!("{}", *signal.read());
///
/// rsx! {
/// button {
/// // If we don't want to rerun the app component when the button is clicked, we can use write_silent
/// onclick: move |_| *signal.write_silent() += 1,
/// "Increment"
/// }
/// Child {}
/// }
/// }
/// ```
/// child.rs:
/// ```rust, no_run
/// # use dioxus::prelude::*;
/// fn Child() -> Element {
/// let signal: Signal<i32> = use_context();
///
/// // It is difficult to tell that changing the button to use write_silent in the main.rs file will cause UI to be out of sync in a completely different file
/// rsx! {
/// "{signal}"
/// }
/// }
/// ```
///
/// Instead [`peek`](Signal::peek) locally opts out of automatic state updates explicitly for a specific read which is easier to reason about.
///
/// Here is the same example using peek:
/// main.rs:
/// ```rust, no_run
/// # use dioxus::prelude::*;
/// # fn Child() -> Element { todo!() }
/// fn app() -> Element {
/// let mut signal = use_context_provider(|| Signal::new(0));
///
/// // We want to log the value of the signal whenever the app component reruns, but we don't want to rerun the app component when the signal is updated so we use peek instead of read
/// println!("{}", *signal.peek());
///
/// rsx! {
/// button {
/// // We can use write like normal and update the child component automatically
/// onclick: move |_| *signal.write() += 1,
/// "Increment"
/// }
/// Child {}
/// }
/// }
/// ```
/// child.rs:
/// ```rust, no_run
/// # use dioxus::prelude::*;
/// fn Child() -> Element {
/// let signal: Signal<i32> = use_context();
///
/// rsx! {
/// "{signal}"
/// }
/// }
/// ```
#[track_caller]
#[deprecated = "This pattern is no longer recommended. Prefer `peek` or creating new signals instead."]
pub fn write_silent(&self) -> S::Mut<'static, T> {
S::map_mut(self.inner.write_unchecked(), |inner| &mut inner.value)
}
}
impl<T, S: Storage<SignalData<T>>> Readable for Signal<T, S> {
type Target = T;
type Storage = S;
#[track_caller]
fn try_read_unchecked(
&self,
) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError> {
let inner = self.inner.try_read_unchecked()?;
if let Some(reactive_context) = ReactiveContext::current() {
tracing::trace!("Subscribing to the reactive context {}", reactive_context);
inner.subscribers.lock().unwrap().insert(reactive_context);
}
Ok(S::map(inner, |v| &v.value))
}
/// Get the current value of the signal. **Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.**
///
/// If the signal has been dropped, this will panic.
#[track_caller]
fn peek_unchecked(&self) -> ReadableRef<'static, Self> {
let inner = self.inner.try_read_unchecked().unwrap();
S::map(inner, |v| &v.value)
}
}
impl<T: 'static, S: Storage<SignalData<T>>> Writable for Signal<T, S> {
type Mut<'a, R: ?Sized + 'static> = Write<'a, R, S>;
fn map_mut<I: ?Sized, U: ?Sized + 'static, F: FnOnce(&mut I) -> &mut U>(
ref_: Self::Mut<'_, I>,
f: F,
) -> Self::Mut<'_, U> {
Write::map(ref_, f)
}
fn try_map_mut<
I: ?Sized + 'static,
U: ?Sized + 'static,
F: FnOnce(&mut I) -> Option<&mut U>,
>(
ref_: Self::Mut<'_, I>,
f: F,
) -> Option<Self::Mut<'_, U>> {
Write::filter_map(ref_, f)
}
fn downcast_lifetime_mut<'a: 'b, 'b, R: ?Sized + 'static>(
mut_: Self::Mut<'a, R>,
) -> Self::Mut<'b, R> {
Write::downcast_lifetime(mut_)
}
#[track_caller]
fn try_write_unchecked(
&self,
) -> Result<WritableRef<'static, Self>, generational_box::BorrowMutError> {
#[cfg(debug_assertions)]
let origin = std::panic::Location::caller();
self.inner.try_write_unchecked().map(|inner| {
let borrow = S::map_mut(inner, |v| &mut v.value);
Write {
write: borrow,
drop_signal: Box::new(SignalSubscriberDrop {
signal: *self,
#[cfg(debug_assertions)]
origin,
}),
}
})
}
}
impl<T> IntoAttributeValue for Signal<T>
where
T: Clone + IntoAttributeValue,
{
fn into_value(self) -> dioxus_core::AttributeValue {
self.with(|f| f.clone().into_value())
}
}
impl<T> IntoDynNode for Signal<T>
where
T: Clone + IntoDynNode,
{
fn into_dyn_node(self) -> dioxus_core::DynamicNode {
self().into_dyn_node()
}
}
impl<T: 'static, S: Storage<SignalData<T>>> PartialEq for Signal<T, S> {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}
impl<T: 'static, S: Storage<SignalData<T>>> Eq for Signal<T, S> {}
/// Allow calling a signal with signal() syntax
///
/// Currently only limited to copy types, though could probably specialize for string/arc/rc
impl<T: Clone, S: Storage<SignalData<T>> + 'static> Deref for Signal<T, S> {
type Target = dyn Fn() -> T;
fn deref(&self) -> &Self::Target {
Readable::deref_impl(self)
}
}
#[cfg(feature = "serde")]
impl<T: serde::Serialize + 'static, Store: Storage<SignalData<T>>> serde::Serialize
for Signal<T, Store>
{
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.read().serialize(serializer)
}
}
#[cfg(feature = "serde")]
impl<'de, T: serde::Deserialize<'de> + 'static, Store: Storage<SignalData<T>>>
serde::Deserialize<'de> for Signal<T, Store>
{
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Ok(Self::new_maybe_sync(T::deserialize(deserializer)?))
}
}
/// A mutable reference to a signal's value.
///
/// T is the current type of the write
/// S is the storage type of the signal
pub struct Write<'a, T: ?Sized + 'static, S: AnyStorage = UnsyncStorage> {
write: S::Mut<'a, T>,
drop_signal: Box<dyn Any>,
}
impl<'a, T: ?Sized + 'static, S: AnyStorage> Write<'a, T, S> {
/// Map the mutable reference to the signal's value to a new type.
pub fn map<O: ?Sized>(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write<'a, O, S> {
let Self {
write, drop_signal, ..
} = myself;
Write {
write: S::map_mut(write, f),
drop_signal,
}
}
/// Try to map the mutable reference to the signal's value to a new type
pub fn filter_map<O: ?Sized>(
myself: Self,
f: impl FnOnce(&mut T) -> Option<&mut O>,
) -> Option<Write<'a, O, S>> {
let Self {
write, drop_signal, ..
} = myself;
let write = S::try_map_mut(write, f);
write.map(|write| Write { write, drop_signal })
}
/// Downcast the lifetime of the mutable reference to the signal's value.
///
/// This function enforces the variance of the lifetime parameter `'a` in Mut. Rust will typically infer this cast with a concrete type, but it cannot with a generic type.
pub fn downcast_lifetime<'b>(mut_: Self) -> Write<'b, T, S>
where
'a: 'b,
{
Write {
write: S::downcast_lifetime_mut(mut_.write),
drop_signal: mut_.drop_signal,
}
}
}
impl<T: ?Sized + 'static, S: AnyStorage> Deref for Write<'_, T, S> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.write
}
}
impl<T: ?Sized, S: AnyStorage> DerefMut for Write<'_, T, S> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.write
}
}
#[allow(unused)]
const SIGNAL_READ_WRITE_SAME_SCOPE_HELP: &str = r#"This issue is caused by reading and writing to the same signal in a reactive scope. Components, effects, memos, and resources each have their own a reactive scopes. Reactive scopes rerun when any signal you read inside of them are changed. If you read and write to the same signal in the same scope, the write will cause the scope to rerun and trigger the write again. This can cause an infinite loop.
You can fix the issue by either:
1) Splitting up your state and Writing, reading to different signals:
For example, you could change this broken code:
#[derive(Clone, Copy)]
struct Counts {
count1: i32,
count2: i32,
}
fn app() -> Element {
let mut counts = use_signal(|| Counts { count1: 0, count2: 0 });
use_effect(move || {
// This effect both reads and writes to counts
counts.write().count1 = counts().count2;
})
}
Into this working code:
fn app() -> Element {
let mut count1 = use_signal(|| 0);
let mut count2 = use_signal(|| 0);
use_effect(move || {
count1.write(count2());
});
}
2) Reading and Writing to the same signal in different scopes:
For example, you could change this broken code:
fn app() -> Element {
let mut count = use_signal(|| 0);
use_effect(move || {
// This effect both reads and writes to count
println!("{}", count());
count.write(count());
});
}
To this working code:
fn app() -> Element {
let mut count = use_signal(|| 0);
use_effect(move || {
count.write(count());
});
use_effect(move || {
println!("{}", count());
});
}
"#;
struct SignalSubscriberDrop<T: 'static, S: Storage<SignalData<T>>> {
signal: Signal<T, S>,
#[cfg(debug_assertions)]
origin: &'static std::panic::Location<'static>,
}
impl<T: 'static, S: Storage<SignalData<T>>> Drop for SignalSubscriberDrop<T, S> {
fn drop(&mut self) {
#[cfg(debug_assertions)]
{
tracing::trace!(
"Write on signal at {} finished, updating subscribers",
self.origin
);
// Check if the write happened during a render. If it did, we should warn the user that this is generally a bad practice.
if dioxus_core::vdom_is_rendering() {
tracing::warn!(
"Write on signal at {} happened while a component was running. Writing to signals during a render can cause infinite rerenders when you read the same signal in the component. Consider writing to the signal in an effect, future, or event handler if possible.",
self.origin
);
}
// Check if the write happened during a scope that the signal is also subscribed to. If it did, this will probably cause an infinite loop.
if let Some(reactive_context) = ReactiveContext::current() {
if let Ok(inner) = self.signal.inner.try_read() {
if let Ok(subscribers) = inner.subscribers.lock() {
for subscriber in subscribers.iter() {
if reactive_context == *subscriber {
let origin = self.origin;
tracing::warn!(
"Write on signal at {origin} finished in {reactive_context} which is also subscribed to the signal. This will likely cause an infinite loop. When the write finishes, {reactive_context} will rerun which may cause the write to be rerun again.\nHINT:\n{SIGNAL_READ_WRITE_SAME_SCOPE_HELP}",
);
}
}
}
}
}
}
self.signal.update_subscribers();
}
}
fmt_impls!(Signal<T, S: Storage<SignalData<T>>>);
default_impl!(Signal<T, S: Storage<SignalData<T>>>);
write_impls!(Signal<T, S: Storage<SignalData<T>>>);
impl<T: 'static, S: Storage<SignalData<T>>> Clone for Signal<T, S> {
fn clone(&self) -> Self {
*self
}
}
impl<T: 'static, S: Storage<SignalData<T>>> Copy for Signal<T, S> {}