import { Breadcrumb , CaptureContext , Event , EventHint , EventProcessor , Scope as ScopeInterface , ScopeContext , Span , Transaction } from '../types' ; import { getGlobalObject , isPlainObject , isThenable , SyncPromise , timestampWithMs } from '../utils' ; /**
 * Holds additional event information.  Scope . applyToEvent will be
 * called by the client before an event will be sent.
  export class Scope implements ScopeInterface { /** Flag if notifiying is happening. */ protected _notifyingListeners : boolean = false ; /** Callback for client to receive scope changes. */ protected _scopeListeners : Array < ( scope : Scope ) => void > = [ ] ; /** Callback list that will be called after  applyToEvent .  protected _eventProcessors : EventProcessor [ ] = [ ] ; /** Array of breadcrumbs. */ protected _breadcrumbs : Breadcrumb [ ] = [ ] ; /** Extra */ protected _extra : { [ key ] : any ; } = { } ; /** Contexts */ protected _contexts : { [ key ] : any ; } = { } ; /** Transaction Name */ protected _transactionName ? : string ; /** Span */ protected _span ? : Span ; param scope public static clone ( scope ? ) { const newScope = new Scope ( ) ; if ( scope ) { newScope . _breadcrumbs = [ ... scope . _breadcrumbs ] ; newScope . _extra = { ... scope . _extra } ; newScope . _contexts = { ... scope . _contexts } ; newScope . _span = scope . _span ; newScope . _transactionName = scope . _transactionName ; newScope . _eventProcessors = [ ... scope . _eventProcessors ] ; } return newScope ; } hidden public addScopeListener ( callback ) { this . _scopeListeners . push ( callback ) ; } inheritDoc public addEventProcessor ( callback ) { this . _eventProcessors . push ( callback ) ; return this ; } inheritDoc public setTransactionName ( name ? ) { this . _transactionName = name ; this . _notifyScopeListeners ( ) ; return this ; } deprecated in favor of  this . setTransactionName public setTransaction ( name ? ) { return this . setTransactionName ( name ) ; } inheritDoc public setContext ( key , context ) { this . _contexts = { ... this . _contexts , [ key ] : context } ; this . _notifyScopeListeners ( ) ; return this ; } inheritDoc public setSpan ( span ? ) { this . _span = span ; this . _notifyScopeListeners ( ) ; return this ; } inheritDoc public getSpan ( ) { return this . _span ; } inheritDoc public getTransaction ( ) { const span = this . getSpan ( ) as Span & { spanRecorder : { spans : Span [ ] ; } ; } ; if ( span && span . spanRecorder && span . spanRecorder . spans [ 0 ] ) { return span . spanRecorder . spans [ 0 ] as Transaction ; } return undefined ; } inheritDoc public getBreadcrumb ( ) { return this . _breadcrumbs ; } inheritDoc public update ( captureContext ? ) { if ( ! captureContext ) { return this ; } if ( typeof captureContext === 'function' ) { const updatedScope = ( captureContext as < T > ( scope : T ) => T ) ( this ) ; return updatedScope instanceof Scope ? updatedScope : this ; } if ( captureContext instanceof Scope ) { this . _extra = { ... this . _extra , ... captureContext . _extra } ; this . _contexts = { ... this . _contexts , ... captureContext . _contexts } ; } else if ( isPlainObject ( captureContext ) ) { captureContext = captureContext as ScopeContext ; this . _extra = { ... this . _extra , ... captureContext . extra } ; this . _contexts = { ... this . _contexts , ... captureContext . contexts } ; } return this ; } inheritDoc public clear ( ) { this . _breadcrumbs = [ ] ; this . _extra = { } ; this . _contexts = { } ; this . _transactionName = undefined ; this . _span = undefined ; this . _notifyScopeListeners ( ) ; return this ; } inheritDoc public addBreadcrumb ( breadcrumb , maxBreadcrumbs ? ) { const mergedBreadcrumb = { timestamp : timestampWithMs ( ) , ... breadcrumb , } ; this . _breadcrumbs = maxBreadcrumbs !== undefined && maxBreadcrumbs >= 0 ? [ ... this . _breadcrumbs , mergedBreadcrumb ] . slice ( - maxBreadcrumbs ) : [ ... this . _breadcrumbs , mergedBreadcrumb ] ; this . _notifyScopeListeners ( ) ; return this ; } inheritDoc public clearBreadcrumbs ( ) { this . _breadcrumbs = [ ] ; this . _notifyScopeListeners ( ) ; return this ; } param event param hint hidden public applyToEvent ( event , hint ? ) { if ( this . _contexts && Object . keys ( this . _contexts ) . length ) { event . contexts = { ... this . _contexts , ... event . contexts } ; } return this . _notifyEventProcessors ( [ ... getGlobalEventProcessors ( ) , ... this . _eventProcessors ] , event , hint ) ; } /**
     * This will be called after  applyToEvent is finished.
      protected _notifyEventProcessors ( processors , event , hint ? , index = 0 ) { return new SyncPromise < Event | null > ( ( resolve , reject ) => { const processor = processors [ index ] ; if ( event === null || typeof processor !== 'function' ) { resolve ( event ) ; } else { const result = processor ( { ... event } , hint ) as Event | null ; if ( isThenable ( result ) ) { ( result as PromiseLike < Event | null > ) . then ( final => this . _notifyEventProcessors ( processors , final , hint , index + 1 ) . then ( resolve ) ) . then ( null , reject ) ; } else { this . _notifyEventProcessors ( processors , result , hint , index + 1 ) . then ( resolve ) . then ( null , reject ) ; } } } ) ; } /**
     * This will be called on every set call.
     */ protected _notifyScopeListeners ( ) { if ( ! this . _notifyingListeners ) { this . _notifyingListeners = true ; setTimeout ( ( ) => { this . _scopeListeners . forEach ( callback => { callback ( this ) ; } ) ; this . _notifyingListeners = false ; } ) ; } } } /**
 * Retruns the global event processors.
 */ function getGlobalEventProcessors ( ) { const global = getGlobalObject < Window > ( ) ; global . __BEIDOU__ = global . __BEIDOU__ || { } ; global . __BEIDOU__ . globalEventProcessors = global . __BEIDOU__ . globalEventProcessors || [ ] ; return global . __BEIDOU__ . globalEventProcessors ; } param callback export function addGlobalEventProcessor ( callback ) { getGlobalEventProcessors ( ) . push ( callback ) ; }