From 45f3d7dee30ded96c094c8dda00ff3021e7d29e6 Mon Sep 17 00:00:00 2001 From: j-mendez Date: Tue, 16 Jan 2024 15:26:36 -0500 Subject: [PATCH 1/5] feat(page): add wait_for_network_idle --- src/page.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/page.rs b/src/page.rs index 44e87195..90cd7aaa 100644 --- a/src/page.rs +++ b/src/page.rs @@ -303,6 +303,53 @@ impl Page { Ok(self) } + /// Wait for the network to be idle for 500ms + #[cfg(feature = "tokio-runtime")] + pub async fn wait_for_network_idle(&self) -> Result<&Self> { + let mut events = self.event_listener::().await?; + + if let Err(_) = tokio::time::timeout(tokio::time::Duration::from_secs(30), async move { + loop { + let sleep = tokio::time::sleep(tokio::time::Duration::from_millis(500)); + tokio::pin!(sleep); + tokio::select! { + _ = &mut sleep => break, + _ = events.next() => (), + else => break, + } + } + }) + .await + {} + + Ok(self) + } + + /// Wait for the network to be idle for 500ms + #[cfg(feature = "async-std-runtime")] + pub async fn wait_for_network_idle(&self) -> Result<&Self> { + use futures::{future::FutureExt, pin_mut, select}; + let mut events = self.event_listener::().await?; + + async_std::io::timeout(std::time::Duration::from_secs(30), async { + loop { + let t1 = async_std::task::sleep(std::time::Duration::from_millis(500)).fuse(); + let t2 = events.next().fuse(); + + pin_mut!(t1, t2); + + select! { + () = t1 => break, + _ = t2 => (), + } + } + Ok(()) + }) + .await?; + + Ok(self) + } + /// Navigate directly to the given URL. /// /// This resolves directly after the requested URL is fully loaded. From 36be5eeec8b9db00e0bac85ce32a1f563144a728 Mon Sep 17 00:00:00 2001 From: j-mendez Date: Wed, 17 Jan 2024 08:41:54 -0500 Subject: [PATCH 2/5] feat(page): add wait_for_network_idle_with_timeout --- src/page.rs | 60 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/src/page.rs b/src/page.rs index 90cd7aaa..75aabf90 100644 --- a/src/page.rs +++ b/src/page.rs @@ -308,19 +308,15 @@ impl Page { pub async fn wait_for_network_idle(&self) -> Result<&Self> { let mut events = self.event_listener::().await?; - if let Err(_) = tokio::time::timeout(tokio::time::Duration::from_secs(30), async move { - loop { - let sleep = tokio::time::sleep(tokio::time::Duration::from_millis(500)); - tokio::pin!(sleep); - tokio::select! { - _ = &mut sleep => break, - _ = events.next() => (), - else => break, - } + loop { + let sleep = tokio::time::sleep(tokio::time::Duration::from_millis(500)); + tokio::pin!(sleep); + tokio::select! { + _ = &mut sleep => break, + _ = events.next() => (), + else => break, } - }) - .await - {} + } Ok(self) } @@ -331,22 +327,38 @@ impl Page { use futures::{future::FutureExt, pin_mut, select}; let mut events = self.event_listener::().await?; - async_std::io::timeout(std::time::Duration::from_secs(30), async { - loop { - let t1 = async_std::task::sleep(std::time::Duration::from_millis(500)).fuse(); - let t2 = events.next().fuse(); + loop { + let t1 = async_std::task::sleep(std::time::Duration::from_millis(500)).fuse(); + let t2 = events.next().fuse(); - pin_mut!(t1, t2); + pin_mut!(t1, t2); - select! { - () = t1 => break, - _ = t2 => (), - } + select! { + () = t1 => break, + _ = t2 => (), } - Ok(()) - }) - .await?; + } + + Ok(self) + } + /// Wait for the network to be idle for 500ms with a duration timeout + #[cfg(feature = "tokio-runtime")] + pub async fn wait_for_network_idle_with_timeout( + &self, + timeout: core::time::Duration, + ) -> Result<&Self> { + if let Err(_) = tokio::time::timeout(timeout, self.wait_for_network_idle()).await {} + Ok(self) + } + + /// Wait for the network to be idle for 500ms with a duration timeout + #[cfg(feature = "async-std-runtime")] + pub async fn wait_for_network_idle_with_timeout( + &self, + timeout: core::time::Duration, + ) -> Result<&Self> { + let _ = (async_std::future::timeout(timeout, self.wait_for_network_idle()).await).is_err(); Ok(self) } From c90907985099cbb5872a6499899ddd38242e694a Mon Sep 17 00:00:00 2001 From: j-mendez Date: Wed, 17 Jan 2024 11:47:18 -0500 Subject: [PATCH 3/5] chore(minor): remove else fallback select --- src/page.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/page.rs b/src/page.rs index 75aabf90..fb787e00 100644 --- a/src/page.rs +++ b/src/page.rs @@ -313,8 +313,7 @@ impl Page { tokio::pin!(sleep); tokio::select! { _ = &mut sleep => break, - _ = events.next() => (), - else => break, + _ = events.next() => () } } From 9079d0d4504b312dea85c86ffcdf1a9c380f4b33 Mon Sep 17 00:00:00 2001 From: j-mendez Date: Wed, 21 Feb 2024 12:43:18 -0500 Subject: [PATCH 4/5] chore(page): fix wait_for_network_idle break --- src/page.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/page.rs b/src/page.rs index fb787e00..69d8caf6 100644 --- a/src/page.rs +++ b/src/page.rs @@ -312,8 +312,12 @@ impl Page { let sleep = tokio::time::sleep(tokio::time::Duration::from_millis(500)); tokio::pin!(sleep); tokio::select! { - _ = &mut sleep => break, - _ = events.next() => () + _ = &mut sleep => (), + v = events.next() => { + if v.is_none () { + break; + } + } } } @@ -333,8 +337,12 @@ impl Page { pin_mut!(t1, t2); select! { - () = t1 => break, - _ = t2 => (), + () = t1 => (), + v = t2 => { + if v.is_none () { + break; + } + } } } From b548614056bf6ea091023b4df562b6565db9feac Mon Sep 17 00:00:00 2001 From: j-mendez Date: Fri, 23 Feb 2024 16:52:17 -0500 Subject: [PATCH 5/5] chore(page): fix wait_for_network_idle break --- src/page.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/page.rs b/src/page.rs index 69d8caf6..3b97f79f 100644 --- a/src/page.rs +++ b/src/page.rs @@ -312,7 +312,7 @@ impl Page { let sleep = tokio::time::sleep(tokio::time::Duration::from_millis(500)); tokio::pin!(sleep); tokio::select! { - _ = &mut sleep => (), + _ = &mut sleep => break, v = events.next() => { if v.is_none () { break; @@ -337,7 +337,7 @@ impl Page { pin_mut!(t1, t2); select! { - () = t1 => (), + () = t1 => break, v = t2 => { if v.is_none () { break;