Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apm): Remove Performance references #2495

Merged
merged 3 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott

- [apm] fix: Use Performance API for timings when available, including Web Workers (#2492)
- [apm] fix: Remove Performance references (#2495)

## 5.14.1

Expand Down
19 changes: 16 additions & 3 deletions packages/utils/src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,18 @@ function _htmlElementAsString(el: unknown): string {
const INITIAL_TIME = Date.now();
let prevNow = 0;

const performanceFallback: Pick<Performance, 'now' | 'timeOrigin'> = {
/**
* Cross platform compatible partial performance implementation
*/
interface CrossPlatformPerformance {
/**
* Returns the current timestamp in ms
*/
now(): number;
timeOrigin: number;
}

const performanceFallback: CrossPlatformPerformance = {
now(): number {
let now = Date.now() - INITIAL_TIME;
if (now < prevNow) {
Expand All @@ -361,10 +372,12 @@ const performanceFallback: Pick<Performance, 'now' | 'timeOrigin'> = {
timeOrigin: INITIAL_TIME,
};

export const crossPlatformPerformance: Pick<Performance, 'now' | 'timeOrigin'> = (() => {
export const crossPlatformPerformance: CrossPlatformPerformance = (() => {
if (isNodeEnv()) {
try {
const perfHooks = dynamicRequire(module, 'perf_hooks') as { performance: Performance };
// tslint:disable-next-line: no-unsafe-any
const perfHooks = dynamicRequire(module, 'perf_hooks');
// tslint:disable-next-line: no-unsafe-any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this work?

Suggested change
// tslint:disable-next-line: no-unsafe-any
const perfHooks = dynamicRequire(module, 'perf_hooks');
// tslint:disable-next-line: no-unsafe-any
const perfHooks = dynamicRequire(module, 'perf_hooks') as { performance: CrossPlatformPerformance };

return perfHooks.performance;
} catch (_) {
return performanceFallback;
Expand Down