diff options
| author | Ulf Lilleengen <[email protected]> | 2021-09-13 14:35:40 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2021-09-13 16:42:39 +0200 |
| commit | e24528051b9ed26157bee93e32e37e7b06b8f4cc (patch) | |
| tree | bb0c4f201a6a7df9099d27b3a64f92036d645d45 /examples/wasm/src | |
| parent | f1c35b40c74db489da8e04f1c2e87a1d4030c617 (diff) | |
Add WASM support for executor
* Adds an executor for WASM runtimes based on wasm_bindgen.
* Add time driver based on JS time handling.
* Add example that can run in browser locally.
* Update to critical-section version that supports 'std' flag
Diffstat (limited to 'examples/wasm/src')
| -rw-r--r-- | examples/wasm/src/lib.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/examples/wasm/src/lib.rs b/examples/wasm/src/lib.rs new file mode 100644 index 000000000..0aa32a70b --- /dev/null +++ b/examples/wasm/src/lib.rs | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | #![feature(type_alias_impl_trait)] | ||
| 2 | #![allow(incomplete_features)] | ||
| 3 | |||
| 4 | use embassy::{ | ||
| 5 | executor::Spawner, | ||
| 6 | time::{Duration, Timer}, | ||
| 7 | }; | ||
| 8 | |||
| 9 | #[embassy::task] | ||
| 10 | async fn ticker() { | ||
| 11 | let window = web_sys::window().expect("no global `window` exists"); | ||
| 12 | |||
| 13 | let mut counter = 0; | ||
| 14 | loop { | ||
| 15 | let document = window.document().expect("should have a document on window"); | ||
| 16 | let list = document | ||
| 17 | .get_element_by_id("log") | ||
| 18 | .expect("should have a log element"); | ||
| 19 | |||
| 20 | let li = document | ||
| 21 | .create_element("li") | ||
| 22 | .expect("error creating list item element"); | ||
| 23 | li.set_text_content(Some(&format!("tick {}", counter))); | ||
| 24 | |||
| 25 | list.append_child(&li).expect("error appending list item"); | ||
| 26 | log::info!("tick {}", counter); | ||
| 27 | counter += 1; | ||
| 28 | |||
| 29 | Timer::after(Duration::from_secs(1)).await; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | #[embassy::main] | ||
| 34 | async fn main(spawner: Spawner) { | ||
| 35 | wasm_logger::init(wasm_logger::Config::default()); | ||
| 36 | spawner.spawn(ticker()).unwrap(); | ||
| 37 | } | ||
