time
since
patronum 1.7.0
import { time } from 'patronum';
// or
import { time } from 'patronum/time';
Motivation
The method allow to read current time just as an effector method.
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
— A custom function to read time whenclock
triggers. Must be synchronous.initial?: Time
— Initial state for$time
store. If not passedgetNow
is called.
— Time
is a generic type argument used for overriding time reader function. By default, is is number
Returns
$time: Store<Time>
— Store contains unix timestamp snapshot, updates when clock
triggers.
If getNow
is overrided, 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 1660293358848
await new Promise((res) => setTimeout(res, 100));
tick();
// => 1660293358952