Skip to content

history: Use thread_local!'s lazy initialization instead of RefCell<Option<_>> #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 26 additions & 39 deletions crates/history/src/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,47 +250,34 @@ impl Default for BrowserHistory {
fn default() -> Self {
// We create browser history only once.
thread_local! {
static BROWSER_HISTORY: RefCell<Option<BrowserHistory>> = RefCell::default();
static LISTENER: RefCell<Option<EventListener>> = RefCell::default();
static BROWSER_HISTORY: (BrowserHistory, EventListener) = {
let window = window();

let inner = window
.history()
.expect_throw("Failed to create browser history. Are you using a browser?");
let callbacks = Rc::default();

let history = BrowserHistory {
inner,
callbacks,
states: Rc::default(),
};

let listener = {
let history = history.clone();

// Listens to popstate.
EventListener::new(&window, "popstate", move |_| {
history.notify_callbacks();
})
};

(history, listener)
};
}

BROWSER_HISTORY.with(|m| {
let mut m = m.borrow_mut();

match *m {
Some(ref m) => m.clone(),
None => {
let window = window();

let inner = window
.history()
.expect_throw("Failed to create browser history. Are you using a browser?");
let callbacks = Rc::default();

let history = Self {
inner,
callbacks,
states: Rc::default(),
};

{
let history = history.clone();

// Listens to popstate.
LISTENER.with(move |m| {
let mut listener = m.borrow_mut();

*listener = Some(EventListener::new(&window, "popstate", move |_| {
history.notify_callbacks();
}));
});
}

*m = Some(history.clone());
history
}
}
})
BROWSER_HISTORY.with(|(history, _)| history.clone())
}
}

Expand Down
43 changes: 16 additions & 27 deletions crates/history/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::borrow::Cow;
use std::cell::RefCell;
use std::fmt;

use gloo_utils::window;
Expand Down Expand Up @@ -238,36 +237,26 @@ impl HashHistory {
impl Default for HashHistory {
fn default() -> Self {
thread_local! {
static HASH_HISTORY: RefCell<Option<HashHistory>> = RefCell::default();
}

HASH_HISTORY.with(|m| {
let mut m = m.borrow_mut();

match *m {
Some(ref m) => m.clone(),
None => {
let browser_history = BrowserHistory::new();
let browser_location = browser_history.location();

let current_hash = browser_location.hash();
static HASH_HISTORY: HashHistory = {
let browser_history = BrowserHistory::new();
let browser_location = browser_history.location();

// Hash needs to start with #/.
if current_hash.is_empty() || !current_hash.starts_with("#/") {
let url = Self::get_url();
url.set_hash("#/");
let current_hash = browser_location.hash();

browser_history.replace(url.href());
}
// Hash needs to start with #/.
if current_hash.is_empty() || !current_hash.starts_with("#/") {
let url = HashHistory::get_url();
url.set_hash("#/");

let history = Self {
inner: browser_history,
};
browser_history.replace(url.href());
}

*m = Some(history.clone());
history
HashHistory {
inner: browser_history,
}
}
})
};
}

HASH_HISTORY.with(|s| s.clone())
}
}