Tarjima qoshish uchun havola boyicha o'tib Pull Request oching (havolaga o'tish).
Standart til uchun tarkibni ko'rsatadi.
import { guard } from "effector";
Since effector 23.0.0.
The core team recommends using sample instead of guard
.
Method for conditional event routing.
It provides a way to control one dataflow with the help of another: when the condition and the data are in different places, we can use guard
with stores as filters to trigger events when condition state is true, thereby modulate signals without mixing them.
Methods
guard({ clock?, source?, filter, target? })
Formulae
guard({ clock?, source?, filter, target? }): target
Either clock
or source
is required
When clock
is triggered, check filter
for truthy and call target
with data from source
if true
.
- If
clock
is not passed,guard
will be triggered on everysource
update - If
source
is not passed, calltarget
with data fromclock
- If
target
is not passed, create Event with type ofsource
and return it fromguard()
- If
filter
is Store, check it value for truthy - If
filter
isFunction
, call it with data fromsource
and check result for truthy
clock
in guard
is available since effector 21.8.0
guard({source, filter, target?})
Arguments
params
(Object): Configuration object
Returns
Event, which fires upon clock
trigger
Examples
Basic
import { createStore, createEffect, createEvent, guard } from "effector";
const clickRequest = createEvent();
const fetchRequest = createEffect((n) => new Promise((rs) => setTimeout(rs, 2500, n)));
const $clicks = createStore(0).on(clickRequest, (x) => x + 1);
const $requestsCount = createStore(0).on(fetchRequest, (x) => x + 1);
const $isIdle = fetchRequest.pending.map((pending) => !pending);
/*
1. on clickRequest
2. if $isIdle is true
3. take current $clicks value
4. and call fetchRequest with it
*/
guard({
clock: clickRequest /* 1 */,
filter: $isIdle /* 2 */,
source: $clicks /* 3 */,
target: fetchRequest /* 4 */,
});
See ui visualization
Function predicate
import { createEffect, createEvent, guard } from "effector";
const submitForm = createEvent();
const searchUser = createEffect();
guard({
source: submitForm,
filter: (user) => user.length > 0,
target: searchUser,
});
submitForm(""); // nothing happens
submitForm("alice"); // ~> searchUser('alice')
guard(source, {filter: booleanStore})
Arguments
source
(Store/Event/Effect): Source unit. Will trigger givenguard
on updatesfilter
(Store): Filter store
Examples
Store filter
import { createEvent, createStore, createApi, guard } from "effector";
const trigger = createEvent();
const $unlocked = createStore(true);
const { lock, unlock } = createApi($unlocked, {
lock: () => false,
unlock: () => true,
});
const target = guard(trigger, {
filter: $unlocked,
});
target.watch(console.log);
trigger("A");
lock();
trigger("B"); // nothing happens
unlock();
trigger("C");
guard(source, {filter: predicate})
Arguments
source
(Store/Event/Effect): Source unit. Will trigger givenguard
on updatesfilter
((payload) => Boolean): Predicate function, should be pure
Examples
Predicate function
import { createEvent, guard } from "effector";
const source = createEvent();
const target = guard(source, {
filter: (x) => x > 0,
});
target.watch(() => {
console.log("target called");
});
source(0);
// nothing happens
source(1);
// target called
Ingliz tilidagi hujjatlar eng dolzarb hisoblanadi, chunki u effector guruhi tomonidan yozilgan va yangilanadi. Hujjatlarni boshqa tillarga tarjima qilish jamiyat tomonidan kuch va istaklar mavjud bo'lganda amalga oshiriladi.
Esda tutingki, tarjima qilingan maqolalar yangilanmasligi mumkin, shuning uchun eng aniq va dolzarb ma'lumot uchun hujjatlarning asl inglizcha versiyasidan foydalanishni tavsiya etamiz.