Skip to content

Commit 5d6f126

Browse files
committed
✨ feat(examples/wasm): 在Cargo.toml中为button-driver添加wasm特性,简化lib.rs代码并修改Instant实现
1 parent 0ee253f commit 5d6f126

File tree

3 files changed

+9
-29
lines changed

3 files changed

+9
-29
lines changed

examples/wasm/Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/wasm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66
[dependencies]
77
wasm-bindgen = { version = "0.2" }
88
web-sys = { version = "0.3.69", features = ["Window", "Document", "Element", "MouseEvent"] }
9-
button-driver = { path = "../../" }
9+
button-driver = { path = "../../", features = ["wasm"] }
1010
log = "0.4.22"
1111
wasm-logger = "0.2.0"
1212

examples/wasm/src/lib.rs

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,19 @@
11
//! A WebAssembly example demonstrating button driver usage in the browser.
2-
//! This example shows how to implement a custom `Instant` provider using JavaScript's `Date.now()`.
32
//!
43
//! To run this example:
54
//! 1. Install trunk: `cargo install trunk`
65
//! 2. Run: `trunk serve`
76
//! 3. Open your browser at `http://localhost:8080`
7+
//!
8+
//! Required features: `wasm` (which includes `std` and `js-sys`).
89
9-
use std::{cell::RefCell, ops::Sub, rc::Rc, time::Duration};
10+
use std::{cell::RefCell, rc::Rc};
1011

11-
use button_driver::{Button, ButtonConfig, InstantProvider, Mode, PinWrapper};
12+
use button_driver::{Button, ButtonConfig, Mode, PinWrapper};
1213
use log::info;
1314
use wasm_bindgen::{closure::Closure, JsCast};
1415
use web_sys::{js_sys::Date, MouseEvent};
1516

16-
#[derive(PartialEq, Clone)]
17-
struct WasmInstant(u64);
18-
19-
impl WasmInstant {
20-
fn now() -> WasmInstant {
21-
WasmInstant(Date::now() as u64)
22-
}
23-
}
24-
25-
impl Sub for WasmInstant {
26-
type Output = Duration;
27-
28-
fn sub(self, rhs: Self) -> Self::Output {
29-
let delta_ms = self.0 - rhs.0;
30-
return Duration::from_millis(delta_ms);
31-
}
32-
}
33-
34-
impl InstantProvider<Duration> for WasmInstant {
35-
fn now() -> Self {
36-
WasmInstant::now()
37-
}
38-
}
39-
4017
#[derive(Clone, Default)]
4118
struct ButtonInput(Rc<RefCell<bool>>);
4219

@@ -77,7 +54,7 @@ pub fn main() {
7754
mouse_up_handler.forget();
7855
}
7956
{
80-
let mut button = Button::<_, WasmInstant>::new(
57+
let mut button = Button::<_, Date>::new(
8158
pin_state,
8259
ButtonConfig {
8360
mode: Mode::PullDown,

0 commit comments

Comments
 (0)