Skip to content
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

Web: remove unnecessary usage of once_cell::unsync::Lazy #3134

Merged
merged 1 commit into from
Oct 8, 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
13 changes: 6 additions & 7 deletions src/platform_impl/web/async.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use atomic_waker::AtomicWaker;
use once_cell::unsync::Lazy;
use std::future;
use std::marker::PhantomData;
use std::ops::Deref;
Expand All @@ -25,7 +24,7 @@ pub struct MainThreadSafe<const SYNC: bool, T: 'static, E: 'static> {

impl<const SYNC: bool, T, E> MainThreadSafe<SYNC, T, E> {
thread_local! {
static MAIN_THREAD: Lazy<bool> = Lazy::new(|| {
static MAIN_THREAD: bool = {
#[wasm_bindgen]
extern "C" {
#[derive(Clone)]
Expand All @@ -37,13 +36,13 @@ impl<const SYNC: bool, T, E> MainThreadSafe<SYNC, T, E> {

let global: Global = js_sys::global().unchecked_into();
!global.window().is_undefined()
});
};
}

#[track_caller]
fn new(value: T, handler: fn(&RwLock<Option<T>>, E)) -> Option<Self> {
Self::MAIN_THREAD.with(|safe| {
if !*safe.deref() {
if !safe {
panic!("only callable from inside the `Window`")
}
});
Expand Down Expand Up @@ -75,7 +74,7 @@ impl<const SYNC: bool, T, E> MainThreadSafe<SYNC, T, E> {

pub fn send(&self, event: E) {
Self::MAIN_THREAD.with(|is_main_thread| {
if *is_main_thread.deref() {
if *is_main_thread {
(self.handler)(&self.value, event)
} else {
self.sender.send(event).unwrap()
Expand All @@ -84,12 +83,12 @@ impl<const SYNC: bool, T, E> MainThreadSafe<SYNC, T, E> {
}

fn is_main_thread(&self) -> bool {
Self::MAIN_THREAD.with(|is_main_thread| *is_main_thread.deref())
Self::MAIN_THREAD.with(|is_main_thread| *is_main_thread)
}

pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R> {
Self::MAIN_THREAD.with(|is_main_thread| {
if *is_main_thread.deref() {
if *is_main_thread {
Some(f(self.value.read().unwrap().as_ref().unwrap()))
} else {
None
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/web/web_sys/fullscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cell::Cell;
use std::rc::Rc;

use js_sys::Promise;
use once_cell::unsync::{Lazy, OnceCell};
use once_cell::unsync::OnceCell;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{JsCast, JsValue};
Expand Down Expand Up @@ -65,7 +65,7 @@ impl FullscreenHandler {

if has_fullscreen_api_support(&self.canvas) {
thread_local! {
static REJECT_HANDLER: Lazy<Closure<dyn FnMut(JsValue)>> = Lazy::new(|| Closure::new(|_| ()));
static REJECT_HANDLER: Closure<dyn FnMut(JsValue)> = Closure::new(|_| ());
}
REJECT_HANDLER.with(|handler| {
let _ = canvas.request_fullscreen().catch(handler);
Expand Down
7 changes: 3 additions & 4 deletions src/platform_impl/web/web_sys/resize_scaling.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use js_sys::{Array, Object};
use once_cell::unsync::Lazy;
use wasm_bindgen::prelude::{wasm_bindgen, Closure};
use wasm_bindgen::{JsCast, JsValue};
use web_sys::{
Expand Down Expand Up @@ -292,7 +291,7 @@ impl Drop for ResizeScaleInternal {
// See <https://bugs.webkit.org/show_bug.cgi?id=219005>.
pub fn has_device_pixel_support() -> bool {
thread_local! {
static DEVICE_PIXEL_SUPPORT: Lazy<bool> = Lazy::new(|| {
static DEVICE_PIXEL_SUPPORT: bool = {
#[wasm_bindgen]
extern "C" {
type ResizeObserverEntryExt;
Expand All @@ -307,8 +306,8 @@ pub fn has_device_pixel_support() -> bool {
&JsValue::from_str("devicePixelContentBoxSize"),
);
!descriptor.is_undefined()
});
};
}

DEVICE_PIXEL_SUPPORT.with(|support| **support)
DEVICE_PIXEL_SUPPORT.with(|support| *support)
}
4 changes: 2 additions & 2 deletions src/platform_impl/web/web_sys/schedule.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use js_sys::{Function, Object, Promise, Reflect};
use once_cell::unsync::{Lazy, OnceCell};
use once_cell::unsync::OnceCell;
use std::time::Duration;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::prelude::wasm_bindgen;
Expand Down Expand Up @@ -73,7 +73,7 @@ impl Schedule {
}

thread_local! {
static REJECT_HANDLER: Lazy<Closure<dyn FnMut(JsValue)>> = Lazy::new(|| Closure::new(|_| ()));
static REJECT_HANDLER: Closure<dyn FnMut(JsValue)> = Closure::new(|_| ());
}
REJECT_HANDLER.with(|handler| {
let _ = scheduler
Expand Down
Loading