interval
import { interval } from 'patronum';// orimport { interval } from 'patronum/interval';interval({ timeout, start, stop })
Motivation
Creates a dynamic interval with events to start, stop interval, and handle tick.
Formulae
const { tick, isRunning } = interval({ timeout, start, stop, leading, trailing,});- When
startis triggered,tickwill betriggeredeachtimeoutms - Till
startis triggered, untilstopis triggered,isRunningwill betrue - After
stopis triggeredtickwon’t be triggered, untilstartis run again
Arguments
timeout(number | Store<number>)- timeout for eachtickrunstart(Event<any>)- start triggeringtick. Double trigger withoutstopcall, do nothing.stop(Event<any>)- stop triggeringtick. Double trigger withoutstartcall between them, do nothing.leading(boolean)- Allows runningtickwhenstartis triggered.trailing(boolean)- Allows runningtickwhenstopis triggered, it will be final call oftick.
Returns
- An object of event
tickand storeisRunningtick(Event<void)- the event is run on eachtimeoutms afterstarttrigger untilstoptriggerisRunning(Store<boolean>)- the store containsfalseuntilstartis triggered, next untilstoptriggered the store will betrue.
Example
import { createStore, createEvent } from 'effector';import { interval } from 'patronum';
const startCounter = createEvent();const stopCounter = createEvent();const $counter = createStore(0);
const { tick } = interval({ timeout: 500, start: startCounter, stop: stopCounter,});
$counter.on(tick, (number) => number + 1);$counter.watch((value) => console.log('COUNTER', value));
startCounter();
setTimeout(() => stopCounter(), 5000);@@trigger protocol
interval supports @@trigger protocol. It means that you can use interval whatever you can use trigger with, just do not pass start and stop options.
import { interval } from 'patronum';
somethingThatRequiresTrigger({ trigger: interval({ timeout: 1000 }),});For example, you can use interval to refresh data in the Query from the library Farfetched using @@trigger protocol.
import { keepFresh } from '@farfetched/core';import { interval } from 'patronum';
keepFresh(someQuery, { // 👇 Query will be refreshed each 1000 ms triggers: [interval({ timeout: 1000 })],});