import { getLog } from './utils' ; import type { Runtime , EventListenersManagerParameters } from './types' ; export const bootstrapOne = async ( { applicationName , name , eventstoreAdapter , eventSubscriber , eventTypes , destination , upstream , ensureQueue , forceResume , } ) => { const log = getLog ( `bootstrapOne:${ name }` ) ; try { const errors = [ ] ; try { await ensureQueue ( name ) ; } catch ( err ) { errors . push ( err ) ; } try { log . debug ( `ensuring event subscriber` ) ; await eventstoreAdapter . ensureEventSubscriber ( { applicationName , eventSubscriber : name , status : null , destination , } ) ; } catch ( err ) { log . error ( err ) ; errors . push ( err ) ; } try { log . debug ( `subscribing` ) ; await eventSubscriber . subscribe ( { eventSubscriber : name , subscriptionOptions : { eventTypes } , } ) ; if ( upstream || forceResume ) { const sideEffectsKey = 'RESOLVE_SIDE_EFFECTS_START_TIMESTAMP' ; log . debug ( `requesting subscriber status` ) ; const [ status , sideEffectsValue ] = await Promise . all ( [ eventSubscriber . status ( { eventSubscriber : name } ) . then ( ( result ) => ( result != null ? result . status : result ) ) , eventSubscriber . getProperty ( { eventSubscriber : name , key : sideEffectsKey , } ) , ] ) ; log . debug ( `subscriber status [${ status }]` ) ; if ( sideEffectsValue == null || sideEffectsValue === '' ) { log . debug ( `setting side effects timestamp to date.now` ) ; await eventSubscriber . setProperty ( { eventSubscriber : name , key : sideEffectsKey , value : `${ Date . now ( ) }` , } ) ; } if ( status === 'deliver' ) { log . debug ( `resuming event subscriber` ) ; await eventSubscriber . resume ( { eventSubscriber : name } ) ; } } } catch ( err ) { log . error ( err ) ; errors . push ( err ) ; } if ( errors . length > 0 ) { const summaryError = new Error ( errors . map ( ( { message } ) => message ) . join ( '\n' ) ) ; summaryError . stack = errors . map ( ( { stack } ) => stack ) . join ( '\n' ) ; throw summaryError ; } } catch ( error ) { console . warn ( `
      Event subscriber "${ name }" can't resume subscription due to error "${ error . stack }"
    ` ) ; } } ;