Skip to content

feat: Introduce hub.run #1584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [core] ref: Move debug initialization as the first step
- [node] fix: Make handlers types compatibile with Express
- [utils] fix: Dont break when non-string is passed to truncate
- [hub] feat: Add `run` function that makes `this` hub the current global one

## 4.0.4

Expand Down
12 changes: 12 additions & 0 deletions packages/hub/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ export function getMainCarrier(): Carrier {
return carrier.__SENTRY__;
}

/**
* Replaces the current main hub with the passed one on the global object
*
* @returns The old replaced hub
*/
export function makeMain(hub?: Hub): Hub | undefined {
const registry = getMainCarrier();
const oldHub = registry.hub;
registry.hub = hub;
return oldHub;
}

/**
* Returns the default hub instance.
*
Expand Down
15 changes: 15 additions & 0 deletions packages/hub/src/hub.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Breadcrumb, SentryBreadcrumbHint, SentryEvent, SentryEventHint, Severity } from '@sentry/types';
import { uuid4 } from '@sentry/utils/misc';
import { makeMain } from './global';
import { Layer } from './interfaces';
import { Scope } from './scope';

Expand Down Expand Up @@ -250,4 +251,18 @@ export class Hub {
callback(top.scope);
}
}

/**
* For the duraction of the callback, this hub will be set as the global current Hub.
* This function is useful if you want to run your own client and hook into an already initialized one
* e.g.: Reporting issues to your own sentry when running in your component while still using the users configuration
*/
public run(callback: ((hub: Hub) => void)): void {
const oldHub = makeMain(this);
try {
callback(this);
} finally {
makeMain(oldHub);
}
}
}
16 changes: 15 additions & 1 deletion packages/hub/test/lib/hub.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SentryEvent } from '@sentry/types';
import { Hub, Scope } from '../../src';
import { getCurrentHub, Hub, Scope } from '../../src';

const clientFn = jest.fn();
const asyncClientFn = async () => Promise.reject('error');
Expand Down Expand Up @@ -300,4 +300,18 @@ describe('Hub', () => {
const eventId = hub.captureEvent(event);
expect(eventId).toBe(hub.lastEventId());
});

test('run', () => {
const currentHub = getCurrentHub();
const myScope = new Scope();
const myClient = { a: 'b' };
myScope.setExtra('a', 'b');
const myHub = new Hub(myClient, myScope);
myHub.run(hub => {
expect(hub.getScope()).toBe(myScope);
expect(hub.getClient()).toBe(myClient);
expect(hub).toBe(getCurrentHub());
});
expect(currentHub).toBe(getCurrentHub());
});
});