Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions Libraries/WebPerformance/NativePerformanceObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ void NativePerformanceObserver::setOnPerformanceEntryCallback(
PerformanceEntryReporter::getInstance().setReportingCallback(callback);
}

void NativePerformanceObserver::logRawEntry(
jsi::Runtime &rt,
RawPerformanceEntry entry) {
PerformanceEntryReporter::getInstance().logEntry(entry);
}

} // namespace facebook::react
2 changes: 2 additions & 0 deletions Libraries/WebPerformance/NativePerformanceObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class NativePerformanceObserver
jsi::Runtime &rt,
std::optional<AsyncCallback<>> callback);

void logRawEntry(jsi::Runtime &rt, RawPerformanceEntry entry);

private:
};

Expand Down
7 changes: 4 additions & 3 deletions Libraries/WebPerformance/NativePerformanceObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import type {TurboModule} from '../TurboModule/RCTExport';

import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry';
import NativePerformanceObserverRef from './NativePerformanceObserverRef';

export const RawPerformanceEntryTypeValues = {
UNDEFINED: 0,
Expand Down Expand Up @@ -42,8 +43,8 @@ export interface Spec extends TurboModule {
+stopReporting: (entryType: string) => void;
+popPendingEntries: () => GetPendingEntriesResult;
+setOnPerformanceEntryCallback: (callback?: () => void) => void;
+logRawEntry: (entry: RawPerformanceEntry) => void;
}

export default (TurboModuleRegistry.get<Spec>(
'NativePerformanceObserverCxx',
): ?Spec);
export default (TurboModuleRegistry.get<Spec>('NativePerformanceObserverCxx') ??
NativePerformanceObserverRef: ?Spec);
71 changes: 71 additions & 0 deletions Libraries/WebPerformance/NativePerformanceObserverRef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/

import type {
GetPendingEntriesResult,
RawPerformanceEntry,
RawPerformanceEntryType,
Spec as NativePerformanceObserver,
} from './NativePerformanceObserver';

import {RawPerformanceEntryTypeValues} from './NativePerformanceObserver';

function performanceEntryTypeToRaw(type: string): RawPerformanceEntryType {
switch (type) {
case 'mark':
return RawPerformanceEntryTypeValues.MARK;
case 'measure':
return RawPerformanceEntryTypeValues.MEASURE;
case 'event':
return RawPerformanceEntryTypeValues.EVENT;
default:
throw new TypeError(
`performanceEntryTypeToRaw: unexpected performance entry type received: ${type}`,
);
}
}

const NativePerformanceObserverRef: NativePerformanceObserver = (function () {
const _reportingType: Set<RawPerformanceEntryType> = new Set();
let _entries: Array<RawPerformanceEntry> = [];
let _onPerformanceEntryCallback: ?() => void;

return {
startReporting: (entryType: string) => {
_reportingType.add(performanceEntryTypeToRaw(entryType));
},

stopReporting: (entryType: string) => {
_reportingType.delete(performanceEntryTypeToRaw(entryType));
},

popPendingEntries: (): GetPendingEntriesResult => {
const res = _entries;
_entries = [];
return {
droppedEntriesCount: 0,
entries: res,
};
},

setOnPerformanceEntryCallback: (callback?: () => void) => {
_onPerformanceEntryCallback = callback;
},

logRawEntry: (entry: RawPerformanceEntry) => {
if (_reportingType.has(entry.entryType)) {
_entries.push(entry);
_onPerformanceEntryCallback?.();
}
},
};
})();

export default NativePerformanceObserverRef;
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall react_native
*/

import {RawPerformanceEntryTypeValues} from '../NativePerformanceObserver';
import NativePerformanceObserverRef from '../NativePerformanceObserverRef';

describe('NativePerformanceObserver', () => {
it('correctly starts and stops listening to entries in a nominal scenario', async () => {
NativePerformanceObserverRef.startReporting('mark');

NativePerformanceObserverRef.logRawEntry({
name: 'mark1',
entryType: RawPerformanceEntryTypeValues.MARK,
startTime: 0,
duration: 10,
});

NativePerformanceObserverRef.logRawEntry({
name: 'mark2',
entryType: RawPerformanceEntryTypeValues.MARK,
startTime: 0,
duration: 20,
});

NativePerformanceObserverRef.logRawEntry({
name: 'event1',
entryType: RawPerformanceEntryTypeValues.EVENT,
startTime: 0,
duration: 20,
});

const entriesResult = NativePerformanceObserverRef.popPendingEntries();
expect(entriesResult).not.toBe(undefined);
const entries = entriesResult.entries;

expect(entries.length).toBe(2);
expect(entries[0].name).toBe('mark1');
expect(entries[1].name).toBe('mark2');

const entriesResult1 = NativePerformanceObserverRef.popPendingEntries();
expect(entriesResult1).not.toBe(undefined);
const entries1 = entriesResult1.entries;
expect(entries1.length).toBe(0);

NativePerformanceObserverRef.stopReporting('mark');
});
});
22 changes: 22 additions & 0 deletions Libraries/WebPerformance/__tests__/PerformanceObserver-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall react_native
*/

import NativePerformanceObserver from '../NativePerformanceObserver';
import PerformanceObserver from '../PerformanceObserver';

describe('PerformanceObserver', () => {
it('is backed by an existing NativePerformanceObserver implementation', async () => {
expect(NativePerformanceObserver).not.toBe(undefined);

const observer = new PerformanceObserver((list, _observer) => {});
expect(() => observer.observe({entryTypes: []})).not.toThrow();
observer.disconnect();
});
});