import { isString } from 'is-what' ; import { PluginStreamAction , StreamResponse , DoOnStream , PluginStreamActionPayload , } from '../../../../core/src' ; import { StorePluginModuleConfig , StorePluginOptions } from '../CreatePlugin' ; import { insertActionFactory } from './insert' ; import { deleteActionFactory } from './delete' ; import { throwIfEmulatedError } from '../../helpers' ; export function streamActionFactory ( data , storePluginOptions ) { return function ( { payload , collectionPath , docId , actionConfig , pluginModuleConfig , mustExecuteOnRead , } ) : StreamResponse | DoOnStream | Promise < StreamResponse | DoOnStream > { throwIfEmulatedError ( payload , storePluginOptions ) ; const doOnStream = { added : ( payload , meta ) => { if ( payload === undefined ) return ; insertActionFactory ( data , storePluginOptions ) ( { payload , collectionPath , docId , actionConfig , pluginModuleConfig , } ) ; } , modified : ( payload , meta ) => { if ( payload === undefined ) return ; insertActionFactory ( data , storePluginOptions ) ( { payload , collectionPath , docId , actionConfig , pluginModuleConfig , } ) ; } , removed : ( payload , meta ) => { const collectionPathDocIdToDelete = docId ? [ collectionPath , docId ] : isString ( payload ) ? [ collectionPath , payload ] : [ collectionPath , meta . id ] ; const [ _cPath , _dId ] = collectionPathDocIdToDelete ; if ( payload === undefined ) return ; deleteActionFactory ( data , storePluginOptions ) ( { payload : undefined , collectionPath : _cPath , docId : _dId , actionConfig , pluginModuleConfig , } ) ; } , } ; return doOnStream ; } ; }