Web: remove unnecessary usage of once_cell::unsync::Lazy (#3134)
This commit is contained in:
parent
48a1e84906
commit
f5dd1c008c
4 changed files with 13 additions and 15 deletions
|
|
@ -1,5 +1,4 @@
|
|||
use atomic_waker::AtomicWaker;
|
||||
use once_cell::unsync::Lazy;
|
||||
use std::future;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Deref;
|
||||
|
|
@ -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)]
|
||||
|
|
@ -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`")
|
||||
}
|
||||
});
|
||||
|
|
@ -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()
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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};
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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::{
|
||||
|
|
@ -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;
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue