11use std:: time:: Duration ;
22
33use gloo:: utils:: document;
4- use js_sys:: { Array , Reflect } ;
54use 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.
9068pub 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.
10882pub fn clear_resource_timings ( ) {
109- call_method ( & performance ( ) , "clearResourceTimings" , & Array :: new ( ) ) ;
83+ performance ( ) . clear_resource_timings ( ) ;
11084}
0 commit comments