manifold.time
This namespace contains methods for converting units of time, with milliseconds as the base representation, and for deferring execution of functions to some time in the future. In practice, the methods here are not necessary to use Manifold effectively - manifold.deferred/timeout
and manifold.stream/periodically
are more directly useful - but they are available for anyone who should need them.
add
(add timestamp value unit)
Takes a timestamp
, and adds value
multiples of unit
to the value.
at
(at timestamp f)
Schedules no-arg function f
to be invoked at timestamp
, which is the milliseconds since the epoch. Returns a deferred representing the returned value of the function.
every
(every period f)
(every period initial-delay f)
Schedules no-arg function f
to be invoked every period
milliseconds, after initial-delay
milliseconds, which defaults to 0
. Returns a zero-argument function which, when invoked, cancels the repeated invocation.
If the invocation of f
ever throws an exception, repeated invocation is automatically cancelled.
floor
(floor timestamp unit)
Takes a timestamp
, and rounds it down to the nearest even multiple of the unit
.
(floor 1001 :second) => 1000 (floor (seconds 61) :minute) => 60000
format-duration
(format-duration n)
Takes a duration in milliseconds, and returns a formatted string describing the interval, i.e. ‘5d 3h 1m’
IMockClock
protocol
members
advance
(advance clock time)
Advances the mock clock by the specified interval of time
.
Advancing the clock is a continuous action - the clock doesn’t just jump from now
to new-now = (+ (now clock) time)
. Rather, for each scheduled event within [now; new-now]
the clock is reset to the time of the event and the event function is executed.
For example, if you have a periodic function scheduled with
(every 1 #(swap! counter inc))
and advance the clock by 5, the counter will be incremented 6 times in total: once initially, as the initial delay is 0 and 5 times for every 1 ms step of the clock.
now
(now clock)
Returns the current time for the clock
in
(in interval f)
Schedules no-arg function f
to be invoked in interval
milliseconds. Returns a deferred representing the returned value of the function. If the returned deferred is completed before the interval has passed, the timeout function will be cancelled.
mock-clock
(mock-clock)
(mock-clock initial-time)
Creates a clock designed for testing scheduled behaviors. It can replace the default scheduler using with-clock
, and can be advanced to a particular time via advance
. By default, the initial time is 0
.
with-clock
macro
(with-clock clock & body)
Ensures that all calls to every
and in
are made through the specified clock, rather than the default one.