Skip to content

Commit 9c0969b

Browse files
committed
fix ember ts issues
1 parent 3b2e57c commit 9c0969b

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

packages/ember/addon/instance-initializers/sentry-performance.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getActiveTransaction } from '..';
99
import { browserPerformanceTimeOrigin, GLOBAL_OBJ, timestampWithMs } from '@sentry/utils';
1010
import { macroCondition, isTesting, getOwnConfig } from '@embroider/macros';
1111
import { EmberSentryConfig, GlobalConfig, OwnConfig } from '../types';
12+
import RouterService from '@ember/routing/router-service';
1213

1314
function getSentryConfig() {
1415
const _global = GLOBAL_OBJ as typeof GLOBAL_OBJ & GlobalConfig;
@@ -308,24 +309,21 @@ function _instrumentInitialLoad(config: EmberSentryConfig) {
308309
const startName = '@sentry/ember:initial-load-start';
309310
const endName = '@sentry/ember:initial-load-end';
310311

311-
const { performance } = window;
312-
// @ts-ignore
313-
const HAS_PERFORMANCE = performance && performance.clearMarks && performance.clearMeasures;
312+
let { HAS_PERFORMANCE, HAS_PERFORMANCE_TIMING } = _hasPerformanceSupport();
314313

315314
if (!HAS_PERFORMANCE) {
316315
return;
317316
}
318317

318+
const { performance } = window;
319+
319320
if (config.disableInitialLoadInstrumentation) {
320321
performance.clearMarks(startName);
321322
performance.clearMarks(endName);
322323
return;
323324
}
324325

325326
// Split performance check in two so clearMarks still happens even if timeOrigin isn't available.
326-
const HAS_PERFORMANCE_TIMING =
327-
// @ts-ignore
328-
performance.measure && performance.getEntriesByName && browserPerformanceTimeOrigin !== undefined;
329327
if (!HAS_PERFORMANCE_TIMING) {
330328
return;
331329
}
@@ -356,6 +354,26 @@ function _instrumentInitialLoad(config: EmberSentryConfig) {
356354
performance.clearMeasures(measureName);
357355
}
358356

357+
function _hasPerformanceSupport() {
358+
// TS says that all of these methods are always available, but some of them may not be supported in older browsers
359+
// So we "pretend" they are all optional in order to be able to check this properly without TS complaining
360+
const _performance = window.performance as {
361+
clearMarks?: Performance['clearMarks'];
362+
clearMeasures?: Performance['clearMeasures'];
363+
measure?: Performance['measure'];
364+
getEntriesByName?: Performance['getEntriesByName'];
365+
};
366+
const HAS_PERFORMANCE = Boolean(_performance && _performance.clearMarks && _performance.clearMeasures);
367+
const HAS_PERFORMANCE_TIMING = Boolean(
368+
_performance.measure && _performance.getEntriesByName && browserPerformanceTimeOrigin !== undefined,
369+
);
370+
371+
return {
372+
HAS_PERFORMANCE,
373+
HAS_PERFORMANCE_TIMING,
374+
};
375+
}
376+
359377
export async function instrumentForPerformance(appInstance: ApplicationInstance) {
360378
const config = getSentryConfig();
361379
const sentryConfig = config.sentry;
@@ -373,7 +391,9 @@ export async function instrumentForPerformance(appInstance: ApplicationInstance)
373391
new tracing.Integrations.BrowserTracing({
374392
routingInstrumentation: (customStartTransaction, startTransactionOnPageLoad) => {
375393
const routerMain = appInstance.lookup('router:main');
376-
let routerService = appInstance.lookup('service:router');
394+
let routerService = appInstance.lookup('service:router') as
395+
| RouterService & { externalRouter?: RouterService; _hasMountedSentryPerformanceRouting?: boolean };
396+
377397
if (routerService.externalRouter) {
378398
// Using ember-engines-router-service in an engine.
379399
routerService = routerService.externalRouter;

0 commit comments

Comments
 (0)