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

Update RCTDeviceEventEmitter singleton instantiation #49085

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @format
*/

import type {EventSubscription} from '../../vendor/emitter/EventEmitter';
import type {EmitterSubscription} from '../../vendor/emitter/EventEmitter';
import type {PlatformConfig} from '../AnimatedPlatformConfig';
import type Animation, {EndCallback} from '../animations/Animation';
import type {InterpolationConfigType} from './AnimatedInterpolation';
Expand Down Expand Up @@ -86,7 +86,7 @@ function _executeAsAnimatedBatch(id: string, operation: () => void) {
*/
export default class AnimatedValue extends AnimatedWithChildren {
#listenerCount: number = 0;
#updateSubscription: ?EventSubscription = null;
#updateSubscription: ?EmitterSubscription = null;

_value: number;
_startingValue: number;
Expand Down Expand Up @@ -159,7 +159,7 @@ export default class AnimatedValue extends AnimatedWithChildren {
}
const nativeTag = this.__getNativeTag();
NativeAnimatedAPI.startListeningToAnimatedNodeValue(nativeTag);
const subscription: EventSubscription =
const subscription: EmitterSubscription =
NativeAnimatedHelper.nativeEventEmitter.addListener(
'onAnimatedValueUpdate',
data => {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/Libraries/Animated/useAnimatedProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @format
*/

import type {EventSubscription} from '../EventEmitter/NativeEventEmitter';
import type {EmitterSubscription} from '../EventEmitter/NativeEventEmitter';
import type {AnimatedPropsAllowlist} from './nodes/AnimatedProps';

import NativeAnimatedHelper from '../../src/private/animated/NativeAnimatedHelper';
Expand Down Expand Up @@ -67,7 +67,7 @@ export default function useAnimatedProps<TProps: {...}, TInstance>(
// If multiple components call `flushQueue`, the first one will flush the
// queue and subsequent ones will do nothing.
NativeAnimatedHelper.API.flushQueue();
let drivenAnimationEndedListener: ?EventSubscription = null;
let drivenAnimationEndedListener: ?EmitterSubscription = null;
if (node.__isNative) {
drivenAnimationEndedListener =
NativeAnimatedHelper.nativeEventEmitter.addListener(
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/Libraries/AppState/AppState.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';
import logError from '../Utilities/logError';
import Platform from '../Utilities/Platform';
import {type EventSubscription} from '../vendor/emitter/EventEmitter';
import {type EmitterSubscription} from '../vendor/emitter/EventEmitter';
import NativeAppState from './NativeAppState';

export type AppStateValues = 'inactive' | 'background' | 'active';
Expand Down Expand Up @@ -92,7 +92,7 @@ class AppState {
addEventListener<K: $Keys<AppStateEventDefinitions>>(
type: K,
handler: (...$ElementType<AppStateEventDefinitions, K>) => void,
): EventSubscription {
): EmitterSubscription {
const emitter = this._emitter;
if (emitter == null) {
throw new Error('Cannot use AppState when `isAvailable` is false.');
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native/Libraries/BugReporting/BugReporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter';
import NativeRedBox from '../NativeModules/specs/NativeRedBox';
import {type EventSubscription} from '../vendor/emitter/EventEmitter';
import {type EmitterSubscription} from '../vendor/emitter/EventEmitter';
import NativeBugReporting from './NativeBugReporting';

type ExtraData = {[key: string]: string, ...};
Expand All @@ -36,8 +36,8 @@ function defaultExtras() {
class BugReporting {
static _extraSources: Map<string, SourceCallback> = new Map();
static _fileSources: Map<string, SourceCallback> = new Map();
static _subscription: ?EventSubscription = null;
static _redboxSubscription: ?EventSubscription = null;
static _subscription: ?EmitterSubscription = null;
static _redboxSubscription: ?EmitterSubscription = null;

static _maybeInit() {
if (!BugReporting._subscription) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import type {HostInstance} from '../../Renderer/shims/ReactNativeTypes';
import type {EventSubscription} from '../../vendor/emitter/EventEmitter';
import type {EmitterSubscription} from '../../vendor/emitter/EventEmitter';

import RCTDeviceEventEmitter from '../../EventEmitter/RCTDeviceEventEmitter';
import {sendAccessibilityEvent} from '../../ReactNative/RendererProxy';
Expand Down Expand Up @@ -398,7 +398,7 @@ const AccessibilityInfo = {
eventName: K,
// $FlowIssue[incompatible-type] - Flow bug with unions and generics (T128099423)
handler: (...$ElementType<AccessibilityEventDefinitions, K>) => void,
): EventSubscription {
): EmitterSubscription {
const deviceEventName = EventNames.get(eventName);
return deviceEventName == null
? {remove(): void {}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @flow strict-local
*/

import type {EventSubscription} from '../../vendor/emitter/EventEmitter';
import type {EmitterSubscription} from '../../vendor/emitter/EventEmitter';

import NativeEventEmitter from '../../EventEmitter/NativeEventEmitter';
import LayoutAnimation from '../../LayoutAnimation/LayoutAnimation';
Expand Down Expand Up @@ -150,7 +150,7 @@ class Keyboard {
eventType: K,
listener: (...$ElementType<KeyboardEventDefinitions, K>) => mixed,
context?: mixed,
): EventSubscription {
): EmitterSubscription {
return this._emitter.addListener(eventType, listener);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type {KeyboardEvent, KeyboardMetrics} from './Keyboard';
import LayoutAnimation from '../../LayoutAnimation/LayoutAnimation';
import StyleSheet from '../../StyleSheet/StyleSheet';
import Platform from '../../Utilities/Platform';
import {type EventSubscription} from '../../vendor/emitter/EventEmitter';
import {type EmitterSubscription} from '../../vendor/emitter/EventEmitter';
import AccessibilityInfo from '../AccessibilityInfo/AccessibilityInfo';
import View from '../View/View';
import Keyboard from './Keyboard';
Expand Down Expand Up @@ -62,7 +62,7 @@ type State = {
class KeyboardAvoidingView extends React.Component<Props, State> {
_frame: ?ViewLayout = null;
_keyboardEvent: ?KeyboardEvent = null;
_subscriptions: Array<EventSubscription> = [];
_subscriptions: Array<EmitterSubscription> = [];
viewRef: {current: React.ElementRef<typeof View> | null, ...};
_initialFrameHeight: number = 0;
_bottom: number = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
PressEvent,
ScrollEvent,
} from '../../Types/CoreEventTypes';
import type {EventSubscription} from '../../vendor/emitter/EventEmitter';
import type {EmitterSubscription} from '../../vendor/emitter/EventEmitter';
import type {KeyboardEvent, KeyboardMetrics} from '../Keyboard/Keyboard';
import typeof View from '../View/View';
import type {ViewProps} from '../View/ViewPropTypes';
Expand Down Expand Up @@ -735,10 +735,10 @@ class ScrollView extends React.Component<Props, State> {

_animated: ?boolean = null;

_subscriptionKeyboardWillShow: ?EventSubscription = null;
_subscriptionKeyboardWillHide: ?EventSubscription = null;
_subscriptionKeyboardDidShow: ?EventSubscription = null;
_subscriptionKeyboardDidHide: ?EventSubscription = null;
_subscriptionKeyboardWillShow: ?EmitterSubscription = null;
_subscriptionKeyboardWillHide: ?EmitterSubscription = null;
_subscriptionKeyboardDidShow: ?EmitterSubscription = null;
_subscriptionKeyboardDidHide: ?EmitterSubscription = null;

state: State = {
layoutHeight: null,
Expand Down
18 changes: 12 additions & 6 deletions packages/react-native/Libraries/EventEmitter/NativeEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'use strict';

import type {
EventSubscription,
EmitterSubscription,
IEventEmitter,
} from '../vendor/emitter/EventEmitter';

Expand All @@ -24,7 +24,10 @@ interface NativeModule {
removeListeners(count: number): void;
}

export type {EventSubscription};
export type {EmitterSubscription};

// $FlowFixMe[unclear-type] unclear type of events
type UnsafeObject = Object;

/**
* `NativeEventEmitter` is intended for use by Native Modules to emit events to
Expand All @@ -36,8 +39,11 @@ export type {EventSubscription};
* This means event names must be globally unique, and it means that call sites
* can theoretically listen to `RCTDeviceEventEmitter` (although discouraged).
*/
export default class NativeEventEmitter<TEventToArgsMap: {...}>
implements IEventEmitter<TEventToArgsMap>
export default class NativeEventEmitter<
TEventToArgsMap: $ReadOnly<
Record<string, $ReadOnlyArray<UnsafeObject>>,
> = $ReadOnly<Record<string, $ReadOnlyArray<UnsafeObject>>>,
> implements IEventEmitter<TEventToArgsMap>
{
_nativeModule: ?NativeModule;

Expand Down Expand Up @@ -76,9 +82,9 @@ export default class NativeEventEmitter<TEventToArgsMap: {...}>
eventType: TEvent,
listener: (...args: $ElementType<TEventToArgsMap, TEvent>) => mixed,
context?: mixed,
): EventSubscription {
): EmitterSubscription {
this._nativeModule?.addListener(eventType);
let subscription: ?EventSubscription = RCTDeviceEventEmitter.addListener(
let subscription: ?EmitterSubscription = RCTDeviceEventEmitter.addListener(
eventType,
listener,
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type RCTDeviceEventDefinitions = {[name: string]: Array<any>};
*
* NativeModules that emit events should instead subclass `NativeEventEmitter`.
*/
class RCTDeviceEventEmitter extends EventEmitter<RCTDeviceEventDefinitions> {
class RCTDeviceEventEmitterImpl extends EventEmitter<RCTDeviceEventDefinitions> {
// Add systrace to RCTDeviceEventEmitter.emit method for debugging
emit<TEvent: $Keys<RCTDeviceEventDefinitions>>(
eventType: TEvent,
Expand All @@ -34,11 +34,12 @@ class RCTDeviceEventEmitter extends EventEmitter<RCTDeviceEventDefinitions> {
endEvent();
}
}
const instance = new RCTDeviceEventEmitter();
const RCTDeviceEventEmitter: IEventEmitter<RCTDeviceEventDefinitions> =
new RCTDeviceEventEmitterImpl();

Object.defineProperty(global, '__rctDeviceEventEmitter', {
configurable: true,
value: instance,
value: RCTDeviceEventEmitter,
});

export default (instance: IEventEmitter<RCTDeviceEventDefinitions>);
export default (RCTDeviceEventEmitter: IEventEmitter<RCTDeviceEventDefinitions>);
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import type {
EventSubscription,
EmitterSubscription,
IEventEmitter,
} from '../../vendor/emitter/EventEmitter';

Expand All @@ -18,14 +18,15 @@ import RCTDeviceEventEmitter from '../RCTDeviceEventEmitter';
/**
* Mock `NativeEventEmitter` to ignore Native Modules.
*/
export default class NativeEventEmitter<TEventToArgsMap: {...}>
implements IEventEmitter<TEventToArgsMap>
export default class NativeEventEmitter<
TEventToArgsMap: $ReadOnly<Record<string, $ReadOnlyArray<mixed>>>,
> implements IEventEmitter<TEventToArgsMap>
{
addListener<TEvent: $Keys<TEventToArgsMap>>(
eventType: TEvent,
listener: (...args: $ElementType<TEventToArgsMap, TEvent>) => mixed,
context?: mixed,
): EventSubscription {
): EmitterSubscription {
return RCTDeviceEventEmitter.addListener(eventType, listener, context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @format
*/

import type {EventSubscription} from '../vendor/emitter/EventEmitter';
import type {EmitterSubscription} from '../vendor/emitter/EventEmitter';

const invariant = require('invariant');

Expand Down Expand Up @@ -155,7 +155,7 @@ const InteractionManagerStub = {
/**
* @deprecated
*/
addListener(): EventSubscription {
addListener(): EmitterSubscription {
return {
remove() {},
};
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/Libraries/Linking/Linking.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @flow strict-local
*/

import type {EventSubscription} from '../vendor/emitter/EventEmitter';
import type {EmitterSubscription} from '../vendor/emitter/EventEmitter';

import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';
import Platform from '../Utilities/Platform';
Expand All @@ -35,7 +35,7 @@ class LinkingImpl extends NativeEventEmitter<LinkingEventDefinitions> {
addEventListener<K: $Keys<LinkingEventDefinitions>>(
eventType: K,
listener: (...$ElementType<LinkingEventDefinitions, K>) => mixed,
): EventSubscription {
): EmitterSubscription {
return this.addListener(eventType, listener);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/Libraries/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {RootTag} from '../ReactNative/RootTag';
import type {DirectEventHandler} from '../Types/CodegenTypes';

import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';
import {type EventSubscription} from '../vendor/emitter/EventEmitter';
import {type EmitterSubscription} from '../vendor/emitter/EventEmitter';
import ModalInjection from './ModalInjection';
import NativeModalManager from './NativeModalManager';
import RCTModalHostView from './RCTModalHostViewNativeComponent';
Expand Down Expand Up @@ -210,7 +210,7 @@ class Modal extends React.Component<Props, State> {
static contextType: React.Context<RootTag> = RootTagContext;

_identifier: number;
_eventSubscription: ?EventSubscription;
_eventSubscription: ?EmitterSubscription;

constructor(props: Props) {
super(props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @flow
*/

import type {EventSubscription} from '../vendor/emitter/EventEmitter';
import type {EmitterSubscription} from '../vendor/emitter/EventEmitter';
import type {RequestBody} from './convertRequestBody';
import type {RCTNetworkingEventDefinitions} from './RCTNetworkingEventDefinitions.flow';
import type {NativeResponseType} from './XMLHttpRequest';
Expand Down Expand Up @@ -52,7 +52,7 @@ const RCTNetworking = {
eventType: K,
listener: (...$ElementType<RCTNetworkingEventDefinitions, K>) => mixed,
context?: mixed,
): EventSubscription {
): EmitterSubscription {
// $FlowFixMe[incompatible-call]
return emitter.addListener(eventType, listener, context);
},
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/Libraries/Network/RCTNetworking.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'use strict';

import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter';
import {type EventSubscription} from '../vendor/emitter/EventEmitter';
import {type EmitterSubscription} from '../vendor/emitter/EventEmitter';
import convertRequestBody, {type RequestBody} from './convertRequestBody';
import NativeNetworkingIOS from './NativeNetworkingIOS';
import {type RCTNetworkingEventDefinitions} from './RCTNetworkingEventDefinitions.flow';
Expand All @@ -22,7 +22,7 @@ const RCTNetworking = {
eventType: K,
listener: (...$ElementType<RCTNetworkingEventDefinitions, K>) => mixed,
context?: mixed,
): EventSubscription {
): EmitterSubscription {
// $FlowFixMe[incompatible-call]
return RCTDeviceEventEmitter.addListener(eventType, listener, context);
},
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/Libraries/Network/RCTNetworking.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

import type {EventSubscription} from '../vendor/emitter/EventEmitter';
import type {EmitterSubscription} from '../vendor/emitter/EventEmitter';
import type {RequestBody} from './convertRequestBody';
import type {RCTNetworkingEventDefinitions} from './RCTNetworkingEventDefinitions.flow';
import type {NativeResponseType} from './XMLHttpRequest';
Expand All @@ -21,7 +21,7 @@ declare const RCTNetworking: interface {
// $FlowFixMe[invalid-computed-prop]
listener: (...$ElementType<RCTNetworkingEventDefinitions, K>) => mixed,
context?: mixed,
): EventSubscription,
): EmitterSubscription,

sendRequest(
method: string,
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/Libraries/Network/XMLHttpRequest_new.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import EventTarget from '../../src/private/webapis/dom/events/EventTarget';
import {dispatchTrustedEvent} from '../../src/private/webapis/dom/events/internals/EventTargetInternals';
import ProgressEvent from '../../src/private/webapis/xhr/events/ProgressEvent';
import {type EventSubscription} from '../vendor/emitter/EventEmitter';
import {type EmitterSubscription} from '../vendor/emitter/EventEmitter';

const BlobManager = require('../Blob/BlobManager').default;
const GlobalPerformanceLogger = require('../Utilities/GlobalPerformanceLogger');
Expand Down Expand Up @@ -138,7 +138,7 @@ class XMLHttpRequest extends EventTarget {
upload: XMLHttpRequestEventTarget = new XMLHttpRequestEventTarget();

_requestId: ?number;
_subscriptions: Array<EventSubscription>;
_subscriptions: Array<EmitterSubscription>;

_aborted: boolean = false;
_cachedResponse: Response;
Expand Down
Loading
Loading