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
$timewithDate.now() - When
clockis triggered, callDate.now()to update$timewith results
Arguments
clock: Event<any> | Effect<any, any, any> | Store<any>— The unit triggers time reading and updates$timestore
— 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();// => 1660293358952time({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
$timewithinitial, if not present callgetNow, if not present callDate.now() - When
clockis triggered, callgetNowto update$timewith results, if not present useDate.now()
Arguments
clock: Event<any> | Effect<any, any, any> | Store<any>— The unit triggers time reading and updates$timestoregetNow: () => Time— Optional. A custom function to read time whenclocktriggers. Must be synchronous.initial: Time— Optional. Initial state for$timestore. If not passedgetNowis 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