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
28 changes: 28 additions & 0 deletions packages/react-native/Libraries/Performance/Systrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ export function endEvent(args?: EventArgs): void {
}
}

/**
* Traces the execution of the given function by marking its start with
* `beginEvent` and its end with `endEvent`, even if the function throws.
*
* @example
* Systrace.trace('myEvent', () => {
* // logic to trace
* });
*/
export function trace<T>(
eventName: EventName,
fn: () => T,
args?: EventArgs,
): T {
if (isEnabled()) {
const eventNameString =
typeof eventName === 'function' ? eventName() : eventName;
global.nativeTraceBeginSection(TRACE_TAG_REACT, eventNameString, args);
try {
return fn();
} finally {
global.nativeTraceEndSection(TRACE_TAG_REACT);
}
}
return fn();
}

/**
* Marks the start of a potentially asynchronous event. The end of this event
* should be marked calling the `endAsyncEvent` function with the cookie
Expand Down Expand Up @@ -128,6 +155,7 @@ if (__DEV__) {
setEnabled,
beginEvent,
endEvent,
trace,
beginAsyncEvent,
endAsyncEvent,
counterEvent,
Expand Down
10 changes: 8 additions & 2 deletions packages/react-native/ReactNativeApi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<7f88fbd0bea021db8f52ca8ef3709d9e>>
* @generated SignedSource<<08dd369849273136812ea5edbda6e1df>>
*
* This file was generated by scripts/js-api/build-types/index.js.
*/
Expand Down Expand Up @@ -5041,6 +5041,7 @@ declare namespace Systrace {
setEnabled,
beginEvent,
endEvent,
trace,
beginAsyncEvent,
endAsyncEvent,
counterEvent,
Expand Down Expand Up @@ -5628,6 +5629,11 @@ declare type TouchEventProps = {
readonly onTouchStart?: (e: GestureResponderEvent) => void
readonly onTouchStartCapture?: (e: GestureResponderEvent) => void
}
declare function trace<T>(
eventName: EventName,
fn: () => T,
args?: EventArgs,
): T
declare type TransformsStyle = ____TransformStyle_Internal
declare interface TurboModule extends DEPRECATED_RCTExport<void> {}
declare namespace TurboModuleRegistry {
Expand Down Expand Up @@ -6217,7 +6223,7 @@ export {
Switch, // 015be3f7
SwitchChangeEvent, // 63e9c50b
SwitchProps, // 0dbf23ea
Systrace, // b5aa21fc
Systrace, // 626d178c
TVViewPropsIOS, // 330ce7b5
TargetedEvent, // 16e98910
TaskProvider, // 266dedf2
Expand Down
Loading