Skip to content

Commit 229edd6

Browse files
committed
Merge branch 'master' of github.com:rustwasm/gloo into future-gate
2 parents 4664ccf + 4636c9a commit 229edd6

15 files changed

+195
-0
lines changed

.azure-pipelines.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pr: ["master"]
2+
3+
jobs:
4+
- job:
5+
pool:
6+
vmImage: 'ubuntu-16.04'
7+
steps:
8+
- template: .ci/install-rust.yml
9+
parameters:
10+
rust_version: stable
11+
- bash: |
12+
export RUST_BACKTRACE=1
13+
cargo check --all || exit
14+
cargo test --all || exit
15+
displayName: Run cargo check and test

.ci/install-rust.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
steps:
2+
- script: |
3+
set -e
4+
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain $RUSTUP_TOOLCHAIN
5+
echo "##vso[task.setvariable variable=PATH;]$PATH:$HOME/.cargo/bin"
6+
env:
7+
RUSTUP_TOOLCHAIN: ${{parameters.rust_version}}
8+
displayName: Install Rust
9+
10+
# All platforms.
11+
- script: |
12+
rustc -Vv
13+
cargo -V
14+
displayName: Query rust and cargo versions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/target
22
**/*.rs.bk
33
Cargo.lock
4+
5+
guide/book

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ Here is a checklist that all Gloo utility crates should fulfill:
141141

142142
* [ ] Crate's root module documentation has at least one realistic example.
143143

144+
* [ ] Crate has at least a brief description of how to use it in the Gloo guide.
145+
144146
[unwrap-throw]: https://docs.rs/wasm-bindgen/0.2.37/wasm_bindgen/trait.UnwrapThrowExt.html
145147
[api-guidelines]: https://rust-lang-nursery.github.io/api-guidelines/
146148

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ version = "0.1.0"
99

1010
[dependencies]
1111
gloo-timers = { version = "0.1.0", path = "crates/timers" }
12+
gloo-console-timer = { version = "0.1.0", path = "crates/console-timer" }
1213

1314
[features]
1415
default = []

crates/console-timer/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "gloo-console-timer"
3+
version = "0.1.0"
4+
authors = ["Rust and WebAssembly Working Group"]
5+
edition = "2018"
6+
7+
[dependencies.web-sys]
8+
version = "0.3.14"
9+
features = [
10+
"console",
11+
]
12+
13+
[dev-dependencies]
14+
wasm-bindgen-test = "0.2.37"
15+
gloo-timers = { version = "0.1.0", path = "../timers" }

crates/console-timer/src/lib.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*!
2+
3+
The `console.time` and `console.timeEnd` functions allow you to log the
4+
timing of named operations to the browser's developer tools console. You
5+
call `console.time("foo")` when the operation begins, and call
6+
`console.timeEnd("foo")` when it finishes.
7+
8+
Additionally, these measurements will show up in your browser's profiler's
9+
"timeline" or "waterfall" view.
10+
11+
[See MDN for more info](https://developer.mozilla.org/en-US/docs/Web/API/console#Timers).
12+
13+
This API wraps both the `time` and `timeEnd` calls into a single type
14+
named `ConsoleTimer`, ensuring both are called.
15+
16+
## Scoped Measurement
17+
18+
Wrap code to be measured in a closure with `ConsoleTimer::scope`.
19+
20+
```no_run
21+
use gloo_console_timer::ConsoleTimer;
22+
23+
let value = ConsoleTimer::scope("foo", || {
24+
// Place code to be measured here
25+
// Optionally return a value.
26+
});
27+
```
28+
29+
## RAII-Style Measurement
30+
31+
For scenarios where `ConsoleTimer::scope` can't be used, like with
32+
asynchronous operations, you can use `ConsoleTimer::new` to create a timer.
33+
The measurement ends when the timer object goes out of scope / is dropped.
34+
35+
```no_run
36+
use gloo_console_timer::ConsoleTimer;
37+
use gloo_timers::Timeout;
38+
39+
// Start timing a new operation.
40+
let timer = ConsoleTimer::new("foo");
41+
42+
// And then asynchronously finish timing.
43+
let timeout = Timeout::new(1_000, move || {
44+
drop(timer);
45+
});
46+
```
47+
*/
48+
49+
#![deny(missing_docs, missing_debug_implementations)]
50+
51+
use web_sys::console;
52+
53+
/// A console time measurement.
54+
///
55+
/// See `ConsoleTimer::scope` for starting a labeled time measurement
56+
/// of code wrapped in a closure.
57+
#[derive(Debug)]
58+
pub struct ConsoleTimer<'a> {
59+
label: &'a str,
60+
}
61+
62+
impl<'a> ConsoleTimer<'a> {
63+
/// Starts a console time measurement. The measurement
64+
/// ends when the constructed `ConsoleTimer` object is dropped.
65+
///
66+
/// # Example
67+
///
68+
/// ```no_run
69+
/// use gloo_console_timer::ConsoleTimer;
70+
///
71+
/// let _timer = ConsoleTimer::new("foo");
72+
/// ```
73+
pub fn new(label: &'a str) -> ConsoleTimer<'a> {
74+
console::time_with_label(label);
75+
ConsoleTimer { label }
76+
}
77+
78+
/// Starts a scoped console time measurement
79+
///
80+
/// # Example
81+
///
82+
/// ```no_run
83+
/// use gloo_console_timer::ConsoleTimer;
84+
///
85+
/// let value = ConsoleTimer::scope("foo", || {
86+
/// // Code to measure here
87+
/// });
88+
/// ```
89+
pub fn scope<F, T>(label: &str, f: F) -> T
90+
where
91+
F: FnOnce() -> T,
92+
{
93+
let _timer = ConsoleTimer::new(label);
94+
f()
95+
}
96+
}
97+
98+
impl<'a> Drop for ConsoleTimer<'a> {
99+
fn drop(&mut self) {
100+
console::time_end_with_label(self.label);
101+
}
102+
}

crates/console-timer/tests/web.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! Test suite for the Web and headless browsers.
2+
3+
#![cfg(target_arch = "wasm32")]
4+
5+
use gloo_console_timer::ConsoleTimer;
6+
use wasm_bindgen_test::*;
7+
8+
wasm_bindgen_test_configure!(run_in_browser);
9+
10+
#[wasm_bindgen_test]
11+
fn scoped_timer_returns_value() {
12+
let value = ConsoleTimer::scope("foo", || true);
13+
14+
assert!(value);
15+
}

guide/book.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[book]
2+
authors = ["The Rust and WebAssembly Working Group"]
3+
multilingual = false
4+
src = "src"
5+
title = "Gloo Guide"

guide/src/SUMMARY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The Gloo Guide
2+
3+
- [Introduction](./introduction.md)
4+
- [Using Gloo crates](./using-gloo.md)
5+
- [Timers](./timers.md)
6+
- [Console-timer](./console-timer.md)

guide/src/console-timer.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Console Timer

guide/src/introduction.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Introduction
2+
3+
Welcome to the Gloo Guide, an introductory guide about [Gloo](https://github.com/rustwasm/gloo).
4+
Gloo is a toolkit for building web applications and libraries with Rust and Wasm, composed of
5+
modular crates. [Gloo crates](https://github.com/rustwasm/gloo/tree/master/crates)
6+
include example code, both in their respective `example` folders, and commented in their code, as well
7+
as [API documentation](https://docs.rs/gloo/). The purpose of this guide is to complement these
8+
by providing narritive description,
9+
and step-by-step of how to use Gloo and its crates.
10+
11+
This guide assumes familiarity with the [Rust language](https://www.rust-lang.org/), and web programming.
12+
If you'd like to learn Rust,
13+
check out the [Rust Book](https://doc.rust-lang.org/book/index.html). The [MDN web docs](https://developer.mozilla.org/en-US/)
14+
is a good reference for web programming.

guide/src/timers.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Timers

guide/src/using-gloo.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Using Gloo crates

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
#![deny(missing_docs, missing_debug_implementations)]
55

66
// Re-exports of toolkit crates.
7+
pub use gloo_console_timer as console_timer;
78
pub use gloo_timers as timers;

0 commit comments

Comments
 (0)