wasm fixes

This commit is contained in:
John Smith
2022-11-27 10:52:07 -05:00
parent 87366c7fb2
commit 80c8a62ea1
9 changed files with 36 additions and 74 deletions

View File

@@ -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;

View File

@@ -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

View File

@@ -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);

View File

@@ -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),
}

View File

@@ -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");

View File

@@ -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")]