UNPKG

mst-effect

Version:

Designed to be used with MobX-State-Tree to create asynchronous actions using RxJS.

21 lines (20 loc) 668 B
import { EFFECT_ACTION_IDENTITY } from '../const'; export function action(fn, ...params) { return { [EFFECT_ACTION_IDENTITY]: () => fn(...params) }; } export const NOOP = action(() => { }); export function runActions(actions) { ; (Array.isArray(actions) ? actions : [actions]).forEach(runAction); } function runAction(action) { if (isValidAction(action)) { action[EFFECT_ACTION_IDENTITY](); } else { console.warn(`[mst-effect]: ${String(action)} is not a valid EffectActions`); } } function isValidAction(action) { return (action && typeof action === 'object' && typeof action[EFFECT_ACTION_IDENTITY] === 'function'); }