UNPKG

mst-effect

Version:

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

30 lines (22 loc) 901 B
import { EFFECT_ACTION_IDENTITY } from '../const' export type EffectAction = { [EFFECT_ACTION_IDENTITY]: () => void } export type ValidEffectActions = EffectAction | EffectAction[] export function action<P extends any[]>(fn: (...params: P) => void, ...params: P): EffectAction { return { [EFFECT_ACTION_IDENTITY]: () => fn(...params) } } export const NOOP = action(() => {}) export function runActions(actions: ValidEffectActions): void { ;(Array.isArray(actions) ? actions : [actions]).forEach(runAction) } function runAction(action: any): void { if (isValidAction(action)) { action[EFFECT_ACTION_IDENTITY]() } else { console.warn(`[mst-effect]: ${String(action)} is not a valid EffectActions`) } } function isValidAction(action: any): action is EffectAction { return ( action && typeof action === 'object' && typeof action[EFFECT_ACTION_IDENTITY] === 'function' ) }