import { O } from 'ts-toolbelt' ; import { ActionConfig } from '../types/actions' ; import { EventNameFnsMap } from '../types/events' ; import { PluginModuleConfig , PluginStreamAction , DoOnStream , StreamResponse , } from '../types/plugins' ; /**
 * handleStream is responsible for executing (1) on.before (2) the action provided by the store plugin (3) on.error / on.success
 */ export async function handleStream ( args ) { const { collectionPath , docId , pluginModuleConfig , pluginAction , payload , actionConfig = { } , eventNameFnsMap : on , actionName , storeName , mustExecuteOnRead , } = args ; const abort = ( ) => { } ; const path = [ collectionPath , docId ] . filter ( Boolean ) . join ( '/' ) ; for ( const fn of on . before ) { await fn ( { payload , actionName , storeName , abort , collectionPath , docId , path , pluginModuleConfig } ) ; } let result ; try { const pluginStreamAction = pluginAction ; result = await pluginStreamAction ( { payload , actionConfig , collectionPath , docId , pluginModuleConfig , mustExecuteOnRead , } ) ; } catch ( error ) { for ( const fn of on . error ) { await fn ( { payload , actionName , storeName , error , abort , collectionPath , docId , path , pluginModuleConfig } ) ; } throw error ; } for ( const fn of on . success ) { await fn ( { payload , result , actionName , storeName , abort , collectionPath , docId , path , pluginModuleConfig } ) ; } return result ; }