Skip to content

Commit

Permalink
Rename to main process function to enableMainProcessAnrDetection
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Oct 13, 2023
1 parent 9fd19d6 commit 371ab99
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 42 deletions.
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ interface ProcessEntryPoint {
init: (options: Partial<ElectronOptions>) => void;
close?: (timeout?: number) => Promise<boolean>;
flush?: (timeout?: number) => Promise<boolean>;
enableAnrDetection?(options: Parameters<typeof enableNodeAnrDetection>[0]): Promise<void>;
enableMainProcessAnrDetection?(options: Parameters<typeof enableNodeAnrDetection>[0]): Promise<void>;
}

/** Fetches the SDK entry point for the current process */
Expand Down Expand Up @@ -178,25 +178,25 @@ export async function flush(timeout?: number): Promise<boolean> {
* 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<typeof enableNodeAnrDetection>[0]): Promise<void> {
export function enableMainProcessAnrDetection(options: Parameters<typeof enableNodeAnrDetection>[0]): Promise<void> {
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');
Expand Down
43 changes: 12 additions & 31 deletions src/main/anr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,30 +117,6 @@ export function createRendererAnrStatusHook(): (status: RendererStatus, contents
};
}

type MainProcessAnrOptions = Parameters<typeof enableNodeAnrDetection>[0];

function enableAnrMainProcess(options: MainProcessAnrOptions): Promise<void> {
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.
*
Expand All @@ -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<void> {
if (options.mainProcess !== false) {
return enableAnrMainProcess(options.mainProcess || {});
export function enableMainProcessAnrDetection(options: Parameters<typeof enableNodeAnrDetection>[0]): Promise<void> {
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);
}
2 changes: 1 addition & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
2 changes: 1 addition & 1 deletion src/renderer/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface ElectronRendererOptions extends BrowserOptions {
export function init<O extends ElectronRendererOptions>(
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');

Expand Down
4 changes: 2 additions & 2 deletions test/e2e/test-apps/anr/anr-main/src/main.js
Original file line number Diff line number Diff line change
@@ -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__',
Expand All @@ -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();
Expand Down

0 comments on commit 371ab99

Please sign in to comment.