wasm fixes
This commit is contained in:
@@ -28,6 +28,8 @@ impl RngCore for VeilidRng {
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(target_arch = "wasm32")] {
|
||||
use js_sys::Math;
|
||||
|
||||
pub fn random_bytes(dest: &mut [u8]) -> EyreResult<()> {
|
||||
let len = dest.len();
|
||||
let u32len = len / 4;
|
||||
|
@@ -3,7 +3,7 @@ use std::time::Duration;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(target_arch = "wasm32")] {
|
||||
use async_executors::Bindgen;
|
||||
use async_executors::{Bindgen, Timer};
|
||||
|
||||
pub async fn sleep(millis: u32) {
|
||||
Bindgen.sleep(Duration::from_millis(millis.into())).await
|
||||
|
@@ -295,7 +295,7 @@ pub async fn test_sleep() {
|
||||
if #[cfg(target_arch = "wasm32")] {
|
||||
|
||||
let t1 = Date::now();
|
||||
intf::sleep(1000).await;
|
||||
sleep(1000).await;
|
||||
let t2 = Date::now();
|
||||
assert!((t2-t1) >= 1000.0);
|
||||
|
||||
|
@@ -7,7 +7,7 @@ cfg_if! {
|
||||
where
|
||||
F: Future<Output = T>,
|
||||
{
|
||||
match select(Box::pin(intf::sleep(dur_ms)), Box::pin(f)).await {
|
||||
match select(Box::pin(sleep(dur_ms)), Box::pin(f)).await {
|
||||
Either::Left((_x, _b)) => Err(TimeoutError()),
|
||||
Either::Right((y, _a)) => Ok(y),
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@ cfg_if! {
|
||||
use js_sys::Date;
|
||||
|
||||
pub fn get_timestamp() -> u64 {
|
||||
if utils::is_browser() {
|
||||
if is_browser() {
|
||||
return (Date::now() * 1000.0f64) as u64;
|
||||
} else {
|
||||
panic!("WASM requires browser environment");
|
||||
|
@@ -1,6 +1,7 @@
|
||||
use super::*;
|
||||
use core::sync::atomic::{AtomicI8, Ordering};
|
||||
use js_sys::{global, Reflect};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
@@ -27,21 +28,21 @@ pub fn is_browser() -> bool {
|
||||
res
|
||||
}
|
||||
|
||||
// pub fn is_browser_https() -> bool {
|
||||
// static CACHE: AtomicI8 = AtomicI8::new(-1);
|
||||
// let cache = CACHE.load(Ordering::Relaxed);
|
||||
// if cache != -1 {
|
||||
// return cache != 0;
|
||||
// }
|
||||
pub fn is_browser_https() -> bool {
|
||||
static CACHE: AtomicI8 = AtomicI8::new(-1);
|
||||
let cache = CACHE.load(Ordering::Relaxed);
|
||||
if cache != -1 {
|
||||
return cache != 0;
|
||||
}
|
||||
|
||||
// let res = js_sys::eval("window.location.protocol === 'https'")
|
||||
// .map(|res| res.is_truthy())
|
||||
// .unwrap_or_default();
|
||||
let res = js_sys::eval("window.location.protocol === 'https'")
|
||||
.map(|res| res.is_truthy())
|
||||
.unwrap_or_default();
|
||||
|
||||
// CACHE.store(res as i8, Ordering::Relaxed);
|
||||
CACHE.store(res as i8, Ordering::Relaxed);
|
||||
|
||||
// res
|
||||
// }
|
||||
res
|
||||
}
|
||||
|
||||
#[derive(ThisError, Debug, Clone, Eq, PartialEq)]
|
||||
#[error("JsValue error")]
|
||||
|
Reference in New Issue
Block a user