Skip to content

Commit

Permalink
feat(page): add wait_for_network_idle
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Jan 16, 2024
1 parent 8d039e5 commit 45f3d7d
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<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,
}
}
})
.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::<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();

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.
Expand Down

0 comments on commit 45f3d7d

Please sign in to comment.