Skip to content

Commit 58b335c

Browse files
committed
history: Use thread_local!'s lazy initialization instead of RefCell<Option<_>>
1 parent ff19d20 commit 58b335c

File tree

2 files changed

+42
-66
lines changed

2 files changed

+42
-66
lines changed

crates/history/src/browser.rs

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -250,47 +250,34 @@ impl Default for BrowserHistory {
250250
fn default() -> Self {
251251
// We create browser history only once.
252252
thread_local! {
253-
static BROWSER_HISTORY: RefCell<Option<BrowserHistory>> = RefCell::default();
254-
static LISTENER: RefCell<Option<EventListener>> = RefCell::default();
253+
static BROWSER_HISTORY: (BrowserHistory, EventListener) = {
254+
let window = window();
255+
256+
let inner = window
257+
.history()
258+
.expect_throw("Failed to create browser history. Are you using a browser?");
259+
let callbacks = Rc::default();
260+
261+
let history = BrowserHistory {
262+
inner,
263+
callbacks,
264+
states: Rc::default(),
265+
};
266+
267+
let listener = {
268+
let history = history.clone();
269+
270+
// Listens to popstate.
271+
EventListener::new(&window, "popstate", move |_| {
272+
history.notify_callbacks();
273+
})
274+
};
275+
276+
(history, listener)
277+
};
255278
}
256279

257-
BROWSER_HISTORY.with(|m| {
258-
let mut m = m.borrow_mut();
259-
260-
match *m {
261-
Some(ref m) => m.clone(),
262-
None => {
263-
let window = window();
264-
265-
let inner = window
266-
.history()
267-
.expect_throw("Failed to create browser history. Are you using a browser?");
268-
let callbacks = Rc::default();
269-
270-
let history = Self {
271-
inner,
272-
callbacks,
273-
states: Rc::default(),
274-
};
275-
276-
{
277-
let history = history.clone();
278-
279-
// Listens to popstate.
280-
LISTENER.with(move |m| {
281-
let mut listener = m.borrow_mut();
282-
283-
*listener = Some(EventListener::new(&window, "popstate", move |_| {
284-
history.notify_callbacks();
285-
}));
286-
});
287-
}
288-
289-
*m = Some(history.clone());
290-
history
291-
}
292-
}
293-
})
280+
BROWSER_HISTORY.with(|(history, _)| history.clone())
294281
}
295282
}
296283

crates/history/src/hash.rs

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::borrow::Cow;
2-
use std::cell::RefCell;
32
use std::fmt;
43

54
use gloo_utils::window;
@@ -238,36 +237,26 @@ impl HashHistory {
238237
impl Default for HashHistory {
239238
fn default() -> Self {
240239
thread_local! {
241-
static HASH_HISTORY: RefCell<Option<HashHistory>> = RefCell::default();
242-
}
243-
244-
HASH_HISTORY.with(|m| {
245-
let mut m = m.borrow_mut();
246-
247-
match *m {
248-
Some(ref m) => m.clone(),
249-
None => {
250-
let browser_history = BrowserHistory::new();
251-
let browser_location = browser_history.location();
252-
253-
let current_hash = browser_location.hash();
240+
static HASH_HISTORY: HashHistory = {
241+
let browser_history = BrowserHistory::new();
242+
let browser_location = browser_history.location();
254243

255-
// Hash needs to start with #/.
256-
if current_hash.is_empty() || !current_hash.starts_with("#/") {
257-
let url = Self::get_url();
258-
url.set_hash("#/");
244+
let current_hash = browser_location.hash();
259245

260-
browser_history.replace(url.href());
261-
}
246+
// Hash needs to start with #/.
247+
if current_hash.is_empty() || !current_hash.starts_with("#/") {
248+
let url = HashHistory::get_url();
249+
url.set_hash("#/");
262250

263-
let history = Self {
264-
inner: browser_history,
265-
};
251+
browser_history.replace(url.href());
252+
}
266253

267-
*m = Some(history.clone());
268-
history
254+
HashHistory {
255+
inner: browser_history,
269256
}
270-
}
271-
})
257+
};
258+
}
259+
260+
HASH_HISTORY.with(|s| s.clone())
272261
}
273262
}

0 commit comments

Comments
 (0)