import type { Store as ReduxStore } from 'redux' ; import type { LoadingSettings } from '@dura/types' ; export async function dispatchLoading < S > ( reduxStore , type , loading , fn ) { const dispatchTrue = ( type ) => reduxStore . dispatch ( { type : 'DURA/CHANGE_LOADING' , payload : { k : type , v : { status : true , error : null , } , } , } ) ; const dispatchFalse = ( type ) => reduxStore . dispatch ( { type : 'DURA/CHANGE_LOADING' , payload : { k : type , v : { status : false , error : null , } , } , } ) ; const dispatchError = ( type , e ) => reduxStore . dispatch ( { type : 'DURA/CHANGE_LOADING' , payload : { k : type , v : { status : false , error : e , } , } , } ) ; const finallyTyoe = typeof loading === 'boolean' ? `${ type }` : `${ type }/customize/${ loading . customizeId }` ; try { dispatchTrue ( finallyTyoe ) ; await fn ( ) ; dispatchFalse ( finallyTyoe ) ; } catch ( error ) { dispatchError ( finallyTyoe , error ) ; } }