Skip to content

Commit 2d2f3fd

Browse files
HazATkamilogorek
authored andcommitted
feat: Introduce hub.run (#1584)
* feat: Introduce hub.run * meta: Add changelog
1 parent 2e5cfd5 commit 2d2f3fd

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [core] ref: Move debug initialization as the first step
1414
- [node] fix: Make handlers types compatibile with Express
1515
- [utils] fix: Dont break when non-string is passed to truncate
16+
- [hub] feat: Add `run` function that makes `this` hub the current global one
1617

1718
## 4.0.4
1819

packages/hub/src/global.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ export function getMainCarrier(): Carrier {
1111
return carrier.__SENTRY__;
1212
}
1313

14+
/**
15+
* Replaces the current main hub with the passed one on the global object
16+
*
17+
* @returns The old replaced hub
18+
*/
19+
export function makeMain(hub?: Hub): Hub | undefined {
20+
const registry = getMainCarrier();
21+
const oldHub = registry.hub;
22+
registry.hub = hub;
23+
return oldHub;
24+
}
25+
1426
/**
1527
* Returns the default hub instance.
1628
*

packages/hub/src/hub.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Breadcrumb, SentryBreadcrumbHint, SentryEvent, SentryEventHint, Severity } from '@sentry/types';
22
import { uuid4 } from '@sentry/utils/misc';
3+
import { makeMain } from './global';
34
import { Layer } from './interfaces';
45
import { Scope } from './scope';
56

@@ -250,4 +251,18 @@ export class Hub {
250251
callback(top.scope);
251252
}
252253
}
254+
255+
/**
256+
* For the duraction of the callback, this hub will be set as the global current Hub.
257+
* This function is useful if you want to run your own client and hook into an already initialized one
258+
* e.g.: Reporting issues to your own sentry when running in your component while still using the users configuration
259+
*/
260+
public run(callback: ((hub: Hub) => void)): void {
261+
const oldHub = makeMain(this);
262+
try {
263+
callback(this);
264+
} finally {
265+
makeMain(oldHub);
266+
}
267+
}
253268
}

packages/hub/test/lib/hub.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SentryEvent } from '@sentry/types';
2-
import { Hub, Scope } from '../../src';
2+
import { getCurrentHub, Hub, Scope } from '../../src';
33

44
const clientFn = jest.fn();
55
const asyncClientFn = async () => Promise.reject('error');
@@ -300,4 +300,18 @@ describe('Hub', () => {
300300
const eventId = hub.captureEvent(event);
301301
expect(eventId).toBe(hub.lastEventId());
302302
});
303+
304+
test('run', () => {
305+
const currentHub = getCurrentHub();
306+
const myScope = new Scope();
307+
const myClient = { a: 'b' };
308+
myScope.setExtra('a', 'b');
309+
const myHub = new Hub(myClient, myScope);
310+
myHub.run(hub => {
311+
expect(hub.getScope()).toBe(myScope);
312+
expect(hub.getClient()).toBe(myClient);
313+
expect(hub).toBe(getCurrentHub());
314+
});
315+
expect(currentHub).toBe(getCurrentHub());
316+
});
303317
});

0 commit comments

Comments
 (0)