@@ -50,6 +50,14 @@ export const API_VERSION = 4;
5050 */
5151const DEFAULT_BREADCRUMBS = 100 ;
5252
53+ /**
54+ * Strategy used to track async context.
55+ */
56+ export interface AsyncContextStrategy {
57+ getCurrentHub : ( ) => Hub | undefined ;
58+ runWithAsyncContext < T , A > ( callback : ( hub : Hub , ...args : A [ ] ) => T , ...args : A [ ] ) : T ;
59+ }
60+
5361/**
5462 * A layer in the process stack.
5563 * @hidden
@@ -66,6 +74,7 @@ export interface Layer {
6674export interface Carrier {
6775 __SENTRY__ ?: {
6876 hub ?: Hub ;
77+ acs ?: AsyncContextStrategy ;
6978 /**
7079 * Extra Hub properties injected by various SDKs
7180 */
@@ -519,6 +528,14 @@ export function getCurrentHub(): Hub {
519528 // Get main carrier (global for every environment)
520529 const registry = getMainCarrier ( ) ;
521530
531+ if ( registry . __SENTRY__ && registry . __SENTRY__ . acs ) {
532+ const hub = registry . __SENTRY__ . acs . getCurrentHub ( ) ;
533+
534+ if ( hub ) {
535+ return hub ;
536+ }
537+ }
538+
522539 // If there's no hub, or its an old API, assign a new one
523540 if ( ! hasHubOnCarrier ( registry ) || getHubFromCarrier ( registry ) . isOlderThan ( API_VERSION ) ) {
524541 setHubOnCarrier ( registry , new Hub ( ) ) ;
@@ -532,6 +549,34 @@ export function getCurrentHub(): Hub {
532549 return getHubFromCarrier ( registry ) ;
533550}
534551
552+ /**
553+ * @private Private API with no semver guarantees!
554+ *
555+ * Sets the global async context strategy
556+ */
557+ export function setAsyncContextStrategy ( strategy : AsyncContextStrategy ) : void {
558+ // Get main carrier (global for every environment)
559+ const registry = getMainCarrier ( ) ;
560+ registry . __SENTRY__ = registry . __SENTRY__ || { } ;
561+ registry . __SENTRY__ . acs = strategy ;
562+ }
563+
564+ /**
565+ * @private Private API with no semver guarantees!
566+ *
567+ * Runs the given callback function with the global async context strategy
568+ */
569+ export function runWithAsyncContext < T , A > ( callback : ( hub : Hub , ...args : A [ ] ) => T , ...args : A [ ] ) : T {
570+ const registry = getMainCarrier ( ) ;
571+
572+ if ( registry . __SENTRY__ && registry . __SENTRY__ . acs ) {
573+ return registry . __SENTRY__ . acs . runWithAsyncContext ( callback , ...args ) ;
574+ }
575+
576+ // if there was no strategy, fallback to just calling the callback
577+ return callback ( getCurrentHub ( ) , ...args ) ;
578+ }
579+
535580/**
536581 * Try to read the hub from an active domain, and fallback to the registry if one doesn't exist
537582 * @returns discovered hub
0 commit comments