clean up prelude (#4416)
* clear away prelude on dioxus * clippy * wip - drop prelude from dioxus-html * docs clippy * wip * more fixe * delete all preludes * doc... * better export * merge some imports * fewer global imports * global incremental renderer * fix cargo doc
This commit is contained in:
1 parent
54d465a7b5
commit
4f1d039bad
140 files changed
+374
-477
No files matched your search
@@ -1,14 +1,10 @@
|
||||
#![allow(clippy::unnecessary_operation)]
|
||||
#![allow(clippy::no_effect)]
|
||||
|
||||
use generational_box::UnsyncStorage;
|
||||
use generational_box::{BorrowResult, GenerationalBoxId};
|
||||
use dioxus_core::{current_owner, current_scope_id, ScopeId};
|
||||
use generational_box::{BorrowResult, GenerationalBox, GenerationalBoxId, Storage, UnsyncStorage};
|
||||
use std::ops::Deref;
|
||||
|
||||
use dioxus_core::prelude::*;
|
||||
|
||||
use generational_box::{GenerationalBox, Storage};
|
||||
|
||||
use crate::read_impls;
|
||||
use crate::Readable;
|
||||
use crate::ReadableRef;
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use std::{ops::Deref, rc::Rc};
|
||||
|
||||
use crate::{read::Readable, read_impls, ReadableRef};
|
||||
use dioxus_core::prelude::*;
|
||||
use dioxus_core::IntoAttributeValue;
|
||||
use generational_box::{AnyStorage, BorrowResult, UnsyncStorage};
|
||||
use std::{ops::Deref, rc::Rc};
|
||||
|
||||
/// A read only signal that has been mapped to a new type.
|
||||
pub struct MappedSignal<O: ?Sized + 'static, S: AnyStorage = UnsyncStorage> {
|
||||
|
||||
@@ -8,7 +8,9 @@ use std::{
|
||||
sync::{atomic::AtomicBool, Arc},
|
||||
};
|
||||
|
||||
use dioxus_core::prelude::*;
|
||||
use dioxus_core::{
|
||||
current_scope_id, spawn_isomorphic, IntoAttributeValue, IntoDynNode, ReactiveContext, ScopeId,
|
||||
};
|
||||
use futures_util::StreamExt;
|
||||
use generational_box::{AnyStorage, BorrowResult, UnsyncStorage};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::{ReadOnlySignal, Signal};
|
||||
use dioxus_core::prelude::*;
|
||||
use dioxus_core::SuperFrom;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct SignalFromMarker<M>(std::marker::PhantomData<M>);
|
||||
@@ -16,6 +16,7 @@ where
|
||||
#[test]
|
||||
#[allow(unused)]
|
||||
fn into_signal_compiles() {
|
||||
use dioxus_core::SuperInto;
|
||||
fn takes_signal_string<M>(_: impl SuperInto<ReadOnlySignal<String>, M>) {}
|
||||
|
||||
fn takes_option_signal_string<M>(_: impl SuperInto<ReadOnlySignal<Option<String>>, M>) {}
|
||||
|
||||
@@ -3,7 +3,7 @@ use dioxus_core::IntoDynNode;
|
||||
use std::ops::Deref;
|
||||
|
||||
use crate::{default_impl, read_impls};
|
||||
use dioxus_core::{prelude::IntoAttributeValue, ScopeId};
|
||||
use dioxus_core::{IntoAttributeValue, ScopeId};
|
||||
use generational_box::{BorrowResult, Storage, UnsyncStorage};
|
||||
|
||||
/// A signal that can only be read from.
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::write::Writable;
|
||||
use std::hash::Hash;
|
||||
|
||||
use crate::read::Readable;
|
||||
use dioxus_core::prelude::*;
|
||||
use dioxus_core::ReactiveContext;
|
||||
use futures_util::StreamExt;
|
||||
use generational_box::{Storage, UnsyncStorage};
|
||||
|
||||
@@ -54,7 +54,7 @@ impl<R: Eq + Hash, S: Storage<SignalData<bool>>> SetCompare<R, S> {
|
||||
*previous = Some(current);
|
||||
};
|
||||
let (rc, mut changed) = ReactiveContext::new();
|
||||
spawn(async move {
|
||||
dioxus_core::spawn(async move {
|
||||
loop {
|
||||
// Recompute the value
|
||||
rc.reset_and_run_in(&mut recompute);
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
use crate::{default_impl, fmt_impls, write_impls, Global};
|
||||
use crate::{read::*, write::*, CopyValue, GlobalMemo, GlobalSignal, ReadableRef};
|
||||
use crate::{Memo, WritableRef};
|
||||
use dioxus_core::prelude::*;
|
||||
use crate::{
|
||||
default_impl, fmt_impls, read::*, write::*, write_impls, CopyValue, Global, GlobalMemo,
|
||||
GlobalSignal, Memo, ReadableRef, WritableRef,
|
||||
};
|
||||
use dioxus_core::{IntoAttributeValue, IntoDynNode, ReactiveContext, ScopeId};
|
||||
use generational_box::{AnyStorage, BorrowResult, Storage, SyncStorage, UnsyncStorage};
|
||||
use std::sync::Arc;
|
||||
use std::{
|
||||
any::Any,
|
||||
collections::HashSet,
|
||||
ops::{Deref, DerefMut},
|
||||
sync::Arc,
|
||||
sync::Mutex,
|
||||
};
|
||||
|
||||
@@ -522,7 +523,7 @@ impl<'de, T: serde::Deserialize<'de> + 'static, Store: Storage<SignalData<T>>>
|
||||
/// # use dioxus::prelude::*;
|
||||
/// fn app() -> Element {
|
||||
/// let mut value = use_signal(|| String::from("hello"));
|
||||
///
|
||||
///
|
||||
/// rsx! {
|
||||
/// button {
|
||||
/// onclick: move |_| {
|
||||
|
||||
@@ -9,7 +9,7 @@ pub fn copy_value_hoisted<T: 'static, S: generational_box::Storage<T>>(
|
||||
caller: &'static std::panic::Location<'static>,
|
||||
) {
|
||||
let origin_scope = value.origin_scope;
|
||||
if let Ok(current_scope) = dioxus_core::prelude::current_scope_id() {
|
||||
if let Ok(current_scope) = dioxus_core::current_scope_id() {
|
||||
// If the current scope is a descendant of the origin scope or is the same scope, we don't need to warn
|
||||
if origin_scope == current_scope || current_scope.is_descendant_of(origin_scope) {
|
||||
return;
|
||||
@@ -53,102 +53,3 @@ fn hoist() {
|
||||
include!("../docs/hoist/fixed_list.rs");
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if the write happened during a render. If it did, warn the user that this is generally a bad practice.
|
||||
#[warning]
|
||||
pub fn signal_write_in_component_body(origin: &'static std::panic::Location<'static>) {
|
||||
// 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.",
|
||||
origin
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if the write happened during a scope that the signal is also subscribed to. If it did, trigger a warning because it will likely cause an infinite loop.
|
||||
#[warning]
|
||||
pub fn signal_read_and_write_in_reactive_scope<
|
||||
T: 'static,
|
||||
S: generational_box::Storage<crate::SignalData<T>>,
|
||||
>(
|
||||
origin: &'static std::panic::Location<'static>,
|
||||
signal: crate::Signal<T, S>,
|
||||
) {
|
||||
// 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) = dioxus_core::prelude::ReactiveContext::current() {
|
||||
if let Ok(inner) = crate::Readable::try_read(&signal.inner) {
|
||||
if let Ok(subscribers) = inner.subscribers.lock() {
|
||||
for subscriber in subscribers.iter() {
|
||||
if reactive_context == *subscriber {
|
||||
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}",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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.set(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.set(1);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
To this working code:
|
||||
|
||||
fn app() -> Element {
|
||||
let mut count = use_signal(|| 0);
|
||||
|
||||
use_effect(move || {
|
||||
count.set(1);
|
||||
});
|
||||
use_effect(move || {
|
||||
println!("{}", count());
|
||||
});
|
||||
}
|
||||
"#;
|
||||
Reference in new issue
Block a user