@@ -50,6 +50,14 @@ export const API_VERSION = 4;
50
50
*/
51
51
const DEFAULT_BREADCRUMBS = 100 ;
52
52
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
+
53
61
/**
54
62
* A layer in the process stack.
55
63
* @hidden
@@ -66,6 +74,7 @@ export interface Layer {
66
74
export interface Carrier {
67
75
__SENTRY__ ?: {
68
76
hub ?: Hub ;
77
+ acs ?: AsyncContextStrategy ;
69
78
/**
70
79
* Extra Hub properties injected by various SDKs
71
80
*/
@@ -519,6 +528,14 @@ export function getCurrentHub(): Hub {
519
528
// Get main carrier (global for every environment)
520
529
const registry = getMainCarrier ( ) ;
521
530
531
+ if ( registry . __SENTRY__ && registry . __SENTRY__ . acs ) {
532
+ const hub = registry . __SENTRY__ . acs . getCurrentHub ( ) ;
533
+
534
+ if ( hub ) {
535
+ return hub ;
536
+ }
537
+ }
538
+
522
539
// If there's no hub, or its an old API, assign a new one
523
540
if ( ! hasHubOnCarrier ( registry ) || getHubFromCarrier ( registry ) . isOlderThan ( API_VERSION ) ) {
524
541
setHubOnCarrier ( registry , new Hub ( ) ) ;
@@ -532,6 +549,34 @@ export function getCurrentHub(): Hub {
532
549
return getHubFromCarrier ( registry ) ;
533
550
}
534
551
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
+
535
580
/**
536
581
* Try to read the hub from an active domain, and fallback to the registry if one doesn't exist
537
582
* @returns discovered hub
0 commit comments