splitMap
import { splitMap } from 'patronum';// orimport { splitMap } from 'patronum/split-map';
shape = splitMap({ source, cases })
Motivation
The method is a combination of split
and map
.
It is useful when you want add some kind of pattern matching.
Formulae
shape = splitMap({ source, cases });
- On each
source
trigger, call each function incases
object one after another until function returns non undefined, and call event inshape
with the same name as function incases
object. - If no function returned value event
__
inshape
should be triggered
Arguments
source
(Event
|Store
|Effect
) — Source unit, data from this unit passed to each function incases
object and__
event inshape
as iscases
({ [key: string]: (payload: T) => any | void }
) — Object of functions. Function receives one argument is a payload fromsource
, should return any value orundefined
Returns
shape
({ [key: string]: Event<any>; __: Event<T> }
) — Object of events, with the same structure ascases
, but with the default event__
, that triggered when each other function returnsundefined
Examples
Extract passed fields from optional object
import { createEvent } from 'effector';import { splitMap } from 'patronum/split-map';
const event = createEvent<object>();
const shape = splitMap({ source: event, cases: { getType: (input) => input.type, getDemo: (input) => input.demo, },});
shape.getType.watch((type) => console.log('TYPE', type));shape.getDemo.watch((demo) => console.log('DEMO', demo));shape.__.watch((other) => console.log('OTHER', other));
event({ type: 'demo' });// => TYPE demo
event({ demo: 5 });// => DEMO 5
event({});// => OTHER {}
Split WebSocket events to effector events
import { createEvent } from 'effector';import { splitMap } from 'patronum/split-map';
type WSEvent = | { type: 'init'; key: string } | { type: 'increment'; count: number; name: string } | { type: 'reset'; name: string };
export const websocketEventReceived = createEvent<WSEvent>();
const { init, increment, reset, __ } = splitMap({ source: websocketEventReceived, cases: { init: (event) => { if (event.type === 'init') return event.key; }, increment: ({ type, ...payload }) => { if (type === 'increment') return payload; }, reset: ({ type, name }) => { if (type === 'reset') return name; }, },});
__.watch((payload) => { console.warn('Unknown type:', payload.type);});
increment.watch((payload) => { console.info('should be incremented', payload.count, payload.name);});
websocketEventReceived({ type: 'increment', name: 'demo', count: 5 });// => should be incremented 5 'demo'
websocketEventReceived({ type: 'bang', random: 'unknown' });// => Unknown type: 'bang'