chore(napi): replace lazy_static with once_cell (#1213)

This commit is contained in:
Jonas Platte 2022-06-25 05:19:45 +02:00 committed by GitHub
parent f879a05b7f
commit 7cf87eaf20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 44 deletions

View file

@ -49,7 +49,7 @@ tokio_time = ["tokio/time"]
[dependencies]
ctor = "0.1"
lazy_static = "1"
once_cell = "1"
thread_local = "1"
[dependencies.napi-sys]

View file

@ -4,16 +4,14 @@ use std::ptr;
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use thread_local::ThreadLocal;
use crate::{bindgen_prelude::*, check_status, sys, Result};
lazy_static! {
#[doc(hidden)]
/// Determined is `constructor` called from Class `factory`
pub static ref ___CALL_FROM_FACTORY: ThreadLocal<AtomicBool> = ThreadLocal::new();
}
#[doc(hidden)]
/// Determined is `constructor` called from Class `factory`
pub static ___CALL_FROM_FACTORY: Lazy<ThreadLocal<AtomicBool>> = Lazy::new(ThreadLocal::new);
pub struct CallbackInfo<const N: usize> {
env: sys::napi_env,

View file

@ -3,7 +3,7 @@ use std::ffi::c_void;
use std::ops::{Deref, DerefMut};
use std::rc::{Rc, Weak};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use crate::{
bindgen_runtime::{PersistedSingleThreadHashMap, ToNapiValue},
@ -16,10 +16,8 @@ type RefInformation = (
*const Cell<*mut dyn FnOnce()>,
);
lazy_static! {
pub(crate) static ref REFERENCE_MAP: PersistedSingleThreadHashMap<*mut c_void, RefInformation> =
Default::default();
}
pub(crate) static REFERENCE_MAP: Lazy<PersistedSingleThreadHashMap<*mut c_void, RefInformation>> =
Lazy::new(Default::default);
/// ### Experimental feature
///

View file

@ -5,7 +5,7 @@ use std::ptr;
use std::sync::atomic::{AtomicBool, AtomicPtr};
use std::sync::{atomic::Ordering, Mutex};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use crate::{
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 =
PersistedSingleThreadHashMap<ExportRegisterCallback, (sys::napi_callback, &'static str)>;
lazy_static! {
static ref MODULE_REGISTER_CALLBACK: ModuleRegisterCallback = Default::default();
static ref MODULE_CLASS_PROPERTIES: ModuleClassProperty = Default::default();
static ref MODULE_REGISTER_LOCK: Mutex<()> = Mutex::new(());
static ref REGISTERED: AtomicBool = AtomicBool::new(false);
static ref REGISTERED_CLASSES: thread_local::ThreadLocal<AtomicPtr<RegisteredClasses>> =
thread_local::ThreadLocal::new();
static ref FN_REGISTER_MAP: FnRegisterMap = Default::default();
}
static MODULE_REGISTER_CALLBACK: Lazy<ModuleRegisterCallback> = Lazy::new(Default::default);
static MODULE_CLASS_PROPERTIES: Lazy<ModuleClassProperty> = Lazy::new(Default::default);
static MODULE_REGISTER_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
static REGISTERED: Lazy<AtomicBool> = Lazy::new(|| AtomicBool::new(false));
static REGISTERED_CLASSES: Lazy<thread_local::ThreadLocal<AtomicPtr<RegisteredClasses>>> =
Lazy::new(thread_local::ThreadLocal::new);
static FN_REGISTER_MAP: Lazy<FnRegisterMap> = Lazy::new(Default::default);
#[inline]
fn wait_first_thread_registered() {
@ -111,9 +109,9 @@ type RegisteredClasses =
#[cfg(feature = "compat-mode")]
// 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)]
pub fn get_class_constructor(js_name: &'static str) -> Option<sys::napi_ref> {

View file

@ -3,7 +3,7 @@ use std::future::Future;
use std::ptr;
use std::sync::atomic::{AtomicUsize, Ordering};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use tokio::{
runtime::Handle,
sync::mpsc::{self, error::TrySendError},
@ -11,25 +11,23 @@ use tokio::{
use crate::{check_status, promise, sys, Result};
lazy_static! {
pub(crate) static ref RT: (Handle, mpsc::Sender<()>) = {
let runtime = tokio::runtime::Runtime::new();
let (sender, mut receiver) = mpsc::channel::<()>(1);
runtime
.map(|rt| {
let h = rt.handle();
let handle = h.clone();
handle.spawn(async move {
if receiver.recv().await.is_some() {
rt.shutdown_background();
}
});
pub(crate) static RT: Lazy<(Handle, mpsc::Sender<()>)> = Lazy::new(|| {
let runtime = tokio::runtime::Runtime::new();
let (sender, mut receiver) = mpsc::channel::<()>(1);
runtime
.map(|rt| {
let h = rt.handle();
let handle = h.clone();
handle.spawn(async move {
if receiver.recv().await.is_some() {
rt.shutdown_background();
}
});
(handle, sender)
})
.expect("Create tokio runtime failed")
};
}
(handle, sender)
})
.expect("Create tokio runtime failed")
});
pub(crate) static TOKIO_RT_REF_COUNT: AtomicUsize = AtomicUsize::new(0);