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

React Native raw event EventEmitter - intended for app-specific perf listeners and debugging #23232

Merged
Merged
Show file tree
Hide file tree
Changes from 13 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
27 changes: 27 additions & 0 deletions packages/react-native-renderer/src/ReactFabricEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {plugins} from './legacy-events/EventPluginRegistry';
import getListener from './ReactNativeGetListener';
import {runEventsInBatch} from './legacy-events/EventBatching';

import {RawEventTelemetryEventEmitterOffByDefault} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';

export {getListener, registrationNameModules as registrationNames};

/**
Expand Down Expand Up @@ -88,6 +90,31 @@ export function dispatchEvent(
}

batchedUpdates(function() {
// Emit event to the event telemetry system.
//
// NOTE: this event telemetry system does *nothing* without explicit,
// per-application opt-in, and merely emits events into the local
// EventEmitter below. If *you* do not add listeners to the `RawEventTelemetryEventEmitter`,
// then all of these emitted events will just blackhole and are no-ops.
// It is available (although not officially supported... yet) if you want to collect
// telemetry on event latency in your application, and could also be useful for debugging
// low-level events issues.
//
// If you do not have any event telemetry and are extremely concerned about event perf,
// it is safe to disable these "emit" statements; it will prevent checking the size of
// an empty array twice and prevent two no-ops. Practically the overhead is so low that
// we don't think it's worth thinking about in prod; your perf issues probably lie elsewhere.
//
// We emit two events here: one for listeners to this specific event,
// and one for the catchall listener '*', for any listeners that want
// to be notified for all events.
// Note that extracted events are *not* emitted into the telemetry system,
// only events that have a 1:1 mapping with a native event, at least for now.
const topLevelTypeStr: string = ((topLevelType: any): string);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we name this somehow to make it clearer this is opt-in and that RN does not collect any telemetry by default? I can absolutely see someone “finding” it in code and writing an HN article about it. Same goes for the comment.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggestion: RawEventTelemetryOffByDefault :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is an extremely valid concern.

JoshuaGross marked this conversation as resolved.
Show resolved Hide resolved
const event = {eventName: topLevelTypeStr, nativeEvent};
RawEventTelemetryEventEmitterOffByDefault.emit(topLevelTypeStr, event);
RawEventTelemetryEventEmitterOffByDefault.emit('*', event);

// Heritage plugin event system
runExtractedPluginEventsInBatch(
topLevelType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

// See the react-native repository for a full implementation.
// However, this is just an EventEmitter.
const RawEventTelemetryEventEmitterOffByDefault = {
emit: jest.fn(),
};

module.exports = {default: RawEventTelemetryEventEmitterOffByDefault};
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ module.exports = {
get legacySendAccessibilityEvent() {
return require('./legacySendAccessibilityEvent');
},
get RawEventTelemetryEventEmitterOffByDefault() {
return require('./RawEventTelemetryEventEmitterOffByDefault').default;
},
};
11 changes: 11 additions & 0 deletions scripts/flow/react-native-host-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ import type {CapturedError} from 'react-reconciler/src/ReactCapturedValue';
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';

type DeepDifferOptions = {|+unsafelyIgnoreFunctions?: boolean|};
type RawEventTelemetryEvent = $ReadOnly<{|
eventName: string,
// We expect, but do not/cannot require, that nativeEvent is an object
// with the properties: key, elementType (string), type (string), tag (numeric),
// and a stateNode of the native element/Fiber the event was emitted to.
nativeEvent: {[string]: mixed},
|}>;

declare module 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface' {
declare export function deepDiffer(
Expand Down Expand Up @@ -127,6 +134,10 @@ declare module 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface'
get: (name: string) => ReactNativeBaseComponentViewConfig,
...
};
declare export var RawEventTelemetryEventEmitterOffByDefault: {
emit: (channel: string, event: RawEventTelemetryEvent) => string,
...
};
}

declare module 'react-native/Libraries/ReactPrivate/ReactNativePrivateInitializeCore' {
Expand Down