@100mslive/react-native-hms 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/android/src/main/java/com/reactnativehmssdk/HMSManager.kt +52 -24
  2. package/android/src/main/java/com/reactnativehmssdk/HMSRNSDK.kt +245 -62
  3. package/android/src/main/java/com/reactnativehmssdk/HMSView.kt +15 -6
  4. package/ios/HMSConstants.swift +3 -1
  5. package/ios/HMSDecoder.swift +1 -1
  6. package/ios/HMSHelper.swift +44 -7
  7. package/ios/HMSManager.m +8 -0
  8. package/ios/HMSManager.swift +35 -1
  9. package/ios/HMSRNSDK.swift +268 -38
  10. package/lib/commonjs/classes/HMSAudioTrackSettings.js +14 -0
  11. package/lib/commonjs/classes/HMSAudioTrackSettings.js.map +1 -1
  12. package/lib/commonjs/classes/HMSEncoder.js +2 -1
  13. package/lib/commonjs/classes/HMSEncoder.js.map +1 -1
  14. package/lib/commonjs/classes/HMSIOSAudioMode.js +13 -0
  15. package/lib/commonjs/classes/HMSIOSAudioMode.js.map +1 -0
  16. package/lib/commonjs/classes/HMSSDK.js +50 -0
  17. package/lib/commonjs/classes/HMSSDK.js.map +1 -1
  18. package/lib/commonjs/classes/HMSSessionStore.js +173 -0
  19. package/lib/commonjs/classes/HMSSessionStore.js.map +1 -0
  20. package/lib/commonjs/classes/HMSUpdateListenerActions.js +8 -0
  21. package/lib/commonjs/classes/HMSUpdateListenerActions.js.map +1 -1
  22. package/lib/commonjs/classes/HMSVideoTrackSettings.js +5 -0
  23. package/lib/commonjs/classes/HMSVideoTrackSettings.js.map +1 -1
  24. package/lib/commonjs/index.js +12 -0
  25. package/lib/commonjs/index.js.map +1 -1
  26. package/lib/commonjs/utils/emitter/EventEmitter.js +162 -0
  27. package/lib/commonjs/utils/emitter/EventEmitter.js.map +1 -0
  28. package/lib/commonjs/utils/emitter/_EmitterSubscription.js +46 -0
  29. package/lib/commonjs/utils/emitter/_EmitterSubscription.js.map +1 -0
  30. package/lib/commonjs/utils/emitter/_EventSubscription.js +36 -0
  31. package/lib/commonjs/utils/emitter/_EventSubscription.js.map +1 -0
  32. package/lib/commonjs/utils/emitter/_EventSubscriptionVendor.js +90 -0
  33. package/lib/commonjs/utils/emitter/_EventSubscriptionVendor.js.map +1 -0
  34. package/lib/commonjs/utils/index.js +17 -0
  35. package/lib/commonjs/utils/index.js.map +1 -0
  36. package/lib/module/classes/HMSAudioTrackSettings.js +14 -0
  37. package/lib/module/classes/HMSAudioTrackSettings.js.map +1 -1
  38. package/lib/module/classes/HMSEncoder.js +2 -1
  39. package/lib/module/classes/HMSEncoder.js.map +1 -1
  40. package/lib/module/classes/HMSIOSAudioMode.js +6 -0
  41. package/lib/module/classes/HMSIOSAudioMode.js.map +1 -0
  42. package/lib/module/classes/HMSSDK.js +50 -0
  43. package/lib/module/classes/HMSSDK.js.map +1 -1
  44. package/lib/module/classes/HMSSessionStore.js +166 -0
  45. package/lib/module/classes/HMSSessionStore.js.map +1 -0
  46. package/lib/module/classes/HMSUpdateListenerActions.js +8 -0
  47. package/lib/module/classes/HMSUpdateListenerActions.js.map +1 -1
  48. package/lib/module/classes/HMSVideoTrackSettings.js +5 -0
  49. package/lib/module/classes/HMSVideoTrackSettings.js.map +1 -1
  50. package/lib/module/index.js +1 -0
  51. package/lib/module/index.js.map +1 -1
  52. package/lib/module/utils/emitter/EventEmitter.js +151 -0
  53. package/lib/module/utils/emitter/EventEmitter.js.map +1 -0
  54. package/lib/module/utils/emitter/_EmitterSubscription.js +39 -0
  55. package/lib/module/utils/emitter/_EmitterSubscription.js.map +1 -0
  56. package/lib/module/utils/emitter/_EventSubscription.js +29 -0
  57. package/lib/module/utils/emitter/_EventSubscription.js.map +1 -0
  58. package/lib/module/utils/emitter/_EventSubscriptionVendor.js +83 -0
  59. package/lib/module/utils/emitter/_EventSubscriptionVendor.js.map +1 -0
  60. package/lib/module/utils/index.js +2 -0
  61. package/lib/module/utils/index.js.map +1 -0
  62. package/lib/typescript/classes/HMSAudioTrackSettings.d.ts +14 -0
  63. package/lib/typescript/classes/HMSIOSAudioMode.d.ts +4 -0
  64. package/lib/typescript/classes/HMSSDK.d.ts +16 -0
  65. package/lib/typescript/classes/HMSSessionStore.d.ts +63 -0
  66. package/lib/typescript/classes/HMSUpdateListenerActions.d.ts +9 -1
  67. package/lib/typescript/classes/HMSVideoTrackSettings.d.ts +5 -0
  68. package/lib/typescript/index.d.ts +2 -0
  69. package/lib/typescript/utils/emitter/EventEmitter.d.ts +91 -0
  70. package/lib/typescript/utils/emitter/_EmitterSubscription.d.ts +29 -0
  71. package/lib/typescript/utils/emitter/_EventSubscription.d.ts +19 -0
  72. package/lib/typescript/utils/emitter/_EventSubscriptionVendor.d.ts +44 -0
  73. package/lib/typescript/utils/index.d.ts +1 -0
  74. package/package.json +21 -2
  75. package/sdk-versions.json +2 -2
  76. package/src/classes/HMSAudioTrackSettings.ts +16 -0
  77. package/src/classes/HMSEncoder.ts +1 -0
  78. package/src/classes/HMSIOSAudioMode.ts +4 -0
  79. package/src/classes/HMSSDK.tsx +70 -4
  80. package/src/classes/HMSSessionStore.ts +209 -0
  81. package/src/classes/HMSUpdateListenerActions.ts +8 -0
  82. package/src/classes/HMSVideoTrackSettings.ts +5 -0
  83. package/src/index.ts +5 -0
  84. package/src/utils/emitter/EventEmitter.ts +160 -0
  85. package/src/utils/emitter/_EmitterSubscription.ts +44 -0
  86. package/src/utils/emitter/_EventSubscription.ts +28 -0
  87. package/src/utils/emitter/_EventSubscriptionVendor.ts +89 -0
  88. package/src/utils/index.ts +1 -0
@@ -0,0 +1,209 @@
1
+ import {
2
+ NativeModules,
3
+ DeviceEventEmitter,
4
+ EmitterSubscription as RNEmitterSubscription,
5
+ } from 'react-native';
6
+ import { HMSConstants } from './HMSConstants';
7
+ import { getLogger } from './HMSLogger';
8
+ import { HMSUpdateListenerActions } from './HMSUpdateListenerActions';
9
+ import { EventEmitter } from '../utils';
10
+ import type { EmitterSubscription } from '../utils';
11
+
12
+ const { HMSManager } = NativeModules;
13
+
14
+ type Nullable<T> = T | null | undefined;
15
+
16
+ export type HMSSessionStoreValue = Nullable<string>;
17
+
18
+ /**
19
+ * Session store is a shared realtime key-value store that is accessible by everyone in the room.
20
+ * It can be utilized to implement features such as pinned text, spotlight (which brings a particular
21
+ * peer to the center stage for everyone in the room) and more.
22
+ *
23
+ * To get an instance of `HMSSessionStore` class, You can add an event listener for `ON_SESSION_STORE_AVAILABLE`
24
+ * event on the `HMSSDK` instance
25
+ *
26
+ * For example:
27
+ * ```
28
+ * hmsInstance.addEventListener(HMSUpdateListenerActions.ON_SESSION_STORE_AVAILABLE, <your callback function>);
29
+ * ```
30
+ *
31
+ * Checkout Session Store docs fore more details ${@link https://www.100ms.live/docs/react-native/v2/how-to-guides/interact-with-room/room/session-store}
32
+ */
33
+ export class HMSSessionStore {
34
+ private _deviceEventEmitterSubscription?: RNEmitterSubscription;
35
+ private _eventEmitter?: EventEmitter;
36
+ private _addedKeyChangeListenerCount = 0;
37
+
38
+ /**
39
+ * This method sets a value for a specific key on session store.
40
+ * Once a value is assigned, it will be available for other peers in the room
41
+ * who are listening for changes in value for that specific key.
42
+ *
43
+ * @param {HMSSessionStoreValue} value
44
+ * @param {string} key
45
+ * @returns {Promise}
46
+ */
47
+ async set(value: HMSSessionStoreValue, key: string) {
48
+ const data: { success: true; finalValue: HMSSessionStoreValue } =
49
+ await HMSManager.setSessionMetadataForKey({
50
+ id: HMSConstants.DEFAULT_SDK_ID,
51
+ key,
52
+ value,
53
+ });
54
+ return data;
55
+ }
56
+
57
+ /**
58
+ * This method returns the value of any specified key on session store.
59
+ * Note that you will not get updates for any change in value of the specified key,
60
+ * It returns the latest value at the time it was called.
61
+ *
62
+ * To listen to value change updates use `addKeyChangeListener` method instead.
63
+ *
64
+ * @param {string} key
65
+ * @returns {Promise}
66
+ */
67
+ async get(key: string) {
68
+ const data: HMSSessionStoreValue =
69
+ await HMSManager.getSessionMetadataForKey({
70
+ id: HMSConstants.DEFAULT_SDK_ID,
71
+ key,
72
+ });
73
+ return data;
74
+ }
75
+
76
+ /**
77
+ * This method registers a callback function for listening to value changes of a particular key.
78
+ * Registered Callback function will be called initially with latest value and whenever the value updates
79
+ *
80
+ * @param {string[]} forKeys
81
+ * @param {Function} callback
82
+ * @returns {Object} subscription object
83
+ */
84
+ addKeyChangeListener<T extends string[]>(
85
+ forKeys: T,
86
+ callback: (
87
+ error: string | null,
88
+ data: { key: T[number]; value: HMSSessionStoreValue } | null
89
+ ) => void
90
+ ) {
91
+ // Add Native Device Event Emitter if it is not already added
92
+ if (!this._deviceEventEmitterSubscription) {
93
+ this._deviceEventEmitterSubscription = DeviceEventEmitter.addListener(
94
+ HMSUpdateListenerActions.ON_SESSION_STORE_CHANGED,
95
+ this._deviceEventEmitterListener.bind(this)
96
+ );
97
+ }
98
+
99
+ // Create JS side EventEmitter
100
+ if (!this._eventEmitter) {
101
+ this._eventEmitter = new EventEmitter();
102
+ }
103
+
104
+ // Unique Identifier for adding native event listener
105
+ const uniqueId = forKeys.join('') + '_' + Date.now().toString();
106
+
107
+ const eventEmitter = this._eventEmitter;
108
+
109
+ // Add listeners on eventEmitter for each key
110
+ const subscriptions = forKeys.map((key) =>
111
+ eventEmitter.addListener(key, callback, { uniqueId })
112
+ );
113
+
114
+ //
115
+ let cleanupHandler: (() => void) | null = () => {
116
+ this._cleanup(subscriptions);
117
+ };
118
+
119
+ // Adding 'KeyChangeListener' on native side
120
+ HMSManager.addKeyChangeListener({
121
+ id: HMSConstants.DEFAULT_SDK_ID,
122
+ keys: forKeys,
123
+ uniqueId,
124
+ })
125
+ // Adding 'KeyChangeListener' fails on native side
126
+ .catch((err: any) => {
127
+ if (typeof cleanupHandler === 'function') {
128
+ callback(err, null);
129
+ cleanupHandler();
130
+ cleanupHandler = null;
131
+ }
132
+ });
133
+
134
+ this._addedKeyChangeListenerCount += 1;
135
+
136
+ return {
137
+ remove: () => {
138
+ if (typeof cleanupHandler === 'function') {
139
+ cleanupHandler();
140
+ cleanupHandler = null;
141
+ }
142
+ },
143
+ };
144
+ }
145
+
146
+ private _cleanup(subscriptionsToRemove: EmitterSubscription[]) {
147
+ // Extracting `uniqueId` from first subscription (all subscriptions have same uniqueId)
148
+ // this `uniqueId` will be used to remove 'KeyChangeListener' from native side
149
+ const uniqueId =
150
+ subscriptionsToRemove.length > 0
151
+ ? (subscriptionsToRemove[0].context as { uniqueId: string }).uniqueId
152
+ : null;
153
+
154
+ // Removing required subscriptions from 'eventEmitter'
155
+ subscriptionsToRemove.forEach((subscription) => subscription.remove());
156
+
157
+ // Removing 'KeyChangeListener' from native side
158
+ HMSManager.removeKeyChangeListener({
159
+ id: HMSConstants.DEFAULT_SDK_ID,
160
+ uniqueId,
161
+ }).catch((error: any) => {
162
+ const logger = getLogger();
163
+ logger?.verbose(
164
+ "Error while removing key change listener, Listener didn't get registerred at first place or was already removed",
165
+ error
166
+ );
167
+ });
168
+
169
+ this._addedKeyChangeListenerCount -= 1;
170
+
171
+ if (this._addedKeyChangeListenerCount <= 0) {
172
+ this._addedKeyChangeListenerCount = 0;
173
+ if (
174
+ this._deviceEventEmitterSubscription &&
175
+ Object.getOwnPropertyNames(
176
+ this._deviceEventEmitterSubscription
177
+ ).includes('remove') &&
178
+ typeof this._deviceEventEmitterSubscription.remove === 'function'
179
+ ) {
180
+ this._deviceEventEmitterSubscription.remove();
181
+ } else {
182
+ DeviceEventEmitter.removeListener(
183
+ HMSUpdateListenerActions.ON_SESSION_STORE_CHANGED,
184
+ this._deviceEventEmitterListener
185
+ );
186
+ }
187
+
188
+ this._deviceEventEmitterSubscription = undefined;
189
+
190
+ this._eventEmitter = undefined;
191
+ }
192
+ }
193
+
194
+ private _deviceEventEmitterListener(data: {
195
+ id: string;
196
+ key: string;
197
+ value: HMSSessionStoreValue;
198
+ }) {
199
+ // if id is different from default sdk_id, return early
200
+ if (data.id !== HMSConstants.DEFAULT_SDK_ID) {
201
+ return;
202
+ }
203
+
204
+ // emit event for the key
205
+ getLogger()?.verbose('#Listener ON_SESSION_STORE_CHANGED event: ', data);
206
+
207
+ this._eventEmitter?.emit(data.key, null, data);
208
+ }
209
+ }
@@ -1,3 +1,9 @@
1
+ /**
2
+ * These are the available events emitted by the `HMSSDK`
3
+ *
4
+ * For more info about these events, checkout Event Listener docs
5
+ * {@link https://www.100ms.live/docs/react-native/v2/how-to-guides/listen-to-room-updates/event-listeners}
6
+ */
1
7
  export enum HMSUpdateListenerActions {
2
8
  ON_PREVIEW = 'ON_PREVIEW',
3
9
  ON_JOIN = 'ON_JOIN',
@@ -18,4 +24,6 @@ export enum HMSUpdateListenerActions {
18
24
  ON_REMOTE_AUDIO_STATS = 'ON_REMOTE_AUDIO_STATS',
19
25
  ON_REMOTE_VIDEO_STATS = 'ON_REMOTE_VIDEO_STATS',
20
26
  ON_AUDIO_DEVICE_CHANGED = 'ON_AUDIO_DEVICE_CHANGED',
27
+ ON_SESSION_STORE_AVAILABLE = 'ON_SESSION_STORE_AVAILABLE',
28
+ ON_SESSION_STORE_CHANGED = 'ON_SESSION_STORE_CHANGED',
21
29
  }
@@ -2,6 +2,11 @@ import type { HMSSimulcastLayerSettings } from './HMSSimulcastLayerSettings';
2
2
  import type { HMSCameraFacing } from './HMSCameraFacing';
3
3
  import type { HMSTrackSettingsInitState } from './HMSTrackSettingsInitState';
4
4
 
5
+ /**
6
+ * Customize local peer's Video track settings before Joining the Room.
7
+ *
8
+ * Checkout Track Settings docs for more details {@link https://www.100ms.live/docs/react-native/v2/how-to-guides/interact-with-room/track/track-settings}
9
+ */
5
10
  export class HMSVideoTrackSettings {
6
11
  readonly simulcastSettings?: HMSSimulcastLayerSettings[];
7
12
  initialState?: HMSTrackSettingsInitState;
package/src/index.ts CHANGED
@@ -80,6 +80,11 @@ export * from './classes/HMSSimulcastLayerDefinition';
80
80
  export * from './classes/HMSQualityLimitationReasons';
81
81
  export * from './classes/HMSQualityLimitationReason';
82
82
  export * from './classes/HMSCameraControl';
83
+ export * from './classes/HMSIOSAudioMode';
84
+ export type {
85
+ HMSSessionStore,
86
+ HMSSessionStoreValue,
87
+ } from './classes/HMSSessionStore';
83
88
  export type { HmsViewComponent as HMSView } from './classes/HmsView';
84
89
 
85
90
  import { HMSSDK as HmsManager } from './classes/HMSSDK';
@@ -0,0 +1,160 @@
1
+ import { EmitterSubscription } from './_EmitterSubscription';
2
+ import { EventSubscriptionVendor } from './_EventSubscriptionVendor';
3
+
4
+ const sparseFilterPredicate = () => true;
5
+
6
+ export { EmitterSubscription } from './_EmitterSubscription';
7
+
8
+ /**
9
+ * @class EventEmitter
10
+ * @description
11
+ * An EventEmitter is responsible for managing a set of listeners and publishing
12
+ * events to them when it is told that such events happened. In addition to the
13
+ * data for the given event it also sends a event control object which allows
14
+ * the listeners/handlers to prevent the default behavior of the given event.
15
+ *
16
+ * The emitter is designed to be generic enough to support all the different
17
+ * contexts in which one might want to emit events. It is a simple multicast
18
+ * mechanism on top of which extra functionality can be composed. For example, a
19
+ * more advanced emitter may use an EventHolder and EventFactory.
20
+ */
21
+ export class EventEmitter {
22
+ _subscriber: EventSubscriptionVendor;
23
+
24
+ /**
25
+ * @constructor
26
+ *
27
+ * @param {EventSubscriptionVendor} subscriber - Optional subscriber instance
28
+ * to use. If omitted, a new subscriber will be created for the emitter.
29
+ */
30
+ constructor(subscriber?: EventSubscriptionVendor | null) {
31
+ this._subscriber = subscriber || new EventSubscriptionVendor();
32
+ }
33
+
34
+ /**
35
+ * Adds a listener to be invoked when events of the specified type are
36
+ * emitted. An optional calling context may be provided. The data arguments
37
+ * emitted will be passed to the listener function.
38
+ *
39
+ * TODO: Annotate the listener arg's type. This is tricky because listeners
40
+ * can be invoked with varargs.
41
+ *
42
+ * @param {string} eventType - Name of the event to listen to
43
+ * @param {function} listener - Function to invoke when the specified event is
44
+ * emitted
45
+ * @param {*} context - Optional context object to use when invoking the
46
+ * listener
47
+ */
48
+ addListener(
49
+ eventType: string,
50
+ listener: Function,
51
+ context: Object | null | undefined
52
+ ): EmitterSubscription {
53
+ return this._subscriber.addSubscription(
54
+ eventType,
55
+ new EmitterSubscription(this, this._subscriber, listener, context)
56
+ ) as EmitterSubscription;
57
+ }
58
+
59
+ /**
60
+ * Removes all of the registered listeners, including those registered as
61
+ * listener maps.
62
+ *
63
+ * @param {?string} eventType - Optional name of the event whose registered
64
+ * listeners to remove
65
+ */
66
+ removeAllListeners(eventType: string | undefined | null) {
67
+ this._subscriber.removeAllSubscriptions(eventType);
68
+ }
69
+
70
+ /**
71
+ * Removes a specific subscription. Called by the `remove()` method of the
72
+ * subscription itself to ensure any necessary cleanup is performed.
73
+ */
74
+ removeSubscription(subscription: EmitterSubscription) {
75
+ if (subscription.emitter !== this) {
76
+ console.warn('Subscription does not belong to this emitter.'); // TODO: use HMSLogger here
77
+ return;
78
+ }
79
+
80
+ this._subscriber.removeSubscription(subscription);
81
+ }
82
+
83
+ /**
84
+ * Returns the number of listeners that are currently registered for the given
85
+ * event.
86
+ *
87
+ * @param {string} eventType - Name of the event to query
88
+ * @returns {number}
89
+ */
90
+ listenerCount(eventType: string): number {
91
+ const subscriptions = this._subscriber.getSubscriptionsForType(eventType);
92
+ return subscriptions
93
+ ? // We filter out missing entries because the array is sparse.
94
+ // "callbackfn is called only for elements of the array which actually
95
+ // exist; it is not called for missing elements of the array."
96
+ // https://www.ecma-international.org/ecma-262/9.0/index.html#sec-array.prototype.filter
97
+ subscriptions.filter(sparseFilterPredicate).length
98
+ : 0;
99
+ }
100
+
101
+ /**
102
+ * Emits an event of the given type with the given data. All handlers of that
103
+ * particular type will be notified.
104
+ *
105
+ * @param {string} eventType - Name of the event to emit
106
+ * @param {...*} Arbitrary arguments to be passed to each registered listener
107
+ *
108
+ * @example
109
+ * emitter.addListener('someEvent', function(message) {
110
+ * console.log(message);
111
+ * });
112
+ *
113
+ * emitter.emit('someEvent', 'abc'); // logs 'abc'
114
+ */
115
+ emit(eventType: string, ...args: any[]) {
116
+ const subscriptions = this._subscriber.getSubscriptionsForType(
117
+ eventType
118
+ ) as EmitterSubscription[] | null | undefined;
119
+ if (subscriptions) {
120
+ for (let i = 0, l = subscriptions.length; i < l; i++) {
121
+ const subscription = subscriptions[i];
122
+
123
+ // The subscription may have been removed during this event loop.
124
+ if (subscription && subscription.listener) {
125
+ subscription.listener.apply(subscription.context, args);
126
+ }
127
+ }
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Removes the given listener for event of specific type.
133
+ *
134
+ * @param {string} eventType - Name of the event to emit
135
+ * @param {function} listener - Function to invoke when the specified event is
136
+ * emitted
137
+ *
138
+ * @example
139
+ * emitter.removeListener('someEvent', function(message) {
140
+ * console.log(message);
141
+ * }); // removes the listener if already registered
142
+ *
143
+ */
144
+ removeListener(eventType: string, listener: Function) {
145
+ const subscriptions = this._subscriber.getSubscriptionsForType(
146
+ eventType
147
+ ) as EmitterSubscription[] | null | undefined;
148
+ if (subscriptions) {
149
+ for (let i = 0, l = subscriptions.length; i < l; i++) {
150
+ const subscription = subscriptions[i];
151
+
152
+ // The subscription may have been removed during this event loop.
153
+ // its listener matches the listener in method parameters
154
+ if (subscription && subscription.listener === listener) {
155
+ subscription.remove();
156
+ }
157
+ }
158
+ }
159
+ }
160
+ }
@@ -0,0 +1,44 @@
1
+ import { EventSubscription } from './_EventSubscription';
2
+ import type { EventEmitter } from './EventEmitter';
3
+ import type { EventSubscriptionVendor } from './_EventSubscriptionVendor';
4
+
5
+ /**
6
+ * EmitterSubscription represents a subscription with listener and context data.
7
+ */
8
+ export class EmitterSubscription extends EventSubscription {
9
+ emitter: EventEmitter;
10
+ listener: Function;
11
+ context: Object | null | undefined;
12
+
13
+ /**
14
+ * @param {EventEmitter} emitter - The event emitter that registered this
15
+ * subscription
16
+ * @param {EventSubscriptionVendor} subscriber - The subscriber that controls
17
+ * this subscription
18
+ * @param {function} listener - Function to invoke when the specified event is
19
+ * emitted
20
+ * @param {*} context - Optional context object to use when invoking the
21
+ * listener
22
+ */
23
+ constructor(
24
+ emitter: EventEmitter,
25
+ subscriber: EventSubscriptionVendor,
26
+ listener: Function,
27
+ context: Object | undefined | null
28
+ ) {
29
+ super(subscriber);
30
+ this.emitter = emitter;
31
+ this.listener = listener;
32
+ this.context = context;
33
+ }
34
+
35
+ /**
36
+ * Removes this subscription from the emitter that registered it.
37
+ * Note: we're overriding the `remove()` method of EventSubscription here
38
+ * but deliberately not calling `super.remove()` as the responsibility
39
+ * for removing the subscription lies with the EventEmitter.
40
+ */
41
+ remove() {
42
+ this.emitter.removeSubscription(this);
43
+ }
44
+ }
@@ -0,0 +1,28 @@
1
+ import type { EventSubscriptionVendor } from './_EventSubscriptionVendor';
2
+
3
+ /**
4
+ * EventSubscription represents a subscription to a particular event. It can
5
+ * remove its own subscription.
6
+ */
7
+ export class EventSubscription {
8
+ // @ts-ignore this value is assigned after creating instance of class
9
+ eventType: string;
10
+ // @ts-ignore this value is assigned after creating instance of class
11
+ key: number;
12
+ subscriber: EventSubscriptionVendor;
13
+
14
+ /**
15
+ * @param {EventSubscriptionVendor} subscriber the subscriber that controls
16
+ * this subscription.
17
+ */
18
+ constructor(subscriber: EventSubscriptionVendor) {
19
+ this.subscriber = subscriber;
20
+ }
21
+
22
+ /**
23
+ * Removes this subscription from the subscriber that controls it.
24
+ */
25
+ remove() {
26
+ this.subscriber.removeSubscription(this);
27
+ }
28
+ }
@@ -0,0 +1,89 @@
1
+ import type { EventSubscription } from './_EventSubscription';
2
+
3
+ /**
4
+ * EventSubscriptionVendor stores a set of EventSubscriptions that are
5
+ * subscribed to a particular event type.
6
+ */
7
+ export class EventSubscriptionVendor {
8
+ _subscriptionsForType: Record<string, EventSubscription[] | null | undefined>;
9
+ _currentSubscription: EventSubscription | null | undefined;
10
+
11
+ constructor() {
12
+ this._subscriptionsForType = {};
13
+ this._currentSubscription = null;
14
+ }
15
+
16
+ /**
17
+ * Adds a subscription keyed by an event type.
18
+ *
19
+ * @param {string} eventType
20
+ * @param {EventSubscription} subscription
21
+ */
22
+ addSubscription(
23
+ eventType: string,
24
+ subscription: EventSubscription
25
+ ): EventSubscription {
26
+ if (subscription.subscriber !== this) {
27
+ console.warn('The subscriber of the subscription is incorrectly set.'); // TODO: throw error or use logger?
28
+ }
29
+ if (!this._subscriptionsForType[eventType]) {
30
+ this._subscriptionsForType[eventType] = [];
31
+ }
32
+ const eventSubscriptions = this._subscriptionsForType[
33
+ eventType
34
+ ] as EventSubscription[];
35
+ const key = eventSubscriptions.length;
36
+ eventSubscriptions.push(subscription);
37
+ subscription.eventType = eventType;
38
+ subscription.key = key;
39
+ return subscription;
40
+ }
41
+
42
+ /**
43
+ * Removes a bulk set of the subscriptions.
44
+ *
45
+ * @param {?string} eventType - Optional name of the event type whose
46
+ * registered supscriptions to remove, if null or undefined remove all subscriptions.
47
+ */
48
+ removeAllSubscriptions(eventType: string | undefined | null) {
49
+ if (eventType === undefined || eventType === null) {
50
+ this._subscriptionsForType = {};
51
+ } else {
52
+ delete this._subscriptionsForType[eventType];
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Removes a specific subscription. Instead of calling this function, call
58
+ * `subscription.remove()` directly.
59
+ *
60
+ * @param {object} subscription
61
+ */
62
+ removeSubscription(subscription: EventSubscription) {
63
+ const eventType = subscription.eventType;
64
+ const key = subscription.key;
65
+
66
+ const subscriptionsForType = this._subscriptionsForType[eventType];
67
+ if (subscriptionsForType) {
68
+ delete subscriptionsForType[key];
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Returns the array of subscriptions that are currently registered for the
74
+ * given event type.
75
+ *
76
+ * Note: This array can be potentially sparse as subscriptions are deleted
77
+ * from it when they are removed.
78
+ *
79
+ * TODO: This returns a nullable array. wat?
80
+ *
81
+ * @param {string} eventType
82
+ * @returns {?array}
83
+ */
84
+ getSubscriptionsForType(
85
+ eventType: string
86
+ ): EventSubscription[] | null | undefined {
87
+ return this._subscriptionsForType[eventType];
88
+ }
89
+ }
@@ -0,0 +1 @@
1
+ export * from './emitter/EventEmitter';