Skip to content

Commit fbd9bd5

Browse files
committed
✨ feat: 在src/instant.rs中添加JsInstant结构体及相关实现,在src/lib.rs中导出JsInstant
💡 docs: 在src/instant.rs中添加JsInstant的注释说明
1 parent 5a2da7e commit fbd9bd5

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/instant.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,25 @@ impl InstantProvider<embassy_time::Duration> for embassy_time::Instant {
3333
}
3434

3535
#[cfg(feature = "wasm")]
36-
impl InstantProvider<Duration> for js_sys::Date {
36+
/// A wrapper around `js_sys::Date` that implements `InstantProvider`.
37+
///
38+
/// This type stores the timestamp as milliseconds since the Unix epoch.
39+
#[derive(Clone, PartialEq)]
40+
pub struct JsInstant(f64);
41+
42+
#[cfg(feature = "wasm")]
43+
impl Sub for JsInstant {
44+
type Output = Duration;
45+
46+
fn sub(self, rhs: Self) -> Self::Output {
47+
let delta_ms = self.0 - rhs.0;
48+
Duration::from_millis(delta_ms as u64)
49+
}
50+
}
51+
52+
#[cfg(feature = "wasm")]
53+
impl InstantProvider<Duration> for JsInstant {
3754
fn now() -> Self {
38-
js_sys::Date::new_0()
55+
JsInstant(js_sys::Date::now())
3956
}
4057
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ pub use config::{ButtonConfig, Mode};
88
pub use instant::InstantProvider;
99
pub use pin_wrapper::PinWrapper;
1010

11+
#[cfg(feature = "wasm")]
12+
pub use instant::JsInstant;
13+
1114
/// Button configuration.
1215
pub mod config;
1316
/// Different current global time sources.

0 commit comments

Comments
 (0)