@@ -25,6 +25,7 @@ import { GLOBAL_OBJ, isThenable, logger, timestampInSeconds, uuid4 } from '@sent
2525import { DEFAULT_ENVIRONMENT } from './constants' ;
2626import { DEBUG_BUILD } from './debug-build' ;
2727import type { Hub } from './hub' ;
28+ import { runWithAsyncContext } from './hub' ;
2829import { getCurrentHub , getIsolationScope } from './hub' ;
2930import type { Scope } from './scope' ;
3031import { closeSession , makeSession , updateSession } from './session' ;
@@ -35,7 +36,7 @@ import { parseEventHintOrCaptureContext } from './utils/prepareEvent';
3536 * Captures an exception event and sends it to Sentry.
3637 *
3738 * @param exception The exception to capture.
38- * @param hint Optinal additional data to attach to the Sentry event.
39+ * @param hint Optional additional data to attach to the Sentry event.
3940 * @returns the id of the captured Sentry event.
4041 */
4142export function captureException (
@@ -208,6 +209,26 @@ export function withScope<T>(
208209 return getCurrentHub ( ) . withScope ( rest [ 0 ] ) ;
209210}
210211
212+ /**
213+ * Attempts to fork the current isolation scope and the current scope based on the current async context strategy. If no
214+ * async context strategy is set, the isolation scope and the current scope will not be forked (this is currently the
215+ * case, for example, in the browser).
216+ *
217+ * Usage of this function in environments without async context strategy is discouraged and may lead to unexpected behaviour.
218+ *
219+ * This function is intended for Sentry SDK and SDK integration development. It is not recommended to be used in "normal"
220+ * applications directly because it comes with pitfalls. Use at your own risk!
221+ *
222+ * @param callback The callback in which the passed isolation scope is active. (Note: In environments without async
223+ * context strategy, the currently active isolation scope may change within execution of the callback.)
224+ * @returns The same value that `callback` returns.
225+ */
226+ export function withIsolationScope < T > ( callback : ( isolationScope : Scope ) => T ) : T {
227+ return runWithAsyncContext ( ( ) => {
228+ return callback ( getIsolationScope ( ) ) ;
229+ } ) ;
230+ }
231+
211232/**
212233 * Starts a new `Transaction` and returns it. This is the entry point to manual tracing instrumentation.
213234 *
0 commit comments