import { is, type Unit } from "effector";
Namespace for unit validators.
Methods
is.store(value)
Checks if given value is store
Returns
boolean
β Type-guard
Examples
import { is, createStore, createEvent, createEffect, createDomain } from "effector";
const $store = createStore(null);
const event = createEvent();
const fx = createEffect();
is.store($store);
// => true
is.store(event);
// => false
is.store(fx);
// => false
is.store(createDomain());
// => false
is.store(fx.pending);
// => true
is.store(fx.done);
// => false
is.store($store.updates);
// => false
is.store(null);
// => false
is.event(value)
Checks if given value is event
Returns
boolean
β Type-guard
Examples
import { is, createStore, createEvent, createEffect, createDomain } from "effector";
const $store = createStore(null);
const event = createEvent();
const fx = createEffect();
is.event($store);
// => false
is.event(event);
// => true
is.event(fx);
// => false
is.event(createDomain());
// => false
is.event(fx.pending);
// => false
is.event(fx.done);
// => true
is.event($store.updates);
// => true
is.event(null);
// => false
is.effect(value)
Checks if given value is effect
Returns
boolean
β Type-guard
Examples
import { is, createStore, createEvent, createEffect, createDomain } from "effector";
const $store = createStore(null);
const event = createEvent();
const fx = createEffect();
is.effect($store);
// => false
is.effect(event);
// => false
is.effect(fx);
// => true
is.effect(createDomain());
// => false
is.effect(null);
// => false
is.targetable
Checks if given value can be used in operators target (or be called as a function in case of events)
Returns
boolean
β Type-guard
Examples
import { is, createStore, createEvent, createEffect } from "effector";
const $store = createStore(null);
const $mapped = $store.map((x) => x);
const event = createEvent();
const mappedEvent = event.map((x) => x);
const fx = createEffect();
is.targetable($store);
// => true
is.targetable($mapped);
// => false
is.targetable(event);
// => true
is.targetable(mappedEvent);
// => false
is.targetable(fx);
// => true
is.domain(value)
Checks if given value is domain
Returns
boolean
β Type-guard
Examples
import { is, createStore, createEvent, createEffect, createDomain } from "effector";
const $store = createStore(null);
const event = createEvent();
const fx = createEffect();
is.domain($store);
// => false
is.domain(event);
// => false
is.domain(fx);
// => false
is.domain(createDomain());
// => true
is.domain(null);
// => false
is.scope(value)
Checks if given value is scope since effector 22.0.0.
Returns
boolean
β Type-guard
Examples
import { fork } from "effector";
const $store = createStore(null);
const event = createEvent();
const fx = createEffect();
const scope = fork();
is.scope(scope);
// => true
is.scope($store);
// => false
is.scope(event);
// => false
is.scope(fx);
// => false
is.scope(createDomain());
// => false
is.scope(null);
// => false
is.unit(value)
Checks if given value is Unit: Store, Event, Effect, Domain or Scope
Returns
boolean
β Type-guard
Examples
import { is, createStore, createEvent, createEffect, createDomain, fork } from "effector";
const $store = createStore(null);
const event = createEvent();
const fx = createEffect();
const scope = fork();
is.unit(scope);
// => true
is.unit($store);
// => true
is.unit(event);
// => true
is.unit(fx);
// => true
is.unit(createDomain());
// => true
is.unit(fx.pending);
// => true
is.unit(fx.done);
// => true
is.unit($store.updates);
// => true
is.unit(null);
// => false
is.attached(value)
Checks if given value is effect created via attach method. If passed not an effect, returns false
.
Returns
boolean
β Type-guard
Usage
Sometimes you need to add an error log on effects failures, but only on effects that have been βlocalizedβ via attach
.
If you leave onCreateEffect
as it is, without checks, the error log will be duplicated, because it will happen on the parent and the child effect.
import { createDomain, attach, is } from "effector";
const logFailuresDomain = createDomain();
logFailuresDomain.onCreateEffect((effect) => {
if (is.attached(effect)) {
effect.fail.watch(({ params, error }) => {
console.warn(`Effect "${effect.compositeName.fullName}" failed`, params, error);
});
}
});
const baseRequestFx = logFailuresDomain.createEffect((path) => {
throw new Error(`path ${path}`);
});
const loadDataFx = attach({
mapParams: () => "/data",
effect: baseRequestFx,
});
const loadListFx = attach({
mapParams: () => "/list",
effect: baseRequestFx,
});
loadDataFx();
loadListFx();
Examples
import { is, createStore, createEvent, createEffect, createDomain, attach } from "effector";
const $store = createStore(null);
const event = createEvent();
const fx = createEffect();
const childFx = attach({
effect: fx,
});
is.attached(childFx);
// => true
is.attached(fx);
// => false
is.attached($store);
// => false
is.attached(event);
// => false
is.attached(createDomain());
// => false
is.attached(null);
// => false