first commit

This commit is contained in:
2026-03-10 16:18:05 +00:00
commit 11f9c069b5
31635 changed files with 3187747 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import EventEmitter, {
EmitterSubscription,
} from '../vendor/emitter/EventEmitter';
/**
* The React Native implementation of the IOS RCTEventEmitter which is required when creating
* a module that communicates with IOS
*/
type NativeModule = {
/**
* Add the provided eventType as an active listener
* @param eventType name of the event for which we are registering listener
*/
addListener: (eventType: string) => void;
/**
* Remove a specified number of events. There are no eventTypes in this case, as
* the native side doesn't remove the name, but only manages a counter of total
* listeners
* @param count number of listeners to remove (of any type)
*/
removeListeners: (count: number) => void;
};
/**
* Abstract base class for implementing event-emitting modules. This implements
* a subset of the standard EventEmitter node module API.
*/
declare class NativeEventEmitter extends EventEmitter {
/**
* @param nativeModule the NativeModule implementation. This is required on IOS and will throw
* an invariant error if undefined.
*/
constructor(nativeModule?: NativeModule);
/**
* Add the specified listener, this call passes through to the NativeModule
* addListener
*
* @param eventType name of the event for which we are registering listener
* @param listener the listener function
* @param context context of the listener
*/
addListener(
eventType: string,
listener: (event: any) => void,
context?: Object,
): EmitterSubscription;
/**
* @param eventType name of the event whose registered listeners to remove
*/
removeAllListeners(eventType: string): void;
}

View File

@@ -0,0 +1,133 @@
/**
* Copyright (c) Meta Platforms, Inc. and 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
*/
'use strict';
import type {
EventSubscription,
IEventEmitter,
} from '../vendor/emitter/EventEmitter';
import Platform from '../Utilities/Platform';
import RCTDeviceEventEmitter from './RCTDeviceEventEmitter';
import invariant from 'invariant';
interface NativeModule {
addListener(eventType: string): void;
removeListeners(count: number): void;
}
/** @deprecated Use `EventSubscription` instead. */
type EmitterSubscription = EventSubscription;
export type {EventSubscription, EmitterSubscription};
/** @deprecated Use `EventSubscription` instead. */
export type NativeEventSubscription = EventSubscription;
// $FlowFixMe[unclear-type] unclear type of events
type UnsafeNativeEventObject = Object;
/**
* `NativeEventEmitter` is intended for use by Native Modules to emit events to
* JavaScript listeners. If a `NativeModule` is supplied to the constructor, it
* will be notified (via `addListener` and `removeListeners`) when the listener
* count changes to manage "native memory".
*
* Currently, all native events are fired via a global `RCTDeviceEventEmitter`.
* 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: $ReadOnly<
Record<string, $ReadOnlyArray<UnsafeNativeEventObject>>,
> = $ReadOnly<Record<string, $ReadOnlyArray<UnsafeNativeEventObject>>>,
> implements IEventEmitter<TEventToArgsMap>
{
_nativeModule: ?NativeModule;
constructor(nativeModule?: ?NativeModule) {
if (Platform.OS === 'ios') {
invariant(
nativeModule != null,
'`new NativeEventEmitter()` requires a non-null argument.',
);
}
const hasAddListener =
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
!!nativeModule && typeof nativeModule.addListener === 'function';
const hasRemoveListeners =
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
!!nativeModule && typeof nativeModule.removeListeners === 'function';
if (nativeModule && hasAddListener && hasRemoveListeners) {
this._nativeModule = nativeModule;
} else if (nativeModule != null) {
if (!hasAddListener) {
console.warn(
'`new NativeEventEmitter()` was called with a non-null argument without the required `addListener` method.',
);
}
if (!hasRemoveListeners) {
console.warn(
'`new NativeEventEmitter()` was called with a non-null argument without the required `removeListeners` method.',
);
}
}
}
addListener<TEvent: $Keys<TEventToArgsMap>>(
eventType: TEvent,
listener: (...args: TEventToArgsMap[TEvent]) => mixed,
context?: mixed,
): EventSubscription {
this._nativeModule?.addListener(eventType);
let subscription: ?EventSubscription = RCTDeviceEventEmitter.addListener(
eventType,
listener,
context,
);
return {
remove: () => {
if (subscription != null) {
this._nativeModule?.removeListeners(1);
// $FlowFixMe[incompatible-use]
subscription.remove();
subscription = null;
}
},
};
}
emit<TEvent: $Keys<TEventToArgsMap>>(
eventType: TEvent,
...args: TEventToArgsMap[TEvent]
): void {
// Generally, `RCTDeviceEventEmitter` is directly invoked. But this is
// included for completeness.
RCTDeviceEventEmitter.emit(eventType, ...args);
}
removeAllListeners<TEvent: $Keys<TEventToArgsMap>>(
eventType?: ?TEvent,
): void {
invariant(
eventType != null,
'`NativeEventEmitter.removeAllListener()` requires a non-null argument.',
);
this._nativeModule?.removeListeners(this.listenerCount(eventType));
RCTDeviceEventEmitter.removeAllListeners(eventType);
}
listenerCount<TEvent: $Keys<TEventToArgsMap>>(eventType: TEvent): number {
return RCTDeviceEventEmitter.listenerCount(eventType);
}
}

View File

@@ -0,0 +1,29 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import EventEmitter, {
EmitterSubscription,
EventSubscriptionVendor,
} from '../vendor/emitter/EventEmitter';
/**
* Deprecated - subclass NativeEventEmitter to create granular event modules instead of
* adding all event listeners directly to RCTDeviceEventEmitter.
*/
interface DeviceEventEmitterStatic extends EventEmitter {
sharedSubscriber: EventSubscriptionVendor;
new (): DeviceEventEmitterStatic;
addListener(
type: string,
listener: (data: any) => void,
context?: any,
): EmitterSubscription;
}
export const DeviceEventEmitter: DeviceEventEmitterStatic;

View File

@@ -0,0 +1,48 @@
/**
* Copyright (c) Meta Platforms, Inc. and 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
* @format
*/
import type {IEventEmitter} from '../vendor/emitter/EventEmitter';
import {beginEvent, endEvent} from '../Performance/Systrace';
import EventEmitter from '../vendor/emitter/EventEmitter';
// FIXME: use typed events
/* $FlowFixMe[unclear-type] unclear type of events */
type RCTDeviceEventDefinitions = {[name: string]: Array<any>};
/**
* Global EventEmitter used by the native platform to emit events to JavaScript.
* Events are identified by globally unique event names.
*
* NativeModules that emit events should instead subclass `NativeEventEmitter`.
*/
class RCTDeviceEventEmitterImpl extends EventEmitter<RCTDeviceEventDefinitions> {
// Add systrace to RCTDeviceEventEmitter.emit method for debugging
emit<TEvent: $Keys<RCTDeviceEventDefinitions>>(
eventType: TEvent,
...args: RCTDeviceEventDefinitions[TEvent]
): void {
beginEvent(() => `RCTDeviceEventEmitter.emit#${eventType}`);
try {
super.emit(eventType, ...args);
} finally {
endEvent();
}
}
}
const RCTDeviceEventEmitter: IEventEmitter<RCTDeviceEventDefinitions> =
new RCTDeviceEventEmitterImpl();
Object.defineProperty(global, '__rctDeviceEventEmitter', {
configurable: true,
value: RCTDeviceEventEmitter,
});
export default (RCTDeviceEventEmitter: IEventEmitter<RCTDeviceEventDefinitions>);

View File

@@ -0,0 +1,21 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import registerCallableModule from '../Core/registerCallableModule';
const RCTEventEmitter = {
register(eventEmitter: any) {
registerCallableModule('RCTEventEmitter', eventEmitter);
},
};
export default RCTEventEmitter;

View File

@@ -0,0 +1,32 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {DeviceEventEmitterStatic} from './RCTDeviceEventEmitter';
export interface NativeEventSubscription {
/**
* Call this method to un-subscribe from a native-event
*/
remove(): void;
}
/**
* Receive events from native-code
* Deprecated - subclass NativeEventEmitter to create granular event modules instead of
* adding all event listeners directly to RCTNativeAppEventEmitter.
* @see https://github.com/facebook/react-native/blob/0.34-stable\Libraries\EventEmitter\RCTNativeAppEventEmitter.js
* @see https://reactnative.dev/docs/native-modules-ios#sending-events-to-javascript
*/
type RCTNativeAppEventEmitter = DeviceEventEmitterStatic;
/**
* Deprecated - subclass NativeEventEmitter to create granular event modules instead of
* adding all event listeners directly to RCTNativeAppEventEmitter.
*/
export const NativeAppEventEmitter: RCTNativeAppEventEmitter;

View File

@@ -0,0 +1,18 @@
/**
* Copyright (c) Meta Platforms, Inc. and 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 RCTDeviceEventEmitter from './RCTDeviceEventEmitter';
/**
* Deprecated - subclass NativeEventEmitter to create granular event modules instead of
* adding all event listeners directly to RCTNativeAppEventEmitter.
*/
const RCTNativeAppEventEmitter = RCTDeviceEventEmitter;
export default RCTNativeAppEventEmitter;