time
import { time } from 'patronum';// orimport { time } from 'patronum/time';
time(clock)
Motivation
The method allow to read current time and write it to store
Formulae
$time = time(clock);
- Initialize
$time
withDate.now()
- When
clock
is triggered, callDate.now()
to update$time
with results
Arguments
clock: Event<any> | Effect<any, any, any> | Store<any>
— The unit triggers time reading and updates$time
store
— Time
is a generic type argument used for overriding time reader function. By default, it is number
Returns
$time: Store<Time>
— Store contains unix timestamp snapshot, updates when clock
triggers.
Initialized with Date.now()
Example
import { time } from 'patronum';
const tick = createEvent();const $time = time(tick);
$time.watch((time) => console.log('time', time));// => time 1660293358847
tick();// => time 1660293358848await new Promise((res) => setTimeout(res, 100));tick();// => 1660293358952
time({clock, getNow, initial})
Motivation
The method allow to read current time and write it to store. Object form allows to use additional parameters: getNow
and initial
Formulae
$time = time({ clock, getNow, initial });
- Initialize
$time
withinitial
, if not present callgetNow
, if not present callDate.now()
- When
clock
is triggered, callgetNow
to update$time
with results, if not present useDate.now()
Arguments
clock: Event<any> | Effect<any, any, any> | Store<any>
— The unit triggers time reading and updates$time
storegetNow: () => Time
— Optional. A custom function to read time whenclock
triggers. Must be synchronous.initial: Time
— Optional. Initial state for$time
store. If not passedgetNow
is called.
— Time
is a generic type argument used for overriding time reader function. By default, it is number
Returns
$time: Store<Time>
— Store contains unix timestamp snapshot, updates when clock
triggers.
If getNow
is overridden, contains value this function returns.
By default, it is number
.
Initialized with initial
, by default, it is Date.now()
Example
import { time } from 'patronum';
const tick = createEvent();const $time = time({ clock: tick });
$time.watch((time) => console.log('time', time));// => time 1660293358847
tick();// => time 1660293358848await new Promise((res) => setTimeout(res, 100));tick();// => 1660293358952