From e9e044b1604c759b951c47847ed61f9fdd2704e1 Mon Sep 17 00:00:00 2001 From: Alex Kazik Date: Tue, 10 Oct 2023 16:27:29 +0200 Subject: [PATCH 1/2] Update Cargo.lock --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index eb6ea0d6..03a1d5a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -607,6 +607,7 @@ dependencies = [ "futures", "futures-channel", "futures-core", + "futures-io", "futures-sink", "gloo-utils 0.2.0", "http", From 137df4be5c995c75db59b897ded5459bd5c59a7d Mon Sep 17 00:00:00 2001 From: Alex Kazik Date: Mon, 9 Oct 2023 22:31:14 +0200 Subject: [PATCH 2/2] (history): Use a different starting id each time (#369) --- Cargo.lock | 3 +++ crates/history/Cargo.toml | 3 +++ crates/history/src/utils.rs | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 03a1d5a4..4a4b2ca9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -402,8 +402,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi 0.11.0+wasi-snapshot-preview1", + "wasm-bindgen", ] [[package]] @@ -567,6 +569,7 @@ dependencies = [ name = "gloo-history" version = "0.2.0" dependencies = [ + "getrandom", "gloo-events 0.2.0", "gloo-timers 0.3.0", "gloo-utils 0.2.0", diff --git a/crates/history/Cargo.toml b/crates/history/Cargo.toml index 4c6233ee..ab18e212 100644 --- a/crates/history/Cargo.toml +++ b/crates/history/Cargo.toml @@ -24,6 +24,9 @@ thiserror = { version = "1.0", optional = true } version = "0.3" features = ["History", "Window", "Location", "Url"] +[target.'cfg(target_arch = "wasm32")'.dependencies] +getrandom = { version = "0.2.10", features = ["js"] } + [dev-dependencies] wasm-bindgen-test = "0.3" gloo-timers = { version = "0.3.0", features = ["futures"], path = "../timers" } diff --git a/crates/history/src/utils.rs b/crates/history/src/utils.rs index 3994c91f..24422d48 100644 --- a/crates/history/src/utils.rs +++ b/crates/history/src/utils.rs @@ -4,12 +4,30 @@ use std::sync::atomic::{AtomicU32, Ordering}; use wasm_bindgen::throw_str; +#[cfg(not(target_arch = "wasm32"))] pub(crate) fn get_id() -> u32 { static ID_CTR: AtomicU32 = AtomicU32::new(0); ID_CTR.fetch_add(1, Ordering::SeqCst) } +#[cfg(target_arch = "wasm32")] +pub(crate) fn get_id() -> u32 { + static ID_CTR: AtomicU32 = AtomicU32::new(0); + static INIT: std::sync::Once = std::sync::Once::new(); + + INIT.call_once(|| { + let mut start: [u8; 4] = [0; 4]; + // If it fails then the start is not or only partly filled. + // But since this method should not fail, we take what we get. + let _ = getrandom::getrandom(&mut start); + // Using a high initial value is not an issue as `fetch_add` does wrap around. + ID_CTR.store(u32::from_ne_bytes(start), Ordering::SeqCst); + }); + + ID_CTR.fetch_add(1, Ordering::SeqCst) +} + pub(crate) fn assert_absolute_path(path: &str) { if !path.starts_with('/') { throw_str("You cannot use relative path with this history type.");