Skip to content

Commit

Permalink
feat(page): add wait_for_network_idle_with_timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Jan 17, 2024
1 parent 45f3d7d commit 36be5ee
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,15 @@ impl Page {
pub async fn wait_for_network_idle(&self) -> Result<&Self> {
let mut events = self.event_listener::<chromiumoxide_cdp::cdp::browser_protocol::network::EventLoadingFinished>().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)
}
Expand All @@ -331,22 +327,38 @@ impl Page {
use futures::{future::FutureExt, pin_mut, select};
let mut events = self.event_listener::<chromiumoxide_cdp::cdp::browser_protocol::network::EventLoadingFinished>().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)
}

Expand Down

0 comments on commit 36be5ee

Please sign in to comment.