All files global.ts

100% Statements 15/15
100% Branches 11/11
100% Functions 3/3
100% Lines 15/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 513x                   3x         3x 2x                   3x 2x   2x 1x     2x               3x 2x 1x   1x 1x 1x      
import { API_VERSION, Hub } from './hub';
import { Carrier } from './interfaces';
 
/** Global interface helper for type safety. */
interface Global {
  __SENTRY__: Carrier;
}
 
declare var global: Global;
 
global.__SENTRY__ = global.__SENTRY__ || {
  hub: undefined,
};
 
/** Returns the global shim registry. */
export function getMainCarrier(): Carrier {
  return global.__SENTRY__;
}
 
/**
 * Returns the default hub instance.
 *
 * If a hub is already registered in the global carrier but this module
 * contains a more recent version, it replaces the registered version.
 * Otherwise, the currently registered hub will be returned.
 */
export function getDefaultHub(): Hub {
  const registry = getMainCarrier();
 
  if (!registry.hub || registry.hub.isOlderThan(API_VERSION)) {
    registry.hub = new Hub();
  }
 
  return registry.hub;
}
 
/**
 * This will create a new {@link Hub} and add to the passed object on
 * __SENTRY__.hub.
 * @param carrier object
 */
export function getHubFromCarrier(carrier: any): Hub {
  if (carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub) {
    return carrier.__SENTRY__.hub;
  } else {
    carrier.__SENTRY__ = {};
    carrier.__SENTRY__.hub = new Hub();
    return carrier.__SENTRY__.hub;
  }
}