Skip to content

Commit c4c0065

Browse files
JoshuaGrossfacebook-github-bot
authored andcommitted
Add PressabilityPerformanceEventEmitter
Summary: Add PressabilityPerformanceEventEmitter which allows product code / infrastructure to subscribe to touch-related performance events. Changelog: [Added][JS] Product/infra can subscribe to Pressability touch events for telemetry purposes Reviewed By: fkgozali Differential Revision: D27034835 fbshipit-source-id: e62811f641994b9eadb5cdd7391e806b6cce479a
1 parent 9c19260 commit c4c0065

File tree

3 files changed

+74
-9
lines changed

3 files changed

+74
-9
lines changed

Libraries/Pressability/Pressability.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import type {
1818
PressEvent,
1919
MouseEvent,
2020
} from '../Types/CoreEventTypes';
21+
import PressabilityPerformanceEventEmitter from './PressabilityPerformanceEventEmitter.js';
22+
import {type PressabilityTouchSignal as TouchSignal} from './PressabilityTypes.js';
2123
import Platform from '../Utilities/Platform';
2224
import UIManager from '../ReactNative/UIManager';
2325
import type {HostComponent} from '../Renderer/shims/ReactNativeTypes';
@@ -174,15 +176,6 @@ type TouchState =
174176
| 'RESPONDER_ACTIVE_LONG_PRESS_OUT'
175177
| 'ERROR';
176178

177-
type TouchSignal =
178-
| 'DELAY'
179-
| 'RESPONDER_GRANT'
180-
| 'RESPONDER_RELEASE'
181-
| 'RESPONDER_TERMINATED'
182-
| 'ENTER_PRESS_RECT'
183-
| 'LEAVE_PRESS_RECT'
184-
| 'LONG_PRESS_DETECTED';
185-
186179
const Transitions = Object.freeze({
187180
NOT_RESPONDER: {
188181
DELAY: 'ERROR',
@@ -630,6 +623,13 @@ export default class Pressability {
630623
: '<<host component>>',
631624
);
632625
if (prevState !== nextState) {
626+
PressabilityPerformanceEventEmitter.emitEvent(() => {
627+
return {
628+
signal,
629+
touchDelayMs: Date.now() - event.nativeEvent.timestamp,
630+
};
631+
});
632+
633633
this._performTransitionSideEffects(prevState, nextState, signal, event);
634634
this._touchState = nextState;
635635
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import {type PressabilityTouchSignal as TouchSignal} from './PressabilityTypes.js';
12+
13+
export type PressabilityPerformanceEvent = $ReadOnly<{|
14+
signal: TouchSignal,
15+
touchDelayMs: number,
16+
|}>;
17+
export type PressabilityPerformanceEventListener = PressabilityPerformanceEvent => void;
18+
19+
class PressabilityPerformanceEventEmitter {
20+
_listeners: Array<PressabilityPerformanceEventListener> = [];
21+
22+
constructor() {}
23+
24+
addListener(listener: PressabilityPerformanceEventListener): void {
25+
this._listeners.push(listener);
26+
}
27+
28+
removeListener(listener: PressabilityPerformanceEventListener): void {
29+
const index = this._listeners.indexOf(listener);
30+
if (index > -1) {
31+
this._listeners.splice(index, 1);
32+
}
33+
}
34+
35+
emitEvent(constructEvent: () => PressabilityPerformanceEvent): void {
36+
if (this._listeners.length === 0) {
37+
return;
38+
}
39+
40+
const event = constructEvent();
41+
this._listeners.forEach(listener => listener(event));
42+
}
43+
}
44+
45+
const PressabilityPerformanceEventEmitterSingleton: PressabilityPerformanceEventEmitter = new PressabilityPerformanceEventEmitter();
46+
47+
export default PressabilityPerformanceEventEmitterSingleton;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
export type PressabilityTouchSignal =
12+
| 'DELAY'
13+
| 'RESPONDER_GRANT'
14+
| 'RESPONDER_RELEASE'
15+
| 'RESPONDER_TERMINATED'
16+
| 'ENTER_PRESS_RECT'
17+
| 'LEAVE_PRESS_RECT'
18+
| 'LONG_PRESS_DETECTED';

0 commit comments

Comments
 (0)