import { addGlobalEventProcessor , getCurrentHub } from '@sentry/hub' ; import { Integration , Options } from '@sentry/types' ; import { addNonEnumerableProperty , logger } from '@sentry/utils' ; export const installedIntegrations = [ ] ; /** Map of integrations assigned to a client */ export type IntegrationIndex = { [ key ] : Integration ; } & { initialized ? : boolean ; } ; private function filterDuplicates ( integrations ) { return integrations . reduce ( ( acc , integrations ) => { if ( acc . every ( accIntegration => integrations . name !== accIntegration . name ) ) { acc . push ( integrations ) ; } return acc ; } , [ ] as Integration [ ] ) ; } /** Gets integration to install */ export function getIntegrationsToSetup ( options ) { const defaultIntegrations = ( options . defaultIntegrations && [ ... options . defaultIntegrations ] ) || [ ] ; const userIntegrations = options . integrations ; let integrations = [ ... filterDuplicates ( defaultIntegrations ) ] ; if ( Array . isArray ( userIntegrations ) ) { integrations = [ ... integrations . filter ( integrations => userIntegrations . every ( userIntegration => userIntegration . name !== integrations . name ) ) , ... filterDuplicates ( userIntegrations ) , ] ; } else if ( typeof userIntegrations === 'function' ) { integrations = userIntegrations ( integrations ) ; integrations = Array . isArray ( integrations ) ? integrations : [ integrations ] ; } const integrationsNames = integrations . map ( i => i . name ) ; const alwaysLastToRun = 'Debug' ; if ( integrationsNames . indexOf ( alwaysLastToRun ) !== - 1 ) { integrations . push ( ... integrations . splice ( integrationsNames . indexOf ( alwaysLastToRun ) , 1 ) ) ; } return integrations ; } /** Setup given integration */ export function setupIntegration ( integration ) { if ( installedIntegrations . indexOf ( integration . name ) !== - 1 ) { return ; } integration . setupOnce ( addGlobalEventProcessor , getCurrentHub ) ; installedIntegrations . push ( integration . name ) ; logger . log ( `Integration installed: ${ integration . name }` ) ; } param integrations param withDefault export function setupIntegrations < O extends Options > ( options ) { const integrations = { } ; getIntegrationsToSetup ( options ) . forEach ( integration => { integrations [ integration . name ] = integration ; setupIntegration ( integration ) ; } ) ; addNonEnumerableProperty ( integrations , 'initialized' , true ) ; return integrations ; }