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
sourceis triggered, check value of$checker, if it equalstrue, triggerthenwith value fromsource, otherwise triggerelsewith value fromsource resultis the same unit assourceallows to nestconditionto anotherconditionorsample
Arguments
source(Unit<T>)— Data from this unit will be passed tothenorelseif(Store<boolean>)— Updates of this store will not triggerthenandelsethen(Unit<T>)— This unit will be triggered with data fromsourceif$checkercontainstrue. Required ifelseis not providedelse(Unit<T>)— This unit will be triggered with data fromsourceif$checkercontainsfalse. Required ifthenis 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 - datacondition({ source: Unit<T>, if: T, then?: Unit, else?: Unit })
Formulae
result = condition({ source, if: value, then, else,});- When
sourceis triggered, comparevalueliteral withsourcepayload, if it equals triggerthenwith value fromsource, otherwise triggerelsewith value fromsource resultis the same unit assourceallows to nestconditionto anotherconditionorsample
Arguments
source(Unit<T>)— Data from this unit will be passed tothenorelseif(T)— Just value to compare withsourcepayload. Note: objects will be compared by referencethen(Unit<T>)— This unit will be triggered with data fromsourceif$checkercontainstrue. Required ifelseis not providedelse(Unit<T>)— This unit will be triggered with data fromsourceif$checkercontainsfalse. Required ifthenis 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 5condition({ source: Unit<T>, if: Function, then?: Unit, else?: Unit })
Formulae
result = condition({ source, if: (payload) => boolean, then, else,});- When
sourceis triggered, callifwithsourcepayload, if it returnstruetriggerthenwith value fromsource, otherwise triggerelsewith value fromsource resultis the same unit assourceallows to nestconditionto anotherconditionorsample
Arguments
source(Unit<T>)— Data from this unit will be passed tothenorelseif((payload: T) => boolean)— Function comparator. It should return booleanthen(Unit<T>)— This unit will be triggered with data fromsourceif$checkercontainstrue. Required ifelseis not providedelse(Unit<T>)— This unit will be triggered with data fromsourceif$checkercontainsfalse. Required ifthenis 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: oldExamples
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, }),});