|
4 | 4 | * This source code is licensed under the MIT license found in the
|
5 | 5 | * LICENSE file in the root directory of this source tree.
|
6 | 6 | *
|
| 7 | + * @flow strict-local |
7 | 8 | * @format
|
8 |
| - * @flow |
9 | 9 | */
|
10 | 10 |
|
11 | 11 | 'use strict';
|
12 | 12 |
|
| 13 | +import { |
| 14 | + type EventSubscription, |
| 15 | + type IEventEmitter, |
| 16 | +} from '../vendor/emitter/EventEmitter'; |
13 | 17 | import Platform from '../Utilities/Platform';
|
14 |
| -import EventEmitter from '../vendor/emitter/EventEmitter'; |
15 |
| -import type EmitterSubscription from '../vendor/emitter/_EmitterSubscription'; |
16 | 18 | import RCTDeviceEventEmitter from './RCTDeviceEventEmitter';
|
17 | 19 | import invariant from 'invariant';
|
18 | 20 |
|
19 |
| -type NativeModule = { |
20 |
| - +addListener: (eventType: string) => void, |
21 |
| - +removeListeners: (count: number) => void, |
| 21 | +type NativeModule = $ReadOnly<{ |
| 22 | + addListener: (eventType: string) => void, |
| 23 | + removeListeners: (count: number) => void, |
22 | 24 | ...
|
23 |
| -}; |
| 25 | +}>; |
| 26 | + |
| 27 | +export type {EventSubscription}; |
24 | 28 |
|
25 | 29 | /**
|
26 |
| - * Abstract base class for implementing event-emitting modules. This implements |
27 |
| - * a subset of the standard EventEmitter node module API. |
| 30 | + * `NativeEventEmitter` is intended for use by Native Modules to emit events to |
| 31 | + * JavaScript listeners. If a `NativeModule` is supplied to the constructor, it |
| 32 | + * will be notified (via `addListener` and `removeListeners`) when the listener |
| 33 | + * count changes to manage "native memory". |
| 34 | + * |
| 35 | + * Currently, all native events are fired via a global `RCTDeviceEventEmitter`. |
| 36 | + * This means event names must be globally unique, and it means that call sites |
| 37 | + * can theoretically listen to `RCTDeviceEventEmitter` (although discouraged). |
28 | 38 | */
|
29 |
| -export default class NativeEventEmitter< |
30 |
| - EventDefinitions: {...}, |
31 |
| -> extends EventEmitter<EventDefinitions> { |
| 39 | +export default class NativeEventEmitter<TEventToArgsMap: {...}> |
| 40 | + implements IEventEmitter<TEventToArgsMap> { |
32 | 41 | _nativeModule: ?NativeModule;
|
33 | 42 |
|
34 | 43 | constructor(nativeModule: ?NativeModule) {
|
35 |
| - super(RCTDeviceEventEmitter.sharedSubscriber); |
36 | 44 | if (Platform.OS === 'ios') {
|
37 |
| - invariant(nativeModule, 'Native module cannot be null.'); |
| 45 | + invariant( |
| 46 | + nativeModule != null, |
| 47 | + '`new NativeEventEmitter()` requires a non-null argument.', |
| 48 | + ); |
38 | 49 | this._nativeModule = nativeModule;
|
39 | 50 | }
|
40 | 51 | }
|
41 | 52 |
|
42 |
| - addListener<K: $Keys<EventDefinitions>>( |
43 |
| - eventType: K, |
44 |
| - listener: (...$ElementType<EventDefinitions, K>) => mixed, |
45 |
| - context: $FlowFixMe, |
46 |
| - ): EmitterSubscription<EventDefinitions, K> { |
47 |
| - if (this._nativeModule != null) { |
48 |
| - this._nativeModule.addListener(eventType); |
49 |
| - } |
50 |
| - return super.addListener(eventType, listener, context); |
| 53 | + addListener<TEvent: $Keys<TEventToArgsMap>>( |
| 54 | + eventType: TEvent, |
| 55 | + listener: (...args: $ElementType<TEventToArgsMap, TEvent>) => mixed, |
| 56 | + context?: mixed, |
| 57 | + ): EventSubscription { |
| 58 | + this._nativeModule?.addListener(eventType); |
| 59 | + let subscription: ?EventSubscription = RCTDeviceEventEmitter.addListener( |
| 60 | + eventType, |
| 61 | + listener, |
| 62 | + context, |
| 63 | + ); |
| 64 | + |
| 65 | + return { |
| 66 | + remove: () => { |
| 67 | + if (subscription != null) { |
| 68 | + this._nativeModule?.removeListeners(1); |
| 69 | + subscription.remove(); |
| 70 | + subscription = null; |
| 71 | + } |
| 72 | + }, |
| 73 | + }; |
51 | 74 | }
|
52 | 75 |
|
53 |
| - removeAllListeners<K: $Keys<EventDefinitions>>(eventType: ?K): void { |
54 |
| - invariant(eventType, 'eventType argument is required.'); |
55 |
| - const count = this.listenerCount(eventType); |
56 |
| - if (this._nativeModule != null) { |
57 |
| - this._nativeModule.removeListeners(count); |
58 |
| - } |
59 |
| - super.removeAllListeners(eventType); |
| 76 | + emit<TEvent: $Keys<TEventToArgsMap>>( |
| 77 | + eventType: TEvent, |
| 78 | + ...args: $ElementType<TEventToArgsMap, TEvent> |
| 79 | + ): void { |
| 80 | + // Generally, `RCTDeviceEventEmitter` is directly invoked. But this is |
| 81 | + // included for completeness. |
| 82 | + RCTDeviceEventEmitter.emit(eventType, ...args); |
60 | 83 | }
|
61 | 84 |
|
62 |
| - removeSubscription<K: $Keys<EventDefinitions>>( |
63 |
| - subscription: EmitterSubscription<EventDefinitions, K>, |
| 85 | + removeAllListeners<TEvent: $Keys<TEventToArgsMap>>( |
| 86 | + eventType?: ?TEvent, |
64 | 87 | ): void {
|
65 |
| - if (this._nativeModule != null) { |
66 |
| - this._nativeModule.removeListeners(1); |
67 |
| - } |
68 |
| - super.removeSubscription(subscription); |
| 88 | + invariant( |
| 89 | + eventType != null, |
| 90 | + '`NativeEventEmitter.removeAllListener()` requires a non-null argument.', |
| 91 | + ); |
| 92 | + this._nativeModule?.removeListeners(this.listenerCount(eventType)); |
| 93 | + RCTDeviceEventEmitter.removeAllListeners(eventType); |
| 94 | + } |
| 95 | + |
| 96 | + listenerCount<TEvent: $Keys<TEventToArgsMap>>(eventType: TEvent): number { |
| 97 | + return RCTDeviceEventEmitter.listenerCount(eventType); |
69 | 98 | }
|
70 | 99 | }
|
0 commit comments