Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(page): add wait_for_network_idle #201

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
j-mendez marked this conversation as resolved.
Show resolved Hide resolved
#[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
Loading