chromey 2.46.13

Concurrent chrome devtools protocol automation library for Rust
Documentation
use std::pin::Pin;
use std::task::Context;
use std::time::Duration;

use std::future::Future;
use tokio::time::Sleep;

use crate::handler::REQUEST_TIMEOUT;

/// A background job run periodically.
#[derive(Debug)]
pub(crate) struct PeriodicJob {
    /// The interval between job executions.
    interval: Duration,
    /// The delay timer used to wait between executions.
    delay: Pin<Box<Sleep>>,
}

impl PeriodicJob {
    /// Returns `true` if the job is currently not running but ready
    /// to be run, `false` otherwise.
    pub fn poll_ready(&mut self, cx: &mut Context<'_>) -> bool {
        if !Future::poll(self.delay.as_mut(), cx).is_pending() {
            self.delay
                .as_mut()
                .reset(tokio::time::Instant::now() + self.interval);
            return true;
        }
        false
    }
    pub fn new(interval: Duration) -> Self {
        Self {
            delay: Box::pin(tokio::time::sleep(interval)),
            interval,
        }
    }
}

impl Default for PeriodicJob {
    fn default() -> Self {
        Self::new(Duration::from_millis(REQUEST_TIMEOUT))
    }
}