|
1 | | -import type { Client, ClientOptions, Hub as HubInterface } from '@sentry/types'; |
| 1 | +import type { Client, ClientOptions } from '@sentry/types'; |
2 | 2 | import { consoleSandbox, logger } from '@sentry/utils'; |
3 | 3 | import { getCurrentScope } from './currentScopes'; |
4 | 4 |
|
| 5 | +import { getMainCarrier, getSentryCarrier } from './asyncContext'; |
5 | 6 | import { DEBUG_BUILD } from './debug-build'; |
6 | 7 | import type { Hub } from './hub'; |
7 | | -import { getCurrentHub } from './hub'; |
8 | 8 |
|
9 | 9 | /** A class object that can instantiate Client objects. */ |
10 | 10 | export type ClientClass<F extends Client, O extends ClientOptions> = new (options: O) => F; |
@@ -44,19 +44,22 @@ export function initAndBind<F extends Client, O extends ClientOptions>( |
44 | 44 | */ |
45 | 45 | export function setCurrentClient(client: Client): void { |
46 | 46 | getCurrentScope().setClient(client); |
| 47 | + registerClientOnGlobalHub(client); |
| 48 | +} |
47 | 49 |
|
48 | | - // is there a hub too? |
| 50 | +/** |
| 51 | + * Unfortunately, we still have to manually bind the client to the "hub" set on the global |
| 52 | + * Sentry carrier object. This is because certain scripts (e.g. our loader script) obtain |
| 53 | + * the client via `window.__SENTRY__.hub.getClient()`. |
| 54 | + * |
| 55 | + * @see {@link hub.ts getGlobalHub} |
| 56 | + */ |
| 57 | +function registerClientOnGlobalHub(client: Client): void { |
49 | 58 | // eslint-disable-next-line deprecation/deprecation |
50 | | - const hub = getCurrentHub(); |
51 | | - if (isHubClass(hub)) { |
| 59 | + const sentryGlobal = getSentryCarrier(getMainCarrier()) as { hub?: Hub }; |
| 60 | + // eslint-disable-next-line deprecation/deprecation |
| 61 | + if (sentryGlobal.hub && typeof sentryGlobal.hub.getStackTop === 'function') { |
52 | 62 | // eslint-disable-next-line deprecation/deprecation |
53 | | - const top = hub.getStackTop(); |
54 | | - top.client = client; |
| 63 | + sentryGlobal.hub.getStackTop().client = client; |
55 | 64 | } |
56 | 65 | } |
57 | | - |
58 | | -// eslint-disable-next-line deprecation/deprecation |
59 | | -function isHubClass(hub: HubInterface): hub is Hub { |
60 | | - // eslint-disable-next-line deprecation/deprecation |
61 | | - return !!(hub as Hub).getStackTop; |
62 | | -} |
|
0 commit comments