condition
import { condition } from 'patronum';// orimport { condition } from 'patronum/condition';
Motivation
Condition is very similar to sample
, but allows you to have else
branch along with simple if
matcher.
condition({ source: Unit, if: Store, then?: Unit, else?: Unit })
Formulae
result = condition({ source, if: $checker, then, else,});
- When
source
is triggered, check value of$checker
, if it equalstrue
, triggerthen
with value fromsource
, otherwise triggerelse
with value fromsource
result
is the same unit assource
allows to nestcondition
to anothercondition
orsample
Arguments
source
(Unit<T>)
— Data from this unit will be passed tothen
orelse
if
(Store<boolean>)
— Updates of this store will not triggerthen
andelse
then
(Unit<T>)
— This unit will be triggered with data fromsource
if$checker
containstrue
. Required ifelse
is not providedelse
(Unit<T>)
— This unit will be triggered with data fromsource
if$checker
containsfalse
. Required ifthen
is not provided
Returns
(Unit<T>)
— The same unit type that passed tosource
Example
const change = createEvent();const $source = createStore('data').on(change, (_, payload) => payload);
const toggle = createEvent();const $isEnabled = createStore(false).on(toggle, (is) => !is);
const enabled = createEvent();const disabled = createEvent();
condition({ source: $source, if: $isEnabled, then: enabled, else: disabled,});enabled.watch((payload) => console.log('enabled -', payload));disabled.watch((payload) => console.log('disabled -', payload));
change('newdata');// => disabled - newdata
toggle();change('data');// => enabled - data
condition({ source: Unit<T>, if: T, then?: Unit, else?: Unit })
Formulae
result = condition({ source, if: value, then, else,});
- When
source
is triggered, comparevalue
literal withsource
payload, if it equals triggerthen
with value fromsource
, otherwise triggerelse
with value fromsource
result
is the same unit assource
allows to nestcondition
to anothercondition
orsample
Arguments
source
(Unit<T>)
— Data from this unit will be passed tothen
orelse
if
(T)
— Just value to compare withsource
payload. Note: objects will be compared by referencethen
(Unit<T>)
— This unit will be triggered with data fromsource
if$checker
containstrue
. Required ifelse
is not providedelse
(Unit<T>)
— This unit will be triggered with data fromsource
if$checker
containsfalse
. Required ifthen
is not provided
Returns
(Unit<T>)
— The same unit type that passed tosource
Example
const increment = createEvent();const $source = createStore(0).on(increment, (state) => state + 1);
const log = createEvent();const run = createEffect().use((data) => { console.info('FAKE RUN EFFECT', data);});
condition({ source: $source, if: 4, then: run, else: log,});
log.watch((payload) => console.log('LOG ABOUT IT', payload));
increment(); // => LOG ABOUT IT 1increment(); // => LOG ABOUT IT 2increment(); // => LOG ABOUT IT 3increment(); // => FAKE RUN EFFECT 4increment(); // => LOG ABOUT IT 5
condition({ source: Unit<T>, if: Function, then?: Unit, else?: Unit })
Formulae
result = condition({ source, if: (payload) => boolean, then, else,});
- When
source
is triggered, callif
withsource
payload, if it returnstrue
triggerthen
with value fromsource
, otherwise triggerelse
with value fromsource
result
is the same unit assource
allows to nestcondition
to anothercondition
orsample
Arguments
source
(Unit<T>)
— Data from this unit will be passed tothen
orelse
if
((payload: T) => boolean)
— Function comparator. It should return booleanthen
(Unit<T>)
— This unit will be triggered with data fromsource
if$checker
containstrue
. Required ifelse
is not providedelse
(Unit<T>)
— This unit will be triggered with data fromsource
if$checker
containsfalse
. Required ifthen
is not provided
Returns
(Unit<T>)
— The same unit type that passed tosource
Example
const change = createEvent();const $source = createStore('data').on(change, (_, payload) => payload);const target = createEvent();const another = createEvent();
condition({ source: $source, if: (source) => source.length > 3, then: target, else: another,});target.watch((payload) => console.log('triggered', payload));another.watch((payload) => console.log('condition else:', payload));
change('newdata');// => triggered newdata
change('old');// => condition else: old
Examples
Source is event
const inputChanged = createEvent();const $value = createStore('');const $error = createStore(false);
const setError = createEvent();const setValue = createEvent();
$value.on(setValue, (_, value) => value);$error.on(setError, () => true).on(setValue, () => false);
condition({ source: inputChanged, if: isValid, then: setValue, else: setError,});
function isValid(value) { return value.trim().length > 0;}
Condition can be nested
const $value = createStore('hello@world');const updateEmail = createEvent<string>();
condition({ source: $value, if: (length) => length > 0, then: condition({ if: (string) => string.includes('@'), then: updateEmail, }),});