chore(napi): replace lazy_static with once_cell (#1213)
This commit is contained in:
parent
f879a05b7f
commit
7cf87eaf20
5 changed files with 36 additions and 44 deletions
|
@ -49,7 +49,7 @@ tokio_time = ["tokio/time"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ctor = "0.1"
|
ctor = "0.1"
|
||||||
lazy_static = "1"
|
once_cell = "1"
|
||||||
thread_local = "1"
|
thread_local = "1"
|
||||||
|
|
||||||
[dependencies.napi-sys]
|
[dependencies.napi-sys]
|
||||||
|
|
|
@ -4,16 +4,14 @@ use std::ptr;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use once_cell::sync::Lazy;
|
||||||
use thread_local::ThreadLocal;
|
use thread_local::ThreadLocal;
|
||||||
|
|
||||||
use crate::{bindgen_prelude::*, check_status, sys, Result};
|
use crate::{bindgen_prelude::*, check_status, sys, Result};
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
/// Determined is `constructor` called from Class `factory`
|
/// Determined is `constructor` called from Class `factory`
|
||||||
pub static ref ___CALL_FROM_FACTORY: ThreadLocal<AtomicBool> = ThreadLocal::new();
|
pub static ___CALL_FROM_FACTORY: Lazy<ThreadLocal<AtomicBool>> = Lazy::new(ThreadLocal::new);
|
||||||
}
|
|
||||||
|
|
||||||
pub struct CallbackInfo<const N: usize> {
|
pub struct CallbackInfo<const N: usize> {
|
||||||
env: sys::napi_env,
|
env: sys::napi_env,
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::ffi::c_void;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::rc::{Rc, Weak};
|
use std::rc::{Rc, Weak};
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
bindgen_runtime::{PersistedSingleThreadHashMap, ToNapiValue},
|
bindgen_runtime::{PersistedSingleThreadHashMap, ToNapiValue},
|
||||||
|
@ -16,10 +16,8 @@ type RefInformation = (
|
||||||
*const Cell<*mut dyn FnOnce()>,
|
*const Cell<*mut dyn FnOnce()>,
|
||||||
);
|
);
|
||||||
|
|
||||||
lazy_static! {
|
pub(crate) static REFERENCE_MAP: Lazy<PersistedSingleThreadHashMap<*mut c_void, RefInformation>> =
|
||||||
pub(crate) static ref REFERENCE_MAP: PersistedSingleThreadHashMap<*mut c_void, RefInformation> =
|
Lazy::new(Default::default);
|
||||||
Default::default();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// ### Experimental feature
|
/// ### Experimental feature
|
||||||
///
|
///
|
||||||
|
|
|
@ -5,7 +5,7 @@ use std::ptr;
|
||||||
use std::sync::atomic::{AtomicBool, AtomicPtr};
|
use std::sync::atomic::{AtomicBool, AtomicPtr};
|
||||||
use std::sync::{atomic::Ordering, Mutex};
|
use std::sync::{atomic::Ordering, Mutex};
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
check_status, check_status_or_throw, sys, Env, JsError, JsFunction, Property, Result, Value,
|
check_status, check_status_or_throw, sys, Env, JsError, JsFunction, Property, Result, Value,
|
||||||
|
@ -89,15 +89,13 @@ unsafe impl<K, V> Sync for PersistedSingleThreadHashMap<K, V> {}
|
||||||
type FnRegisterMap =
|
type FnRegisterMap =
|
||||||
PersistedSingleThreadHashMap<ExportRegisterCallback, (sys::napi_callback, &'static str)>;
|
PersistedSingleThreadHashMap<ExportRegisterCallback, (sys::napi_callback, &'static str)>;
|
||||||
|
|
||||||
lazy_static! {
|
static MODULE_REGISTER_CALLBACK: Lazy<ModuleRegisterCallback> = Lazy::new(Default::default);
|
||||||
static ref MODULE_REGISTER_CALLBACK: ModuleRegisterCallback = Default::default();
|
static MODULE_CLASS_PROPERTIES: Lazy<ModuleClassProperty> = Lazy::new(Default::default);
|
||||||
static ref MODULE_CLASS_PROPERTIES: ModuleClassProperty = Default::default();
|
static MODULE_REGISTER_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
|
||||||
static ref MODULE_REGISTER_LOCK: Mutex<()> = Mutex::new(());
|
static REGISTERED: Lazy<AtomicBool> = Lazy::new(|| AtomicBool::new(false));
|
||||||
static ref REGISTERED: AtomicBool = AtomicBool::new(false);
|
static REGISTERED_CLASSES: Lazy<thread_local::ThreadLocal<AtomicPtr<RegisteredClasses>>> =
|
||||||
static ref REGISTERED_CLASSES: thread_local::ThreadLocal<AtomicPtr<RegisteredClasses>> =
|
Lazy::new(thread_local::ThreadLocal::new);
|
||||||
thread_local::ThreadLocal::new();
|
static FN_REGISTER_MAP: Lazy<FnRegisterMap> = Lazy::new(Default::default);
|
||||||
static ref FN_REGISTER_MAP: FnRegisterMap = Default::default();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn wait_first_thread_registered() {
|
fn wait_first_thread_registered() {
|
||||||
|
@ -111,9 +109,9 @@ type RegisteredClasses =
|
||||||
|
|
||||||
#[cfg(feature = "compat-mode")]
|
#[cfg(feature = "compat-mode")]
|
||||||
// compatibility for #[module_exports]
|
// compatibility for #[module_exports]
|
||||||
lazy_static! {
|
|
||||||
static ref MODULE_EXPORTS: PersistedSingleThreadVec<ModuleExportsCallback> = Default::default();
|
static MODULE_EXPORTS: Lazy<PersistedSingleThreadVec<ModuleExportsCallback>> =
|
||||||
}
|
Lazy::new(Default::default);
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn get_class_constructor(js_name: &'static str) -> Option<sys::napi_ref> {
|
pub fn get_class_constructor(js_name: &'static str) -> Option<sys::napi_ref> {
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::future::Future;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use once_cell::sync::Lazy;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
runtime::Handle,
|
runtime::Handle,
|
||||||
sync::mpsc::{self, error::TrySendError},
|
sync::mpsc::{self, error::TrySendError},
|
||||||
|
@ -11,8 +11,7 @@ use tokio::{
|
||||||
|
|
||||||
use crate::{check_status, promise, sys, Result};
|
use crate::{check_status, promise, sys, Result};
|
||||||
|
|
||||||
lazy_static! {
|
pub(crate) static RT: Lazy<(Handle, mpsc::Sender<()>)> = Lazy::new(|| {
|
||||||
pub(crate) static ref RT: (Handle, mpsc::Sender<()>) = {
|
|
||||||
let runtime = tokio::runtime::Runtime::new();
|
let runtime = tokio::runtime::Runtime::new();
|
||||||
let (sender, mut receiver) = mpsc::channel::<()>(1);
|
let (sender, mut receiver) = mpsc::channel::<()>(1);
|
||||||
runtime
|
runtime
|
||||||
|
@ -28,8 +27,7 @@ lazy_static! {
|
||||||
(handle, sender)
|
(handle, sender)
|
||||||
})
|
})
|
||||||
.expect("Create tokio runtime failed")
|
.expect("Create tokio runtime failed")
|
||||||
};
|
});
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) static TOKIO_RT_REF_COUNT: AtomicUsize = AtomicUsize::new(0);
|
pub(crate) static TOKIO_RT_REF_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue