Fixing issue with Cloudflare Workers when using now(): "LinkError: WebAssembly.Instance(): Import #9 module="./index_bg.js" function="__wbg_now_dc6f7ce2227b5592" error: function import requires a callable"

Similar issue: https://github.com/rustwasm/gloo/pull/249
This commit is contained in:
Kody Musick 2023-06-21 16:28:16 -06:00
parent 07052be44a
commit b5f16b9e05

View File

@ -262,17 +262,17 @@ pub(crate) const fn decode_unix_timestamp_millis(uuid: &Uuid) -> u64 {
millis millis
} }
#[cfg(all(feature = "std", feature = "js", target = "wasm32-unknown-unknown"))] #[cfg(all(feature = "std", feature = "js", all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown")))]
fn now() -> (u64, u32) { fn now() -> (u64, u32) {
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
#[wasm_bindgen] #[wasm_bindgen]
extern "C" { extern "C" {
#[wasm_bindgen(js_namespace = Date)] #[wasm_bindgen(js_namespace = Date, catch)]
fn now() -> f64; fn now() -> Result<f64, JsValue>;
} }
let now = now(); let now = now().unwrap_throw();
let secs = (now / 1_000.0) as u64; let secs = (now / 1_000.0) as u64;
let nanos = ((now % 1_000.0) * 1_000_000.0) as u32; let nanos = ((now % 1_000.0) * 1_000_000.0) as u32;
@ -280,7 +280,7 @@ fn now() -> (u64, u32) {
dbg!((secs, nanos)) dbg!((secs, nanos))
} }
#[cfg(all(feature = "std", any(not(feature = "js"), not(target = "wasm32-unknown-unknown"))))] #[cfg(all(feature = "std", any(not(feature = "js"), not(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown")))))]
fn now() -> (u64, u32) { fn now() -> (u64, u32) {
let dur = std::time::SystemTime::UNIX_EPOCH let dur = std::time::SystemTime::UNIX_EPOCH
.elapsed() .elapsed()