|
1 | 1 | import * as diagnosticsChannel from 'node:diagnostics_channel'; |
| 2 | +import { types } from 'node:util'; |
2 | 3 | import { Worker } from 'node:worker_threads'; |
3 | | -import { defineIntegration, getCurrentScope, getGlobalScope, getIsolationScope, mergeScopeData } from '@sentry/core'; |
| 4 | +import { |
| 5 | + defineIntegration, |
| 6 | + getClient, |
| 7 | + getCurrentScope, |
| 8 | + getGlobalScope, |
| 9 | + getIsolationScope, |
| 10 | + mergeScopeData, |
| 11 | +} from '@sentry/core'; |
4 | 12 | import type { Contexts, Event, EventHint, Integration, IntegrationFn, ScopeData } from '@sentry/types'; |
5 | 13 | import { GLOBAL_OBJ, getFilenameToDebugIdMap, logger } from '@sentry/utils'; |
6 | 14 | import { NODE_VERSION } from '../../nodeVersion'; |
7 | 15 | import type { NodeClient } from '../../sdk/client'; |
8 | 16 | import type { AnrIntegrationOptions, WorkerStartData } from './common'; |
9 | 17 |
|
| 18 | +const { isPromise } = types; |
| 19 | + |
10 | 20 | // This string is a placeholder that gets overwritten with the worker code. |
11 | 21 | export const base64WorkerScript = '###AnrWorkerScript###'; |
12 | 22 |
|
@@ -219,3 +229,26 @@ async function _startWorker( |
219 | 229 | clearInterval(timer); |
220 | 230 | }; |
221 | 231 | } |
| 232 | + |
| 233 | +export function disableAnrDetectionForCallback<T>(callback: () => T): T; |
| 234 | +export function disableAnrDetectionForCallback<T>(callback: () => Promise<T>): Promise<T>; |
| 235 | +/** |
| 236 | + * Disables ANR detection for the duration of the callback |
| 237 | + */ |
| 238 | +export function disableAnrDetectionForCallback<T>(callback: () => T | Promise<T>): T | Promise<T> { |
| 239 | + const integration = getClient()?.getIntegrationByName(INTEGRATION_NAME) as AnrInternal | undefined; |
| 240 | + |
| 241 | + if (!integration) { |
| 242 | + return callback(); |
| 243 | + } |
| 244 | + |
| 245 | + integration.stopWorker(); |
| 246 | + |
| 247 | + const result = callback(); |
| 248 | + if (isPromise(result)) { |
| 249 | + return result.finally(() => integration.startWorker()); |
| 250 | + } |
| 251 | + |
| 252 | + integration.startWorker(); |
| 253 | + return result; |
| 254 | +} |
0 commit comments