@100mslive/react-native-hms 1.4.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 (205) hide show
  1. package/android/build.gradle +3 -2
  2. package/android/src/main/java/com/reactnativehmssdk/HMSDecoder.kt +189 -55
  3. package/android/src/main/java/com/reactnativehmssdk/HMSHelper.kt +1 -0
  4. package/android/src/main/java/com/reactnativehmssdk/HMSManager.kt +94 -38
  5. package/android/src/main/java/com/reactnativehmssdk/HMSRNSDK.kt +446 -107
  6. package/android/src/main/java/com/reactnativehmssdk/HMSSDKViewManager.kt +5 -0
  7. package/android/src/main/java/com/reactnativehmssdk/HMSView.kt +42 -25
  8. package/android/src/main/res/layout/hms_view.xml +2 -2
  9. package/ios/HMSConstants.swift +33 -0
  10. package/ios/HMSDecoder.swift +372 -157
  11. package/ios/HMSHelper.swift +44 -7
  12. package/ios/HMSManager.m +17 -2
  13. package/ios/HMSManager.swift +177 -75
  14. package/ios/HMSRNSDK.swift +641 -199
  15. package/ios/HMSView.m +1 -0
  16. package/ios/HMSView.swift +21 -19
  17. package/lib/commonjs/classes/HMSAudioTrackSettings.js +14 -0
  18. package/lib/commonjs/classes/HMSAudioTrackSettings.js.map +1 -1
  19. package/lib/commonjs/classes/HMSCameraControl.js +28 -0
  20. package/lib/commonjs/classes/HMSCameraControl.js.map +1 -0
  21. package/lib/commonjs/classes/HMSEncoder.js +97 -14
  22. package/lib/commonjs/classes/HMSEncoder.js.map +1 -1
  23. package/lib/commonjs/classes/HMSIOSAudioMode.js +13 -0
  24. package/lib/commonjs/classes/HMSIOSAudioMode.js.map +1 -0
  25. package/lib/commonjs/classes/HMSLayer.js +14 -0
  26. package/lib/commonjs/classes/HMSLayer.js.map +1 -0
  27. package/lib/commonjs/classes/HMSLocalVideoStats.js +11 -0
  28. package/lib/commonjs/classes/HMSLocalVideoStats.js.map +1 -1
  29. package/lib/commonjs/classes/HMSPeer.js +1 -1
  30. package/lib/commonjs/classes/HMSPeer.js.map +1 -1
  31. package/lib/commonjs/classes/HMSPublishSettings.js +2 -0
  32. package/lib/commonjs/classes/HMSPublishSettings.js.map +1 -1
  33. package/lib/commonjs/classes/HMSQualityLimitationReason.js +16 -0
  34. package/lib/commonjs/classes/HMSQualityLimitationReason.js.map +1 -0
  35. package/lib/commonjs/classes/HMSQualityLimitationReasons.js +27 -0
  36. package/lib/commonjs/classes/HMSQualityLimitationReasons.js.map +1 -0
  37. package/lib/commonjs/classes/HMSRemotePeer.js.map +1 -1
  38. package/lib/commonjs/classes/HMSRemoteVideoTrack.js +39 -3
  39. package/lib/commonjs/classes/HMSRemoteVideoTrack.js.map +1 -1
  40. package/lib/commonjs/classes/HMSRoom.js +4 -0
  41. package/lib/commonjs/classes/HMSRoom.js.map +1 -1
  42. package/lib/commonjs/classes/HMSRoomUpdate.js +1 -0
  43. package/lib/commonjs/classes/HMSRoomUpdate.js.map +1 -1
  44. package/lib/commonjs/classes/HMSSDK.js +82 -45
  45. package/lib/commonjs/classes/HMSSDK.js.map +1 -1
  46. package/lib/commonjs/classes/HMSSessionStore.js +173 -0
  47. package/lib/commonjs/classes/HMSSessionStore.js.map +1 -0
  48. package/lib/commonjs/classes/HMSSimulcastLayerDefinition.js +19 -0
  49. package/lib/commonjs/classes/HMSSimulcastLayerDefinition.js.map +1 -0
  50. package/lib/commonjs/classes/HMSSimulcastLayerSettingsPolicy.js +23 -0
  51. package/lib/commonjs/classes/HMSSimulcastLayerSettingsPolicy.js.map +1 -0
  52. package/lib/commonjs/classes/HMSSimulcastSettings.js +5 -4
  53. package/lib/commonjs/classes/HMSSimulcastSettings.js.map +1 -1
  54. package/lib/commonjs/classes/HMSSimulcastSettingsPolicy.js +17 -0
  55. package/lib/commonjs/classes/HMSSimulcastSettingsPolicy.js.map +1 -0
  56. package/lib/commonjs/classes/HMSSubscribeDegradationPolicy.js +21 -0
  57. package/lib/commonjs/classes/HMSSubscribeDegradationPolicy.js.map +1 -0
  58. package/lib/commonjs/classes/HMSSubscribeSettings.js +2 -0
  59. package/lib/commonjs/classes/HMSSubscribeSettings.js.map +1 -1
  60. package/lib/commonjs/classes/HMSUpdateListenerActions.js +8 -0
  61. package/lib/commonjs/classes/HMSUpdateListenerActions.js.map +1 -1
  62. package/lib/commonjs/classes/HMSVideoResolution.js.map +1 -1
  63. package/lib/commonjs/classes/HMSVideoTrackSettings.js +5 -0
  64. package/lib/commonjs/classes/HMSVideoTrackSettings.js.map +1 -1
  65. package/lib/commonjs/classes/HmsView.js +2 -0
  66. package/lib/commonjs/classes/HmsView.js.map +1 -1
  67. package/lib/commonjs/index.js +72 -0
  68. package/lib/commonjs/index.js.map +1 -1
  69. package/lib/commonjs/utils/emitter/EventEmitter.js +162 -0
  70. package/lib/commonjs/utils/emitter/EventEmitter.js.map +1 -0
  71. package/lib/commonjs/utils/emitter/_EmitterSubscription.js +46 -0
  72. package/lib/commonjs/utils/emitter/_EmitterSubscription.js.map +1 -0
  73. package/lib/commonjs/utils/emitter/_EventSubscription.js +36 -0
  74. package/lib/commonjs/utils/emitter/_EventSubscription.js.map +1 -0
  75. package/lib/commonjs/utils/emitter/_EventSubscriptionVendor.js +90 -0
  76. package/lib/commonjs/utils/emitter/_EventSubscriptionVendor.js.map +1 -0
  77. package/lib/commonjs/utils/index.js +17 -0
  78. package/lib/commonjs/utils/index.js.map +1 -0
  79. package/lib/module/classes/HMSAudioTrackSettings.js +14 -0
  80. package/lib/module/classes/HMSAudioTrackSettings.js.map +1 -1
  81. package/lib/module/classes/HMSCameraControl.js +21 -0
  82. package/lib/module/classes/HMSCameraControl.js.map +1 -0
  83. package/lib/module/classes/HMSEncoder.js +97 -14
  84. package/lib/module/classes/HMSEncoder.js.map +1 -1
  85. package/lib/module/classes/HMSIOSAudioMode.js +6 -0
  86. package/lib/module/classes/HMSIOSAudioMode.js.map +1 -0
  87. package/lib/module/classes/HMSLayer.js +7 -0
  88. package/lib/module/classes/HMSLayer.js.map +1 -0
  89. package/lib/module/classes/HMSLocalVideoStats.js +11 -0
  90. package/lib/module/classes/HMSLocalVideoStats.js.map +1 -1
  91. package/lib/module/classes/HMSPeer.js +1 -1
  92. package/lib/module/classes/HMSPeer.js.map +1 -1
  93. package/lib/module/classes/HMSPublishSettings.js +2 -0
  94. package/lib/module/classes/HMSPublishSettings.js.map +1 -1
  95. package/lib/module/classes/HMSQualityLimitationReason.js +9 -0
  96. package/lib/module/classes/HMSQualityLimitationReason.js.map +1 -0
  97. package/lib/module/classes/HMSQualityLimitationReasons.js +20 -0
  98. package/lib/module/classes/HMSQualityLimitationReasons.js.map +1 -0
  99. package/lib/module/classes/HMSRemotePeer.js.map +1 -1
  100. package/lib/module/classes/HMSRemoteVideoTrack.js +39 -3
  101. package/lib/module/classes/HMSRemoteVideoTrack.js.map +1 -1
  102. package/lib/module/classes/HMSRoom.js +4 -0
  103. package/lib/module/classes/HMSRoom.js.map +1 -1
  104. package/lib/module/classes/HMSRoomUpdate.js +1 -0
  105. package/lib/module/classes/HMSRoomUpdate.js.map +1 -1
  106. package/lib/module/classes/HMSSDK.js +83 -45
  107. package/lib/module/classes/HMSSDK.js.map +1 -1
  108. package/lib/module/classes/HMSSessionStore.js +166 -0
  109. package/lib/module/classes/HMSSessionStore.js.map +1 -0
  110. package/lib/module/classes/HMSSimulcastLayerDefinition.js +12 -0
  111. package/lib/module/classes/HMSSimulcastLayerDefinition.js.map +1 -0
  112. package/lib/module/classes/HMSSimulcastLayerSettingsPolicy.js +16 -0
  113. package/lib/module/classes/HMSSimulcastLayerSettingsPolicy.js.map +1 -0
  114. package/lib/module/classes/HMSSimulcastSettings.js +5 -4
  115. package/lib/module/classes/HMSSimulcastSettings.js.map +1 -1
  116. package/lib/module/classes/HMSSimulcastSettingsPolicy.js +10 -0
  117. package/lib/module/classes/HMSSimulcastSettingsPolicy.js.map +1 -0
  118. package/lib/module/classes/HMSSubscribeDegradationPolicy.js +14 -0
  119. package/lib/module/classes/HMSSubscribeDegradationPolicy.js.map +1 -0
  120. package/lib/module/classes/HMSSubscribeSettings.js +2 -0
  121. package/lib/module/classes/HMSSubscribeSettings.js.map +1 -1
  122. package/lib/module/classes/HMSUpdateListenerActions.js +8 -0
  123. package/lib/module/classes/HMSUpdateListenerActions.js.map +1 -1
  124. package/lib/module/classes/HMSVideoResolution.js.map +1 -1
  125. package/lib/module/classes/HMSVideoTrackSettings.js +5 -0
  126. package/lib/module/classes/HMSVideoTrackSettings.js.map +1 -1
  127. package/lib/module/classes/HmsView.js +2 -0
  128. package/lib/module/classes/HmsView.js.map +1 -1
  129. package/lib/module/index.js +6 -0
  130. package/lib/module/index.js.map +1 -1
  131. package/lib/module/utils/emitter/EventEmitter.js +151 -0
  132. package/lib/module/utils/emitter/EventEmitter.js.map +1 -0
  133. package/lib/module/utils/emitter/_EmitterSubscription.js +39 -0
  134. package/lib/module/utils/emitter/_EmitterSubscription.js.map +1 -0
  135. package/lib/module/utils/emitter/_EventSubscription.js +29 -0
  136. package/lib/module/utils/emitter/_EventSubscription.js.map +1 -0
  137. package/lib/module/utils/emitter/_EventSubscriptionVendor.js +83 -0
  138. package/lib/module/utils/emitter/_EventSubscriptionVendor.js.map +1 -0
  139. package/lib/module/utils/index.js +2 -0
  140. package/lib/module/utils/index.js.map +1 -0
  141. package/lib/typescript/classes/HMSAudioTrackSettings.d.ts +14 -0
  142. package/lib/typescript/classes/HMSCameraControl.d.ts +9 -0
  143. package/lib/typescript/classes/HMSEncoder.d.ts +13 -1
  144. package/lib/typescript/classes/HMSIOSAudioMode.d.ts +4 -0
  145. package/lib/typescript/classes/HMSLayer.d.ts +5 -0
  146. package/lib/typescript/classes/HMSLocalVideoStats.d.ts +6 -0
  147. package/lib/typescript/classes/HMSPeer.d.ts +1 -1
  148. package/lib/typescript/classes/HMSPublishSettings.d.ts +3 -0
  149. package/lib/typescript/classes/HMSQualityLimitationReason.d.ts +7 -0
  150. package/lib/typescript/classes/HMSQualityLimitationReasons.d.ts +17 -0
  151. package/lib/typescript/classes/HMSRemotePeer.d.ts +0 -1
  152. package/lib/typescript/classes/HMSRemoteVideoTrack.d.ts +6 -2
  153. package/lib/typescript/classes/HMSRoomUpdate.d.ts +2 -1
  154. package/lib/typescript/classes/HMSSDK.d.ts +21 -31
  155. package/lib/typescript/classes/HMSSessionStore.d.ts +63 -0
  156. package/lib/typescript/classes/HMSSimulcastLayerDefinition.d.ts +10 -0
  157. package/lib/typescript/classes/HMSSimulcastLayerSettingsPolicy.d.ts +12 -0
  158. package/lib/typescript/classes/HMSSimulcastSettings.d.ts +7 -3
  159. package/lib/typescript/classes/HMSSimulcastSettingsPolicy.d.ts +7 -0
  160. package/lib/typescript/classes/HMSSubscribeDegradationPolicy.d.ts +10 -0
  161. package/lib/typescript/classes/HMSSubscribeSettings.d.ts +5 -2
  162. package/lib/typescript/classes/HMSUpdateListenerActions.d.ts +9 -1
  163. package/lib/typescript/classes/HMSVideoResolution.d.ts +2 -2
  164. package/lib/typescript/classes/HMSVideoTrackSettings.d.ts +5 -0
  165. package/lib/typescript/classes/HmsView.d.ts +2 -2
  166. package/lib/typescript/index.d.ts +7 -0
  167. package/lib/typescript/utils/emitter/EventEmitter.d.ts +91 -0
  168. package/lib/typescript/utils/emitter/_EmitterSubscription.d.ts +29 -0
  169. package/lib/typescript/utils/emitter/_EventSubscription.d.ts +19 -0
  170. package/lib/typescript/utils/emitter/_EventSubscriptionVendor.d.ts +44 -0
  171. package/lib/typescript/utils/index.d.ts +1 -0
  172. package/package.json +21 -2
  173. package/sdk-versions.json +3 -3
  174. package/src/classes/HMSAudioTrackSettings.ts +16 -0
  175. package/src/classes/HMSCameraControl.ts +21 -0
  176. package/src/classes/HMSEncoder.ts +126 -11
  177. package/src/classes/HMSIOSAudioMode.ts +4 -0
  178. package/src/classes/HMSLayer.ts +5 -0
  179. package/src/classes/HMSLocalVideoStats.ts +21 -0
  180. package/src/classes/HMSPeer.ts +1 -1
  181. package/src/classes/HMSPublishSettings.ts +4 -0
  182. package/src/classes/HMSQualityLimitationReason.ts +7 -0
  183. package/src/classes/HMSQualityLimitationReasons.ts +27 -0
  184. package/src/classes/HMSRemotePeer.ts +0 -1
  185. package/src/classes/HMSRemoteVideoTrack.ts +52 -4
  186. package/src/classes/HMSRoom.ts +2 -0
  187. package/src/classes/HMSRoomUpdate.ts +1 -0
  188. package/src/classes/HMSSDK.tsx +124 -52
  189. package/src/classes/HMSSessionStore.ts +209 -0
  190. package/src/classes/HMSSimulcastLayerDefinition.ts +12 -0
  191. package/src/classes/HMSSimulcastLayerSettingsPolicy.ts +18 -0
  192. package/src/classes/HMSSimulcastSettings.ts +12 -3
  193. package/src/classes/HMSSimulcastSettingsPolicy.ts +9 -0
  194. package/src/classes/HMSSubscribeDegradationPolicy.ts +15 -0
  195. package/src/classes/HMSSubscribeSettings.ts +10 -2
  196. package/src/classes/HMSUpdateListenerActions.ts +8 -0
  197. package/src/classes/HMSVideoResolution.ts +1 -1
  198. package/src/classes/HMSVideoTrackSettings.ts +5 -0
  199. package/src/classes/HmsView.tsx +5 -1
  200. package/src/index.ts +10 -0
  201. package/src/utils/emitter/EventEmitter.ts +160 -0
  202. package/src/utils/emitter/_EmitterSubscription.ts +44 -0
  203. package/src/utils/emitter/_EventSubscription.ts +28 -0
  204. package/src/utils/emitter/_EventSubscriptionVendor.ts +89 -0
  205. package/src/utils/index.ts +1 -0
package/src/index.ts CHANGED
@@ -75,6 +75,16 @@ export * from './classes/HMSLogSettings';
75
75
  export * from './classes/HMSLogAlarmManager';
76
76
  export * from './classes/HMSMessageType';
77
77
  export * from './classes/HMSPIPListenerActions';
78
+ export * from './classes/HMSLayer';
79
+ export * from './classes/HMSSimulcastLayerDefinition';
80
+ export * from './classes/HMSQualityLimitationReasons';
81
+ export * from './classes/HMSQualityLimitationReason';
82
+ export * from './classes/HMSCameraControl';
83
+ export * from './classes/HMSIOSAudioMode';
84
+ export type {
85
+ HMSSessionStore,
86
+ HMSSessionStoreValue,
87
+ } from './classes/HMSSessionStore';
78
88
  export type { HmsViewComponent as HMSView } from './classes/HmsView';
79
89
 
80
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';