Skip to content

Commit ea2893a

Browse files
committed
chore: js_sys -> web_sys performance API
1 parent ae60104 commit ea2893a

File tree

3 files changed

+7
-35
lines changed

3 files changed

+7
-35
lines changed

Cargo.lock

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

tools/ssr-e2e-harness/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ edition = "2021"
55

66
[dependencies]
77
gloo = { workspace = true, features = ["futures"] }
8-
web-sys = { workspace = true }
8+
web-sys = { workspace = true, features = ["Performance", "PerformanceEntry"] }
99
wasm-bindgen = { workspace = true }
10-
js-sys = { workspace = true }

tools/ssr-e2e-harness/src/lib.rs

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::time::Duration;
22

33
use gloo::utils::document;
4-
use js_sys::{Array, Reflect};
54
use wasm_bindgen::prelude::*;
65

76
/// Returns the `<div id="output">` element used by wasm-bindgen-test as the
@@ -58,45 +57,20 @@ pub fn push_route(path: &str) {
5857
.unwrap();
5958
}
6059

61-
fn window_js() -> JsValue {
62-
web_sys::window().unwrap().into()
63-
}
64-
65-
fn performance() -> JsValue {
66-
Reflect::get(&window_js(), &JsValue::from_str("performance")).unwrap()
67-
}
68-
69-
fn call_method(obj: &JsValue, method: &str, args: &Array) -> JsValue {
70-
let func: js_sys::Function = Reflect::get(obj, &JsValue::from_str(method))
71-
.unwrap()
72-
.into();
73-
Reflect::apply(&func, obj, args).unwrap()
74-
}
75-
76-
fn resource_entries() -> Array {
77-
let perf = performance();
78-
call_method(
79-
&perf,
80-
"getEntriesByType",
81-
&Array::of1(&JsValue::from_str("resource")),
82-
)
83-
.into()
60+
fn performance() -> web_sys::Performance {
61+
web_sys::window().unwrap().performance().unwrap()
8462
}
8563

8664
/// Counts completed network requests to URLs containing `needle` using the
8765
/// Performance Resource Timing API. This works regardless of how the request
8866
/// was initiated (gloo-net, window.fetch, XMLHttpRequest, etc.) because it
8967
/// observes the browser's actual network activity.
9068
pub fn resource_request_count(needle: &str) -> u32 {
91-
let entries = resource_entries();
69+
let entries = performance().get_entries_by_type("resource");
9270
let mut count = 0;
9371
for i in 0..entries.length() {
94-
let entry = entries.get(i);
95-
let name = Reflect::get(&entry, &JsValue::from_str("name"))
96-
.unwrap()
97-
.as_string()
98-
.unwrap_or_default();
99-
if name.contains(needle) {
72+
let entry: web_sys::PerformanceEntry = entries.get(i).unchecked_into();
73+
if entry.name().contains(needle) {
10074
count += 1;
10175
}
10276
}
@@ -106,5 +80,5 @@ pub fn resource_request_count(needle: &str) -> u32 {
10680
/// Clears all resource timing entries so that subsequent calls to
10781
/// [`resource_request_count`] only see new requests.
10882
pub fn clear_resource_timings() {
109-
call_method(&performance(), "clearResourceTimings", &Array::new());
83+
performance().clear_resource_timings();
11084
}

0 commit comments

Comments
 (0)