Skip to content

Commit

Permalink
Add PressabilityPerformanceEventEmitter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JoshuaGross authored and facebook-github-bot committed Mar 16, 2021
1 parent 9c19260 commit c4c0065
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
18 changes: 9 additions & 9 deletions Libraries/Pressability/Pressability.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import type {
PressEvent,
MouseEvent,
} from '../Types/CoreEventTypes';
import PressabilityPerformanceEventEmitter from './PressabilityPerformanceEventEmitter.js';
import {type PressabilityTouchSignal as TouchSignal} from './PressabilityTypes.js';
import Platform from '../Utilities/Platform';
import UIManager from '../ReactNative/UIManager';
import type {HostComponent} from '../Renderer/shims/ReactNativeTypes';
Expand Down Expand Up @@ -174,15 +176,6 @@ type TouchState =
| 'RESPONDER_ACTIVE_LONG_PRESS_OUT'
| 'ERROR';

type TouchSignal =
| 'DELAY'
| 'RESPONDER_GRANT'
| 'RESPONDER_RELEASE'
| 'RESPONDER_TERMINATED'
| 'ENTER_PRESS_RECT'
| 'LEAVE_PRESS_RECT'
| 'LONG_PRESS_DETECTED';

const Transitions = Object.freeze({
NOT_RESPONDER: {
DELAY: 'ERROR',
Expand Down Expand Up @@ -630,6 +623,13 @@ export default class Pressability {
: '<<host component>>',
);
if (prevState !== nextState) {
PressabilityPerformanceEventEmitter.emitEvent(() => {
return {
signal,
touchDelayMs: Date.now() - event.nativeEvent.timestamp,
};
});

this._performTransitionSideEffects(prevState, nextState, signal, event);
this._touchState = nextState;
}
Expand Down
47 changes: 47 additions & 0 deletions Libraries/Pressability/PressabilityPerformanceEventEmitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* 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.
*
* @flow strict-local
* @format
*/

import {type PressabilityTouchSignal as TouchSignal} from './PressabilityTypes.js';

export type PressabilityPerformanceEvent = $ReadOnly<{|
signal: TouchSignal,
touchDelayMs: number,
|}>;
export type PressabilityPerformanceEventListener = PressabilityPerformanceEvent => void;

class PressabilityPerformanceEventEmitter {
_listeners: Array<PressabilityPerformanceEventListener> = [];

constructor() {}

addListener(listener: PressabilityPerformanceEventListener): void {
this._listeners.push(listener);
}

removeListener(listener: PressabilityPerformanceEventListener): void {
const index = this._listeners.indexOf(listener);
if (index > -1) {
this._listeners.splice(index, 1);
}
}

emitEvent(constructEvent: () => PressabilityPerformanceEvent): void {
if (this._listeners.length === 0) {
return;
}

const event = constructEvent();
this._listeners.forEach(listener => listener(event));
}
}

const PressabilityPerformanceEventEmitterSingleton: PressabilityPerformanceEventEmitter = new PressabilityPerformanceEventEmitter();

export default PressabilityPerformanceEventEmitterSingleton;
18 changes: 18 additions & 0 deletions Libraries/Pressability/PressabilityTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* 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.
*
* @flow strict-local
* @format
*/

export type PressabilityTouchSignal =
| 'DELAY'
| 'RESPONDER_GRANT'
| 'RESPONDER_RELEASE'
| 'RESPONDER_TERMINATED'
| 'ENTER_PRESS_RECT'
| 'LEAVE_PRESS_RECT'
| 'LONG_PRESS_DETECTED';

0 comments on commit c4c0065

Please sign in to comment.