import { isFullString , isString } from 'is-what' ; import { PluginStreamAction , StreamResponse , DoOnStream , PluginStreamActionPayload , } from '@magnetarjs/core' ; import { SimpleStoreModuleConfig , SimpleStoreOptions } from '../CreatePlugin' ; import { insertActionFactory } from './insert' ; import { deleteActionFactory } from './delete' ; export function streamActionFactory ( data , simpleStoreOptions ) { return function ( { payload , collectionPath , docId , actionConfig , pluginModuleConfig , mustExecuteOnRead , } ) : StreamResponse | DoOnStream | Promise < StreamResponse | DoOnStream > { const doOnStream = { added : ( payload , meta ) => { if ( payload === undefined ) return ; const _docId = docId || `${ meta . id }` ; insertActionFactory ( data , simpleStoreOptions ) ( { payload , collectionPath , docId : _docId , actionConfig , pluginModuleConfig , } ) ; } , modified : ( payload , meta ) => { if ( payload === undefined ) return ; const _docId = docId || `${ meta . id }` ; insertActionFactory ( data , simpleStoreOptions ) ( { payload , collectionPath , docId : _docId , actionConfig , pluginModuleConfig , } ) ; } , removed : ( payload , meta ) => { if ( payload === undefined ) return ; const collectionPathDocIdToDelete = isFullString ( docId ) ? [ collectionPath , docId ] : isString ( payload ) ? [ collectionPath , payload ] : [ collectionPath , meta . id ] ; const [ _cPath , _dId ] = collectionPathDocIdToDelete ; deleteActionFactory ( data , simpleStoreOptions ) ( { payload : undefined , collectionPath : _cPath , docId : _dId , actionConfig , pluginModuleConfig , } ) ; } , } ; return doOnStream ; } ; }