import { ActionConfig , ActionName } from '../types/actions' ; import { SyncBatch } from '../types/plugins' ; import { EventNameFnsMap } from '../types/events' ; import { PluginModuleConfig , PluginFetchAction , PluginWriteAction , PluginDeleteAction , PluginDeletePropAction , PluginInsertAction , FetchResponse , } from '../types/plugins' ; import { OnAddedFn } from '../types/modifyReadResponse' ; returns export async function handleAction ( args ) { const { collectionPath , docId , modulePath , pluginModuleConfig , pluginAction , payload , actionConfig = { } , eventNameFnsMap : on , onError , actionName , stopExecutionAfterAction , storeName , } = args ; let abortExecution = false ; const abort = ( ) : void => { abortExecution = true ; } ; for ( const fn of on . before ) { await fn ( { payload , actionName , storeName , abort , collectionPath , docId , path : modulePath , pluginModuleConfig } ) ; } if ( abortExecution ) { stopExecutionAfterAction ( ) ; return ; } let result ; try { result = await pluginAction ( { payload , actionConfig , collectionPath , docId , pluginModuleConfig , } as any ) ; } catch ( error ) { for ( const fn of on . error ) { await fn ( { payload , actionName , storeName , abort , error , collectionPath , docId , path : modulePath , pluginModuleConfig } ) ; } if ( abortExecution || onError === 'stop' ) { stopExecutionAfterAction ( ) ; throw error ; } if ( onError === 'revert' ) { stopExecutionAfterAction ( 'revert' ) ; return error ; } } for ( const fn of on . success ) { await fn ( { payload , result , actionName , storeName , abort , collectionPath , docId , path : modulePath , pluginModuleConfig } ) ; } if ( abortExecution ) { stopExecutionAfterAction ( ) ; return result ; } return result ; }