forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add React Native perf metrics behind flag (#16)
- Loading branch information
1 parent
8c5e07a
commit 72a0dbf
Showing
11 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
export type RNReliabilityEventListener = (event: ReactNativeChromeDevToolsEvent) => void; | ||
|
||
type UnsunscribeFn = () => void; | ||
export type RNPerfMetrics = { | ||
addEventListener: (listener: RNReliabilityEventListener) => UnsunscribeFn, | ||
removeAllEventListeners: () => void, | ||
sendEvent: (event: ReactNativeChromeDevToolsEvent) => void, | ||
}; | ||
|
||
let instance: RNPerfMetrics|null = null; | ||
|
||
export function getInstance(): RNPerfMetrics { | ||
if (instance === null) { | ||
instance = new RNPerfMetricsImpl(); | ||
} | ||
return instance; | ||
} | ||
|
||
class RNPerfMetricsImpl implements RNPerfMetrics { | ||
#listeners: Set<RNReliabilityEventListener> = new Set(); | ||
|
||
addEventListener(listener: RNReliabilityEventListener): () => void { | ||
this.#listeners.add(listener); | ||
|
||
const unsubscribe = (): void => { | ||
this.#listeners.delete(listener); | ||
}; | ||
|
||
return unsubscribe; | ||
} | ||
|
||
removeAllEventListeners(): void { | ||
this.#listeners.clear(); | ||
} | ||
|
||
sendEvent(event: ReactNativeChromeDevToolsEvent): void { | ||
if (globalThis.enableReactNativePerfMetrics !== true) { | ||
return; | ||
} | ||
|
||
const errors = []; | ||
for (const listener of this.#listeners) { | ||
try { | ||
listener(event); | ||
} catch (e) { | ||
errors.push(e); | ||
} | ||
} | ||
|
||
if (errors.length > 0) { | ||
const error = new AggregateError(errors); | ||
console.error('Error occurred when calling event listeners', error); | ||
} | ||
} | ||
} | ||
|
||
export function registerPerfMetricsGlobalPostMessageHandler(): void { | ||
if (globalThis.enableReactNativePerfMetrics !== true || | ||
globalThis.enableReactNativePerfMetricsGlobalPostMessage !== true) { | ||
return; | ||
} | ||
|
||
getInstance().addEventListener(event => { | ||
window.postMessage({event, tag: 'react-native-chrome-devtools-perf-metrics'}, window.location.origin); | ||
}); | ||
} | ||
|
||
export type ReactNativeChromeDevToolsEvent = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
export {}; | ||
|
||
declare global { | ||
namespace globalThis { | ||
var enableReactNativePerfMetrics: boolean|undefined; | ||
var enableReactNativePerfMetricsGlobalPostMessage: boolean|undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters