diff --git a/Cargo.toml b/Cargo.toml index be1c09c5..0a5cd21a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,5 @@ [workspace] +resolver = "2" members = [ "crates/concoct", "crates/concoct-web", diff --git a/crates/concoct/Cargo.toml b/crates/concoct/Cargo.toml index 2030ddf0..ac6bd670 100644 --- a/crates/concoct/Cargo.toml +++ b/crates/concoct/Cargo.toml @@ -11,6 +11,7 @@ full = [] [dependencies] futures = "0.3.30" +rustc-hash = "1.1.0" slotmap = "1.0.7" tracing = "0.1.40" diff --git a/crates/concoct/src/hook/mod.rs b/crates/concoct/src/hook/mod.rs index da0ee1df..23a3af60 100644 --- a/crates/concoct/src/hook/mod.rs +++ b/crates/concoct/src/hook/mod.rs @@ -1,6 +1,9 @@ mod use_context; pub use use_context::{use_context, use_provider}; +mod use_effect; +pub use self::use_effect::use_effect; + mod use_ref; pub use use_ref::use_ref; diff --git a/crates/concoct/src/hook/use_effect.rs b/crates/concoct/src/hook/use_effect.rs new file mode 100644 index 00000000..0cdc1c64 --- /dev/null +++ b/crates/concoct/src/hook/use_effect.rs @@ -0,0 +1,20 @@ +use super::use_ref; +use rustc_hash::FxHasher; + +use std::hash::{Hash, Hasher}; + +pub fn use_effect(dependencies: impl Hash, effect: impl FnOnce()) { + let mut hasher = FxHasher::default(); + dependencies.hash(&mut hasher); + let hash = hasher.finish(); + + let mut is_initial = false; + let last_hash = use_ref(|| { + is_initial = true; + hash + }); + + if is_initial || hash != *last_hash { + effect() + } +}