Skip to content

Commit

Permalink
(history): Use a different starting id each time (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkazik committed Oct 14, 2023
1 parent e9e044b commit 1ad7fc0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/history/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
18 changes: 18 additions & 0 deletions crates/history/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down

0 comments on commit 1ad7fc0

Please sign in to comment.