Skip to content

Commit 9c6d501

Browse files
authored
feat(core): Deprecate getCurrentHub() (#10200)
Instead, users should use the direct replacement of the hub method. This feels like a very big step! Goodbye Hub! ![goodbye](https://github.com/getsentry/sentry-javascript/assets/2411343/bc3b8a49-5c85-4398-aad5-82cbcefb3215)
1 parent 29d597f commit 9c6d501

File tree

36 files changed

+81
-35
lines changed

36 files changed

+81
-35
lines changed

MIGRATION.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ npx @sentry/migr8@latest
1010
This will let you select which updates to run, and automatically update your code. Make sure to still review all code
1111
changes!
1212

13+
## Deprecate `getCurrentHub()`
14+
15+
In v8, you will no longer have a Hub, only Scopes as a concept. This also means that `getCurrentHub()` will eventually
16+
be removed.
17+
18+
Instead of `getCurrentHub()`, use the respective replacement API directly - see [Deprecate Hub](#deprecate-hub) for
19+
details.
20+
1321
## Deprecate `hub.bindClient()` and `makeMain()`
1422

1523
Instead, either directly use `initAndBind()`, or the new APIs `setCurrentClient()` and `client.init()`. See
@@ -54,7 +62,7 @@ If you are using the `Hub` right now, see the following table on how to migrate
5462
| ---------------------- | ------------------------------------------------------------------------------------ |
5563
| `new Hub()` | `withScope()`, `withIsolationScope()` or `new Scope()` |
5664
| hub.isOlderThan() | REMOVED - Was used to compare `Hub` instances, which are gonna be removed |
57-
| hub.bindClient() | A combination of `scope.setClient()` and `client.setupIntegrations()` |
65+
| hub.bindClient() | A combination of `scope.setClient()` and `client.init()` |
5866
| hub.pushScope() | `Sentry.withScope()` |
5967
| hub.popScope() | `Sentry.withScope()` |
6068
| hub.withScope() | `Sentry.withScope()` |

packages/astro/src/index.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export {
2525
// eslint-disable-next-line deprecation/deprecation
2626
getActiveTransaction,
2727
getHubFromCarrier,
28+
// eslint-disable-next-line deprecation/deprecation
2829
getCurrentHub,
2930
getClient,
3031
getCurrentScope,

packages/browser/src/exports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export {
3737
createTransport,
3838
flush,
3939
getHubFromCarrier,
40+
// eslint-disable-next-line deprecation/deprecation
4041
getCurrentHub,
4142
getClient,
4243
getCurrentScope,

packages/browser/src/sdk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ interface ShowReportDialogFunction {
159159
export const showReportDialog: ShowReportDialogFunction = (
160160
// eslint-disable-next-line deprecation/deprecation
161161
options: ReportDialogOptions = {},
162+
// eslint-disable-next-line deprecation/deprecation
162163
hub: Hub = getCurrentHub(),
163164
) => {
164165
// doesn't work without a document (React Native)

packages/browser/test/unit/profiling/hubextensions.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const patchedEncoder = (!global.window.TextEncoder && (global.window.TextEncoder
44
// @ts-expect-error patch the encoder on the window, else importing JSDOM fails (deleted in afterAll)
55
const patchedDecoder = (!global.window.TextDecoder && (global.window.TextDecoder = TextDecoder)) || true;
66

7-
import { getCurrentHub } from '@sentry/core';
7+
import { setCurrentClient } from '@sentry/core';
88
import type { Transaction } from '@sentry/types';
99
import { JSDOM } from 'jsdom';
1010

@@ -30,7 +30,6 @@ describe('BrowserProfilingIntegration', () => {
3030
// @ts-expect-error need to override global document
3131
global.location = dom.window.location;
3232

33-
const hub = getCurrentHub();
3433
const client: any = {
3534
getDsn() {
3635
return {};
@@ -47,8 +46,7 @@ describe('BrowserProfilingIntegration', () => {
4746
},
4847
};
4948

50-
// eslint-disable-next-line deprecation/deprecation
51-
hub.bindClient(client);
49+
setCurrentClient(client);
5250
});
5351

5452
// Reset back to previous values

packages/bun/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export {
4242
// eslint-disable-next-line deprecation/deprecation
4343
getActiveTransaction,
4444
getHubFromCarrier,
45+
// eslint-disable-next-line deprecation/deprecation
4546
getCurrentHub,
4647
getClient,
4748
getCurrentScope,

packages/core/src/exports.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,17 @@ export function withScope<T>(scope: ScopeInterface | undefined, callback: (scope
189189
export function withScope<T>(
190190
...rest: [callback: (scope: Scope) => T] | [scope: ScopeInterface | undefined, callback: (scope: Scope) => T]
191191
): T {
192+
// eslint-disable-next-line deprecation/deprecation
193+
const hub = getCurrentHub();
194+
192195
// If a scope is defined, we want to make this the active scope instead of the default one
193196
if (rest.length === 2) {
194197
const [scope, callback] = rest;
195198
if (!scope) {
196199
// eslint-disable-next-line deprecation/deprecation
197-
return getCurrentHub().withScope(callback);
200+
return hub.withScope(callback);
198201
}
199202

200-
const hub = getCurrentHub();
201203
// eslint-disable-next-line deprecation/deprecation
202204
return hub.withScope(() => {
203205
// eslint-disable-next-line deprecation/deprecation
@@ -207,7 +209,7 @@ export function withScope<T>(
207209
}
208210

209211
// eslint-disable-next-line deprecation/deprecation
210-
return getCurrentHub().withScope(rest[0]);
212+
return hub.withScope(rest[0]);
211213
}
212214

213215
/**

packages/core/src/hub.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,8 @@ export function makeMain(hub: Hub): Hub {
712712
* If a hub is already registered in the global carrier but this module
713713
* contains a more recent version, it replaces the registered version.
714714
* Otherwise, the currently registered hub will be returned.
715+
*
716+
* @deprecated Use the respective replacement method directly instead.
715717
*/
716718
export function getCurrentHub(): Hub {
717719
// Get main carrier (global for every environment)

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export {
3737
captureSession,
3838
} from './exports';
3939
export {
40+
// eslint-disable-next-line deprecation/deprecation
4041
getCurrentHub,
4142
getIsolationScope,
4243
getHubFromCarrier,

packages/core/src/sdk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Client, ClientOptions } from '@sentry/types';
22
import { consoleSandbox, logger } from '@sentry/utils';
33

44
import { DEBUG_BUILD } from './debug-build';
5+
import { getCurrentScope } from './exports';
56
import { getCurrentHub } from './hub';
67

78
/** A class object that can instantiate Client objects. */
@@ -29,9 +30,7 @@ export function initAndBind<F extends Client, O extends ClientOptions>(
2930
});
3031
}
3132
}
32-
const hub = getCurrentHub();
33-
// eslint-disable-next-line deprecation/deprecation
34-
const scope = hub.getScope();
33+
const scope = getCurrentScope();
3534
scope.update(options.initialScope);
3635

3736
const client = new clientClass(options);
@@ -43,6 +42,7 @@ export function initAndBind<F extends Client, O extends ClientOptions>(
4342
* Make the given client the current client.
4443
*/
4544
export function setCurrentClient(client: Client): void {
45+
// eslint-disable-next-line deprecation/deprecation
4646
const hub = getCurrentHub();
4747
// eslint-disable-next-line deprecation/deprecation
4848
const top = hub.getStackTop();

0 commit comments

Comments
 (0)