import init from '@utils/instance'; interface ActionProps { dispatch: any; history: any; } type TimerType = NodeJS.Timeout | null; const actionInstance = init(); class Action { dispatch: any; history: any; constructor({dispatch, history}: ActionProps) { const instance = actionInstance(); if (instance) return instance; this.dispatch = dispatch; this.history = history; actionInstance(this); } static emit = (type: string, payload?: any) => { let timer: TimerType = setTimeout(() => { clearTimeout(timer!); timer = null; actionInstance().dispatch({type, payload}); }); }; static success = (type: string, payload: any) => { let timer: TimerType = setTimeout(() => { clearTimeout(timer!); timer = null; actionInstance().dispatch({type: `${type}_SUCCESS`, ...payload}); }); }; static clean = (type: string) => { let timer: TimerType = setTimeout(() => { clearTimeout(timer!); timer = null; actionInstance().dispatch({type: `${type}_FAIL`, payload: {result: null}}); }); }; static push = (location: any) => { actionInstance().history.push(location); }; static replace = (location: any) => { actionInstance().history.replace(location); }; } export default Action;