/
NativeEventEmitter.js
121 lines (108 loc) · 3.75 KB
/
NativeEventEmitter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* 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;
}
export type {EventSubscription};
/**
* `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: {...}>
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: $ElementType<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: $ElementType<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);
}
}