mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-13 04:51:23 +00:00
Reorganize and rewrite examples
This commit is a large-ish scale reorganization of our examples. The main goal here is to have a dedicated section of the guide for example, and all examples will be listed there. Each example's `README` is now just boilerplate pointing at the guide along with a blurb about how to run it. Some examples like `math` and `smorgasboard` have been deleted as they didn't really serve much purpose, and others like `closures` have been rewritten with `web-sys` instead of hand-bound bindings. Overall it's hoped that this puts us in a good and consistent state for our examples, with all of them being described in the guide, excerpts are in the guide, and they're all relatively idiomatically using `web-sys`.
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "performance"
|
||||
version = "0.1.0"
|
||||
authors = ["Alex Crichton <alex@alexcrichton.com>"]
|
||||
authors = ["The wasm-bindgen Developers"]
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
@ -9,3 +9,7 @@ crate-type = ["cdylib"]
|
||||
[dependencies]
|
||||
wasm-bindgen = { path = "../.." }
|
||||
humantime = "1"
|
||||
|
||||
[dependencies.web-sys]
|
||||
path = '../../crates/web-sys'
|
||||
features = ['Window', 'Performance', 'PerformanceTiming']
|
||||
|
@ -1,12 +1,10 @@
|
||||
# `performance`
|
||||
# web-sys: `performance.now`
|
||||
|
||||
[View this example online](https://webassembly.studio/?f=6gxcp4f4953)
|
||||
[View documentation for this example online][dox]
|
||||
|
||||
This directory is an example of using the `#[wasm_bindgen]` macro to interact
|
||||
with global APIs like `performance.now()` to learn about the current time in
|
||||
Rust.
|
||||
[dox]: https://rustwasm.github.io/wasm-bindgen/examples/performance.html
|
||||
|
||||
You can build the example with:
|
||||
You can build the example locally with:
|
||||
|
||||
```
|
||||
$ ./build.sh
|
||||
@ -14,10 +12,4 @@ $ ./build.sh
|
||||
|
||||
(or running the commands on Windows manually)
|
||||
|
||||
and then opening up `index.html` in a web browser should show a hello message on
|
||||
the web page generated by the wasm.
|
||||
|
||||
For more information about this example be sure to check out
|
||||
[`hello_world`][hello] which also has more comments about caveats and such.
|
||||
|
||||
[hello]: https://github.com/alexcrichton/wasm-bindgen/tree/master/examples/hello_world
|
||||
and then visiting http://localhost:8080 in a browser should run the example!
|
||||
|
@ -3,7 +3,6 @@
|
||||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
|
||||
</head>
|
||||
<body>
|
||||
<script src='./index.js'></script>
|
||||
<p>The developer console should have timing log messages in it</p>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -1,45 +1,35 @@
|
||||
extern crate humantime;
|
||||
extern crate wasm_bindgen;
|
||||
extern crate web_sys;
|
||||
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
// Like with the `dom` example this block will eventually be auto-generated, but
|
||||
// for now we can write it by hand to get by!
|
||||
// lifted from the `console_log` example
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
type Performance;
|
||||
static performance: Performance;
|
||||
#[wasm_bindgen(method)]
|
||||
fn now(this: &Performance) -> f64;
|
||||
#[wasm_bindgen(method, getter)]
|
||||
fn timing(this: &Performance) -> PerformanceTiming;
|
||||
|
||||
type PerformanceTiming;
|
||||
#[wasm_bindgen(method, getter)]
|
||||
fn requestStart(this: &PerformanceTiming) -> f64;
|
||||
#[wasm_bindgen(method, getter)]
|
||||
fn responseEnd(this: &PerformanceTiming) -> f64;
|
||||
|
||||
#[wasm_bindgen(js_namespace = console)]
|
||||
fn log(a: &str);
|
||||
}
|
||||
|
||||
macro_rules! println {
|
||||
macro_rules! console_log {
|
||||
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
|
||||
}
|
||||
|
||||
// Called by our JS entry point to run the example
|
||||
#[wasm_bindgen]
|
||||
pub fn run() {
|
||||
println!("the current time is {}", performance.now());
|
||||
let window = web_sys::window().expect("should have a window in this context");
|
||||
let performance = window.performance().expect("performance should be available");
|
||||
|
||||
let start = perf_to_system(performance.timing().requestStart());
|
||||
let end = perf_to_system(performance.timing().responseEnd());
|
||||
console_log!("the current time (in ms) is {}", performance.now());
|
||||
|
||||
println!("request started at {}", humantime::format_rfc3339(start));
|
||||
println!("request ended at {}", humantime::format_rfc3339(end));
|
||||
let start = perf_to_system(performance.timing().request_start());
|
||||
let end = perf_to_system(performance.timing().response_end());
|
||||
|
||||
console_log!("request started at {}", humantime::format_rfc3339(start));
|
||||
console_log!("request ended at {}", humantime::format_rfc3339(end));
|
||||
}
|
||||
|
||||
fn perf_to_system(amt: f64) -> SystemTime {
|
||||
|
Reference in New Issue
Block a user