From 371ab992fd49353986737eddcf8975b84b23e5db Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 13 Oct 2023 17:13:10 +0100 Subject: [PATCH] Rename to main process function to `enableMainProcessAnrDetection` --- src/index.ts | 14 +++---- src/main/anr.ts | 43 ++++++--------------- src/main/index.ts | 2 +- src/renderer/sdk.ts | 2 +- test/e2e/test-apps/anr/anr-main/src/main.js | 4 +- 5 files changed, 23 insertions(+), 42 deletions(-) diff --git a/src/index.ts b/src/index.ts index 358c4638..2984eaa4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -64,7 +64,7 @@ interface ProcessEntryPoint { init: (options: Partial) => void; close?: (timeout?: number) => Promise; flush?: (timeout?: number) => Promise; - enableAnrDetection?(options: Parameters[0]): Promise; + enableMainProcessAnrDetection?(options: Parameters[0]): Promise; } /** Fetches the SDK entry point for the current process */ @@ -178,25 +178,25 @@ export async function flush(timeout?: number): Promise { * child process. * * ```js - * import { init, enableAnrDetection } from '@sentry/electron'; + * import { init, enableMainProcessAnrDetection } from '@sentry/electron'; * * init({ dsn: "__DSN__" }); * * // with ESM + Electron v28+ - * await enableAnrDetection({ captureStackTrace: true }); + * await enableMainProcessAnrDetection({ captureStackTrace: true }); * runApp(); * * // with CJS - * enableAnrDetection({ captureStackTrace: true }).then(() => { + * enableMainProcessAnrDetection({ captureStackTrace: true }).then(() => { * runApp(); * }); * ``` */ -export async function enableAnrDetection(options: Parameters[0]): Promise { +export function enableMainProcessAnrDetection(options: Parameters[0]): Promise { const entryPoint = getEntryPoint(); - if (entryPoint.enableAnrDetection) { - return entryPoint.enableAnrDetection(options); + if (entryPoint.enableMainProcessAnrDetection) { + return entryPoint.enableMainProcessAnrDetection(options); } throw new Error('ANR detection should be started in the main process'); diff --git a/src/main/anr.ts b/src/main/anr.ts index 1535e7d6..d74a70d4 100644 --- a/src/main/anr.ts +++ b/src/main/anr.ts @@ -117,30 +117,6 @@ export function createRendererAnrStatusHook(): (status: RendererStatus, contents }; } -type MainProcessAnrOptions = Parameters[0]; - -function enableAnrMainProcess(options: MainProcessAnrOptions): Promise { - if (ELECTRON_MAJOR_VERSION < 4) { - throw new Error('Main process ANR detection is only supported on Electron v4+'); - } - - const mainOptions = { - entryScript: app.getAppPath(), - ...options, - }; - - return enableNodeAnrDetection(mainOptions); -} - -interface Options { - /** - * Main process ANR options. - * - * Set to false to disable ANR detection in the main process. - */ - mainProcess?: MainProcessAnrOptions | false; -} - /** * **Note** This feature is still in beta so there may be breaking changes in future releases. * @@ -150,24 +126,29 @@ interface Options { * child process. * * ```js - * import { init, enableAnrDetection } from '@sentry/electron'; + * import { init, enableMainProcessAnrDetection } from '@sentry/electron'; * * init({ dsn: "__DSN__" }); * * // with ESM + Electron v28+ - * await enableAnrDetection({ mainProcess: { captureStackTrace: true }}); + * await enableMainProcessAnrDetection({ captureStackTrace: true }); * runApp(); * * // with CJS - * enableAnrDetection({ mainProcess: { captureStackTrace: true }}).then(() => { + * enableMainProcessAnrDetection({ captureStackTrace: true }).then(() => { * runApp(); * }); * ``` */ -export async function enableAnrDetection(options: Options = {}): Promise { - if (options.mainProcess !== false) { - return enableAnrMainProcess(options.mainProcess || {}); +export function enableMainProcessAnrDetection(options: Parameters[0]): Promise { + if (ELECTRON_MAJOR_VERSION < 4) { + throw new Error('Main process ANR detection is only supported on Electron v4+'); } - return Promise.resolve(); + const mainOptions = { + entryScript: app.getAppPath(), + ...options, + }; + + return enableNodeAnrDetection(mainOptions); } diff --git a/src/main/index.ts b/src/main/index.ts index 393df934..4f17eedf 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -58,4 +58,4 @@ export const Integrations = { ...ElectronMainIntegrations, ...NodeIntegrations } export type { ElectronMainOptions } from './sdk'; export { init, defaultIntegrations } from './sdk'; export { IPCMode } from '../common'; -export { enableAnrDetection } from './anr'; +export { enableMainProcessAnrDetection } from './anr'; diff --git a/src/renderer/sdk.ts b/src/renderer/sdk.ts index 493b3f78..e62168a7 100644 --- a/src/renderer/sdk.ts +++ b/src/renderer/sdk.ts @@ -40,7 +40,7 @@ interface ElectronRendererOptions extends BrowserOptions { export function init( options: ElectronRendererOptions & O = {} as ElectronRendererOptions & O, // This parameter name ensures that TypeScript error messages contain a hint for fixing SDK version mismatches - originalInit: (if_you_get_a_typescript_error_ensure_sdks_use_version_v7_73_0: O) => void = browserInit, + originalInit: (if_you_get_a_typescript_error_ensure_sdks_use_version_v7_74_0: O) => void = browserInit, ): void { ensureProcess('renderer'); diff --git a/test/e2e/test-apps/anr/anr-main/src/main.js b/test/e2e/test-apps/anr/anr-main/src/main.js index d197d788..c733ffd7 100644 --- a/test/e2e/test-apps/anr/anr-main/src/main.js +++ b/test/e2e/test-apps/anr/anr-main/src/main.js @@ -1,7 +1,7 @@ const crypto = require('crypto'); const { app } = require('electron'); -const { init, enableAnrDetection } = require('@sentry/electron/main'); +const { init, enableMainProcessAnrDetection } = require('@sentry/electron/main'); init({ dsn: '__DSN__', @@ -18,7 +18,7 @@ function longWork() { } } -enableAnrDetection({ mainProcess: { debug: true, anrThreshold: 1000, captureStackTrace: true } }).then(() => { +enableMainProcessAnrDetection({ anrThreshold: 1000, captureStackTrace: true }).then(() => { app.on('ready', () => { setTimeout(() => { longWork();