UNPKG

redux-providers

Version:

Minimalist dependency injection system for redux. Create providers to be injected and used in redux reducers.

28 lines (23 loc) 764 B
import { AnyAction, Dispatch } from 'redux'; export enum AsyncEventType { onStart = 'onStart', onFinished = 'onFinished', onSuccess = 'onSuccess', onError = 'onError', } export interface AsyncAction<PayloadType = any> extends AnyAction { eventType: AsyncEventType; payload?: PayloadType; error?: any; } export type AsyncActionFn = <PayloadType = any>(dispatch: Dispatch<AsyncAction<PayloadType>>) => void; export type AsyncStatusHandler<T> = { [K in AsyncEventType]?: (state: T) => T }; export function handleAsyncAction<S>(state: S, action: AsyncAction, handler: AsyncStatusHandler<S>): S { let fn = handler[action.eventType]; if (!fn) { fn = (s) => { return s; }; } return fn(state); }