Clock

Struct Clock 

Source
pub struct Clock<'a> { /* private fields */ }
Expand description

Clock controller for mocking time in a browser page.

The Clock API allows you to control JavaScript’s time-related functions including Date, setTimeout, setInterval, requestAnimationFrame, and performance.now().

§Example

use std::time::Duration;

// Install clock mocking
page.clock().install().await.unwrap();

// Freeze time at a specific moment
page.clock().set_fixed_time("2024-01-01T00:00:00Z").await.unwrap();

// Advance time and fire scheduled timers
page.clock().run_for(Duration::from_secs(5)).await.unwrap();

// Uninstall when done
page.clock().uninstall().await.unwrap();

Implementations§

Source§

impl<'a> Clock<'a>

Source

pub async fn install(&mut self) -> Result<(), PageError>

Install clock mocking on the page.

This replaces Date, setTimeout, setInterval, requestAnimationFrame, and performance.now() with mocked versions that can be controlled.

§Example
use viewpoint_core::Page;

page.clock().install().await?;
§Errors

Returns an error if the clock cannot be installed.

Source

pub async fn uninstall(&mut self) -> Result<(), PageError>

Uninstall clock mocking and restore original functions.

§Example
use viewpoint_core::Page;

page.clock().uninstall().await?;
§Errors

Returns an error if the clock cannot be uninstalled.

Source

pub async fn set_fixed_time( &self, time: impl Into<TimeValue>, ) -> Result<(), PageError>

Set a fixed time that doesn’t advance.

All calls to Date.now() and new Date() will return this time. Time remains frozen until you call run_for, fast_forward, set_system_time, or resume.

§Arguments
  • time - The time to set, either as an ISO 8601 string (e.g., “2024-01-01T00:00:00Z”) or a Unix timestamp in milliseconds.
§Example
use viewpoint_core::Page;

// Using ISO string
page.clock().set_fixed_time("2024-01-01T00:00:00Z").await?;

// Using timestamp
page.clock().set_fixed_time(1704067200000i64).await?;
§Errors

Returns an error if setting the time fails.

Source

pub async fn set_system_time( &self, time: impl Into<TimeValue>, ) -> Result<(), PageError>

Set the system time that flows normally.

Time starts from the specified value and advances in real time.

§Arguments
  • time - The starting time, either as an ISO 8601 string or Unix timestamp.
§Example
use viewpoint_core::Page;

page.clock().set_system_time("2024-01-01T00:00:00Z").await?;
// Time will now flow from 2024-01-01
§Errors

Returns an error if setting the time fails.

Source

pub async fn run_for(&self, duration: Duration) -> Result<u32, PageError>

Advance time by a duration, firing any scheduled timers.

This advances the clock and executes any setTimeout/setInterval callbacks that were scheduled to fire during that period.

§Arguments
  • duration - The amount of time to advance.
§Returns

The number of timers that were fired.

§Example
use viewpoint_core::Page;
use std::time::Duration;

// Advance 5 seconds, firing any timers scheduled in that period
let fired = page.clock().run_for(Duration::from_secs(5)).await?;
println!("Fired {} timers", fired);
§Errors

Returns an error if advancing time fails.

Source

pub async fn fast_forward(&self, duration: Duration) -> Result<(), PageError>

Fast-forward time without firing timers.

This advances the clock but does NOT execute scheduled timers. Use this when you want to jump ahead in time quickly.

§Arguments
  • duration - The amount of time to skip.
§Example
use viewpoint_core::Page;
use std::time::Duration;

// Skip ahead 1 hour without firing any timers
page.clock().fast_forward(Duration::from_secs(3600)).await?;
§Errors

Returns an error if fast-forwarding fails.

Source

pub async fn pause_at( &self, time: impl Into<TimeValue>, ) -> Result<(), PageError>

Pause at a specific time.

This sets the clock to the specified time and pauses it there.

§Arguments
  • time - The time to pause at, as an ISO string or timestamp.
§Example
use viewpoint_core::Page;

// Pause at noon
page.clock().pause_at("2024-01-01T12:00:00Z").await?;
§Errors

Returns an error if pausing fails.

Source

pub async fn resume(&self) -> Result<(), PageError>

Resume normal time flow.

After calling this, time will advance normally from the current mocked time value.

§Example
use viewpoint_core::Page;

page.clock().resume().await?;
§Errors

Returns an error if resuming fails.

Source

pub async fn run_all_timers(&self) -> Result<u32, PageError>

Run all pending timers.

This advances time to execute all scheduled setTimeout and setInterval callbacks, as well as requestAnimationFrame callbacks.

§Returns

The number of timers that were fired.

§Example
use viewpoint_core::Page;

let fired = page.clock().run_all_timers().await?;
§Errors

Returns an error if running timers fails.

Source

pub async fn run_to_last(&self) -> Result<u32, PageError>

Run to the last scheduled timer.

This advances time to the last scheduled timer and executes all timers up to that point.

§Returns

The number of timers that were fired.

§Example
use viewpoint_core::Page;

let fired = page.clock().run_to_last().await?;
§Errors

Returns an error if running timers fails.

Source

pub async fn pending_timer_count(&self) -> Result<u32, PageError>

Get the number of pending timers.

This includes setTimeout, setInterval, and requestAnimationFrame callbacks.

§Example
use viewpoint_core::Page;

let count = page.clock().pending_timer_count().await?;
println!("{} timers pending", count);
§Errors

Returns an error if getting the count fails.

Source

pub async fn is_installed(&self) -> Result<bool, PageError>

Check if clock mocking is installed.

§Example
use viewpoint_core::Page;

if page.clock().is_installed().await? {
    println!("Clock is mocked");
}
§Errors

Returns an error if the check fails.

Trait Implementations§

Source§

impl<'a> Debug for Clock<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Clock<'a>

§

impl<'a> !RefUnwindSafe for Clock<'a>

§

impl<'a> Send for Clock<'a>

§

impl<'a> Sync for Clock<'a>

§

impl<'a> Unpin for Clock<'a>

§

impl<'a> !UnwindSafe for Clock<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more