delay
import { delay } from 'patronum';// orimport { delay } from 'patronum/delay';delay(source, timeout: number)
Motivation
Method creates a new event that will be triggered after some time. It is useful for implementing timeouts, animations, or scheduling delayed operations.
Formulae
target = delay(source, timeout);- When
sourceis triggered, wait fortimeout, then triggertargetwith payload of thesource
Arguments
source(Event<T>|Store<T>|Effect<T>)— Source unit, data from this unit used to triggertargetwith.timeout(number)— time to wait before triggerevent
Returns
target(Event<T>)— New event which will receivesourcepayload aftertimeoutdelay
Example
import { createEvent } from 'effector';import { delay } from 'patronum/delay';
const trigger = createEvent<string>(); // createStore or createEffectconst delayed = delay(trigger, 300);
delayed.watch((payload) => console.info('triggered', payload));
trigger('hello');// after 300ms// => triggered hellodelay({ source, timeout: number, target })
Motivation
This overload is useful when you already have a target unit that needs to be triggered with a delay.
Formulae
target = delay({ source, timeout: number, target });- When
sourceis triggered, wait fortimeout, then triggertargetwith payload of thesource
Arguments
source(Event<T>|Store<T>|Effect<T>)— Source unit, data from this unit used to triggertargetwith.timeout(number)— time to wait before triggereventtarget(Unit<T>|Array<Unit<T>>)— Optional. Target unit or array of units that will be called after delay.
Returns
target(Unit<T>|Array<Unit<T>>)— Target unit or units that were passed todelay
Example
import { createEvent } from 'effector';import { delay } from 'patronum/delay';
const trigger = createEvent<string>(); // createStore or createEffectconst delayed = createEvent<string>();delay({ source: trigger, timeout: 300, target: delayed,});
delayed.watch((payload) => console.info('triggered', payload));
trigger('hello');// after 300ms// => triggered hellodelay(source, timeout: Function)
Motivation
This overload allows to calculate timeout from payload of source.
It is useful when you know that calculations requires more time if you have more data for payload.
Formulae
target = delay(source, timeout);- When
sourceis triggered, calltimeoutwith payload to get the timeout for delay, then triggertargetwith payload of thesource
Arguments
source(Event<T>|Store<T>|Effect<T>)— Source unit, data from this unit used to triggertargetwith.timeout((payload: T) => number)— Calculate delay for eachsourcecall. Receives the payload ofsourceas argument. Should returnnumber— delay in milliseconds.
Returns
target(Event<T>)— New event which will receivesourcepayload aftertimeoutdelay
Example
import { createEvent, createStore } from 'effector';import { delay } from 'patronum/delay';
const update = createEvent<string>();const $data = createStore('');const logDelayed = delay($data, (string) => string.length * 100);
logDelayed.watch((data) => console.log('log', data));
update('Hello');// after 500ms// => log Hello
update('!');// after 100ms// => log !delay({ source, timeout: Function, target })
Motivation
This overload allows to calculate timeout from payload of source.
It is useful when you know that calculations requires more time if you have more data for payload.
Formulae
target = delay({ source, timeout: Function, target });- When
sourceis triggered, calltimeoutwith payload to get the timeout for delay, then triggertargetwith payload of thesource
Arguments
source(Event<T>|Store<T>|Effect<T>)— Source unit, data from this unit used to triggertargetwith.timeout((payload: T) => number)— Calculate delay for eachsourcecall. Receives the payload ofsourceas argument. Should returnnumber— delay in milliseconds.target(Unit<T>|Array<Unit<T>>)— Optional. Target unit or array of units that will be called after delay.
Returns
target(Unit<T>|Array<Unit<T>>)— Target unit or units that were passed todelay
Example
import { createEvent, createStore } from 'effector';import { delay } from 'patronum/delay';
const update = createEvent<string>();const logDelayed = createEvent<string>();const $data = createStore('');
delay({ source: $data, timeout: (string) => string.length * 100, target: logDelayed,});
logDelayed.watch((data) => console.log('log', data));
update('Hello');// after 500ms// => log Hello
update('!');// after 100ms// => log !delay(source, timeout: Store<T>)
Motivation
This overload allows you to read timeout from another store. It is useful when you write music editor and need dynamic delay for your events.
Formulae
target = delay(source, timeout);- When
sourceis triggered, read timeout fromtimeoutstore, then triggertargetwith payload of thesource
Arguments
source(Event<T>|Store<T>|Effect<T>)— Source unit, data from this unit used to triggertargetwith.timeout(Store<number>)— Store with number — delay in milliseconds.
Returns
target(Event<T>)— New event which will receivesourcepayload aftertimeoutdelay
Example
import { createEvent, createStore } from 'effector';import { delay } from 'patronum/delay';
const update = createEvent<string>();const $timeout = createStore(500);
const logDelayed = delay(update, $timeout);
logDelayed.watch((data) => console.log('log', data));
update('Hello');// after 500ms// => log Hellodelay({ source, timeout: Store<T>, target })
Motivation
This overload allows you to read timeout from another store. It is useful when you write music editor and need dynamic delay for your events.
Formulae
target = delay({ source, timeout: $store, target });- When
sourceis triggered, read timeout fromtimeoutstore, then triggertargetwith payload of thesource
Arguments
source(Event<T>|Store<T>|Effect<T>)— Source unit, data from this unit used to triggertargetwith.timeout(Store<number>)— Store with number — delay in milliseconds.target(Unit<T>|Array<Unit<T>>)— Optional. Target unit or array of units that will be called after delay.
Returns
target(Unit<T>|Array<Unit<T>>)— Target unit or units that were passed todelay
Example
import { createEvent, createStore } from 'effector';import { delay } from 'patronum/delay';
const update = createEvent<string>();const $timeout = createStore(500);
delay({ source: update, timeout: $timeout, target: logDelayed,});
logDelayed.watch((data) => console.log('log', data));
update('Hello');// after 500ms// => log Hello