Browse Source

Unify events and output single TypeScript declaration (#2407)

* fix(events): unify events to a single EventManager type, add support for single typescript declaration

* fix(lint): fix lint

* fix(events): fix incorrect instatiation

* fix(events): clean up redundant methods

* fix(events): keep EventEmitter name, alias NodeEventEmitter

* fix(events): fix loose reference

* fix(EventEmitter): remove on/off alias as redundant

* fix(RTCUtils): bring event handlers under class to use same event emitter

* fix(RTCUtils): fix lint
release-8443
Daniel McAssey 1 year ago
parent
commit
0e9a4e2428
No account linked to committer's email address

+ 1
- 0
.gitignore View File

14
 stats.json
14
 stats.json
15
 .vscode
15
 .vscode
16
 dist
16
 dist
17
+index.d.ts
17
 types/auto
18
 types/auto
18
 types/types-comparer/auto.json
19
 types/types-comparer/auto.json
19
 types/types-comparer/hand-crafted.json
20
 types/types-comparer/hand-crafted.json

+ 1
- 1
JitsiConference.js View File

1
 import { getLogger } from '@jitsi/logger';
1
 import { getLogger } from '@jitsi/logger';
2
-import EventEmitter from 'events';
3
 import $ from 'jquery';
2
 import $ from 'jquery';
4
 import isEqual from 'lodash.isequal';
3
 import isEqual from 'lodash.isequal';
5
 import { Strophe } from 'strophe.js';
4
 import { Strophe } from 'strophe.js';
38
 import LocalStatsCollector from './modules/statistics/LocalStatsCollector';
37
 import LocalStatsCollector from './modules/statistics/LocalStatsCollector';
39
 import SpeakerStatsCollector from './modules/statistics/SpeakerStatsCollector';
38
 import SpeakerStatsCollector from './modules/statistics/SpeakerStatsCollector';
40
 import Statistics from './modules/statistics/statistics';
39
 import Statistics from './modules/statistics/statistics';
40
+import EventEmitter from './modules/util/EventEmitter';
41
 import { safeSubtract } from './modules/util/MathUtil';
41
 import { safeSubtract } from './modules/util/MathUtil';
42
 import RandomUtil from './modules/util/RandomUtil';
42
 import RandomUtil from './modules/util/RandomUtil';
43
 import ComponentsVersions from './modules/version/ComponentsVersions';
43
 import ComponentsVersions from './modules/version/ComponentsVersions';

+ 5
- 32
JitsiMediaDevices.js View File

1
-import EventEmitter from 'events';
2
-
3
 import * as JitsiMediaDevicesEvents from './JitsiMediaDevicesEvents';
1
 import * as JitsiMediaDevicesEvents from './JitsiMediaDevicesEvents';
4
 import RTC from './modules/RTC/RTC';
2
 import RTC from './modules/RTC/RTC';
5
 import browser from './modules/browser';
3
 import browser from './modules/browser';
4
+import Listenable from './modules/util/Listenable';
6
 import { MediaType } from './service/RTC/MediaType';
5
 import { MediaType } from './service/RTC/MediaType';
7
 import RTCEvents from './service/RTC/RTCEvents';
6
 import RTCEvents from './service/RTC/RTCEvents';
8
 
7
 
13
 /**
12
 /**
14
  * Media devices utilities for Jitsi.
13
  * Media devices utilities for Jitsi.
15
  */
14
  */
16
-class JitsiMediaDevices {
15
+class JitsiMediaDevices extends Listenable {
17
     /**
16
     /**
18
      * Initializes a {@code JitsiMediaDevices} object. There will be a single
17
      * Initializes a {@code JitsiMediaDevices} object. There will be a single
19
      * instance of this class.
18
      * instance of this class.
20
      */
19
      */
21
     constructor() {
20
     constructor() {
22
-        this._eventEmitter = new EventEmitter();
21
+        super();
23
         this._permissions = {};
22
         this._permissions = {};
24
 
23
 
25
         RTC.addListener(
24
         RTC.addListener(
26
             RTCEvents.DEVICE_LIST_CHANGED,
25
             RTCEvents.DEVICE_LIST_CHANGED,
27
             devices =>
26
             devices =>
28
-                this._eventEmitter.emit(
27
+                this.eventEmitter.emit(
29
                     JitsiMediaDevicesEvents.DEVICE_LIST_CHANGED,
28
                     JitsiMediaDevicesEvents.DEVICE_LIST_CHANGED,
30
                     devices));
29
                     devices));
31
 
30
 
128
                 ...this._permissions,
127
                 ...this._permissions,
129
                 ...permissions
128
                 ...permissions
130
             };
129
             };
131
-            this._eventEmitter.emit(JitsiMediaDevicesEvents.PERMISSIONS_CHANGED, this._permissions);
130
+            this.eventEmitter.emit(JitsiMediaDevicesEvents.PERMISSIONS_CHANGED, this._permissions);
132
 
131
 
133
             if (this._permissions[MediaType.AUDIO] || this._permissions[MediaType.VIDEO]) {
132
             if (this._permissions[MediaType.AUDIO] || this._permissions[MediaType.VIDEO]) {
134
                 // Triggering device list update when the permissiions are granted in order to update
133
                 // Triggering device list update when the permissiions are granted in order to update
265
     setAudioOutputDevice(deviceId) {
264
     setAudioOutputDevice(deviceId) {
266
         return RTC.setAudioOutputDevice(deviceId);
265
         return RTC.setAudioOutputDevice(deviceId);
267
     }
266
     }
268
-
269
-    /**
270
-     * Adds an event handler.
271
-     * @param {string} event - event name
272
-     * @param {function} handler - event handler
273
-     */
274
-    addEventListener(event, handler) {
275
-        this._eventEmitter.addListener(event, handler);
276
-    }
277
-
278
-    /**
279
-     * Removes event handler.
280
-     * @param {string} event - event name
281
-     * @param {function} handler - event handler
282
-     */
283
-    removeEventListener(event, handler) {
284
-        this._eventEmitter.removeListener(event, handler);
285
-    }
286
-
287
-    /**
288
-     * Emits an event.
289
-     * @param {string} event - event name
290
-     */
291
-    emitEvent(event, ...args) {
292
-        this._eventEmitter.emit(event, ...args);
293
-    }
294
 }
267
 }
295
 
268
 
296
 export default new JitsiMediaDevices();
269
 export default new JitsiMediaDevices();

+ 2
- 2
JitsiMeetJS.ts View File

318
 
318
 
319
         if (firePermissionPrompt && !RTC.arePermissionsGrantedForAvailableDevices()) {
319
         if (firePermissionPrompt && !RTC.arePermissionsGrantedForAvailableDevices()) {
320
             // @ts-ignore
320
             // @ts-ignore
321
-            JitsiMediaDevices.emitEvent(JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN, browser.getName());
321
+            JitsiMediaDevices.emit(JitsiMediaDevicesEvents.PERMISSION_PROMPT_IS_SHOWN, browser.getName());
322
         } else if (fireSlowPromiseEvent) {
322
         } else if (fireSlowPromiseEvent) {
323
             window.setTimeout(() => {
323
             window.setTimeout(() => {
324
                 if (!promiseFulfilled) {
324
                 if (!promiseFulfilled) {
325
-                    JitsiMediaDevices.emitEvent(JitsiMediaDevicesEvents.SLOW_GET_USER_MEDIA);
325
+                    JitsiMediaDevices.emit(JitsiMediaDevicesEvents.SLOW_GET_USER_MEDIA);
326
                 }
326
                 }
327
             }, USER_MEDIA_SLOW_PROMISE_TIMEOUT);
327
             }, USER_MEDIA_SLOW_PROMISE_TIMEOUT);
328
         }
328
         }

+ 0
- 3
modules/RTC/CodecSelection.spec.js View File

1
-import EventEmitter from 'events';
2
-
3
 import * as JitsiConferenceEvents from '../../JitsiConferenceEvents.ts';
1
 import * as JitsiConferenceEvents from '../../JitsiConferenceEvents.ts';
4
 import Listenable from '../util/Listenable.js';
2
 import Listenable from '../util/Listenable.js';
5
 import JingleSessionPC from '../xmpp/JingleSessionPC.js';
3
 import JingleSessionPC from '../xmpp/JingleSessionPC.js';
42
         };
40
         };
43
 
41
 
44
         this.activeMediaSession = undefined;
42
         this.activeMediaSession = undefined;
45
-        this.eventEmitter = new EventEmitter();
46
         this.mediaSessions = [];
43
         this.mediaSessions = [];
47
         this.participants = [];
44
         this.participants = [];
48
         this._signalingLayer = new MockSignalingLayerImpl();
45
         this._signalingLayer = new MockSignalingLayerImpl();

+ 1
- 5
modules/RTC/JitsiTrack.js View File

1
 import { getLogger } from '@jitsi/logger';
1
 import { getLogger } from '@jitsi/logger';
2
-import EventEmitter from 'events';
3
 
2
 
4
 import * as JitsiTrackEvents from '../../JitsiTrackEvents';
3
 import * as JitsiTrackEvents from '../../JitsiTrackEvents';
5
 import { MediaType } from '../../service/RTC/MediaType';
4
 import { MediaType } from '../../service/RTC/MediaType';
6
 import browser from '../browser';
5
 import browser from '../browser';
6
+import EventEmitter from '../util/EventEmitter';
7
 
7
 
8
 import RTCUtils from './RTCUtils';
8
 import RTCUtils from './RTCUtils';
9
 
9
 
44
             videoType) {
44
             videoType) {
45
         super();
45
         super();
46
 
46
 
47
-        // aliases for addListener/removeListener
48
-        this.addEventListener = this.addListener;
49
-        this.removeEventListener = this.off = this.removeListener;
50
-
51
         /**
47
         /**
52
          * Array with the HTML elements that are displaying the streams.
48
          * Array with the HTML elements that are displaying the streams.
53
          * @type {Array}
49
          * @type {Array}

+ 65
- 74
modules/RTC/RTCUtils.js View File

1
 import { getLogger } from '@jitsi/logger';
1
 import { getLogger } from '@jitsi/logger';
2
-import EventEmitter from 'events';
3
 import clonedeep from 'lodash.clonedeep';
2
 import clonedeep from 'lodash.clonedeep';
4
 import 'webrtc-adapter';
3
 import 'webrtc-adapter';
5
 
4
 
18
 
17
 
19
 const logger = getLogger(__filename);
18
 const logger = getLogger(__filename);
20
 
19
 
21
-const eventEmitter = new EventEmitter();
22
-
23
 const AVAILABLE_DEVICES_POLL_INTERVAL_TIME = 3000; // ms
20
 const AVAILABLE_DEVICES_POLL_INTERVAL_TIME = 3000; // ms
24
 
21
 
25
 /**
22
 /**
157
     return constraints;
154
     return constraints;
158
 }
155
 }
159
 
156
 
160
-/**
161
- * Updates the granted permissions based on the options we requested and the
162
- * streams we received.
163
- * @param um the options we requested to getUserMedia.
164
- * @param stream the stream we received from calling getUserMedia.
165
- */
166
-function updateGrantedPermissions(um, stream) {
167
-    const audioTracksReceived
168
-        = Boolean(stream) && stream.getAudioTracks().length > 0;
169
-    const videoTracksReceived
170
-        = Boolean(stream) && stream.getVideoTracks().length > 0;
171
-    const grantedPermissions = {};
172
-
173
-    if (um.indexOf('video') !== -1) {
174
-        grantedPermissions.video = videoTracksReceived;
175
-    }
176
-    if (um.indexOf('audio') !== -1) {
177
-        grantedPermissions.audio = audioTracksReceived;
178
-    }
179
-
180
-    eventEmitter.emit(RTCEvents.PERMISSIONS_CHANGED, grantedPermissions);
181
-}
182
-
183
 /**
157
 /**
184
  * Checks if new list of available media devices differs from previous one.
158
  * Checks if new list of available media devices differs from previous one.
185
  * @param {MediaDeviceInfo[]} newDevices - list of new devices.
159
  * @param {MediaDeviceInfo[]} newDevices - list of new devices.
247
     });
221
     });
248
 }
222
 }
249
 
223
 
250
-
251
-/**
252
- * Update known devices.
253
- *
254
- * @param {Array<Object>} pds - The new devices.
255
- * @returns {void}
256
- *
257
- * NOTE: Use this function as a shared callback to handle both the devicechange event  and the polling implementations.
258
- * This prevents duplication and works around a chrome bug (verified to occur on 68) where devicechange fires twice in
259
- * a row, which can cause async post devicechange processing to collide.
260
- */
261
-function updateKnownDevices(pds) {
262
-    if (compareAvailableMediaDevices(pds)) {
263
-        onMediaDevicesListChanged(pds);
264
-    }
265
-}
266
-
267
-/**
268
- * Event handler for the 'devicechange' event.
269
- *
270
- * @param {MediaDeviceInfo[]} devices - list of media devices.
271
- * @emits RTCEvents.DEVICE_LIST_CHANGED
272
- */
273
-function onMediaDevicesListChanged(devicesReceived) {
274
-    availableDevices = devicesReceived.slice(0);
275
-    logger.info('list of media devices has changed:', availableDevices);
276
-
277
-    sendDeviceListToAnalytics(availableDevices);
278
-
279
-    // Used by tracks to update the real device id before the consumer of lib-jitsi-meet receives the new device list.
280
-    eventEmitter.emit(RTCEvents.DEVICE_LIST_WILL_CHANGE, availableDevices);
281
-
282
-    eventEmitter.emit(RTCEvents.DEVICE_LIST_CHANGED, availableDevices);
283
-}
284
-
285
 /**
224
 /**
286
  *
225
  *
287
  */
226
  */
288
 class RTCUtils extends Listenable {
227
 class RTCUtils extends Listenable {
289
-    /**
290
-     *
291
-     */
292
-    constructor() {
293
-        super(eventEmitter);
294
-    }
295
-
296
     /**
228
     /**
297
      * Depending on the browser, sets difference instance methods for
229
      * Depending on the browser, sets difference instance methods for
298
      * interacting with user media and adds methods to native WebRTC-related
230
      * interacting with user media and adds methods to native WebRTC-related
347
                 logger.debug('Available devices: ', availableDevices);
279
                 logger.debug('Available devices: ', availableDevices);
348
                 sendDeviceListToAnalytics(availableDevices);
280
                 sendDeviceListToAnalytics(availableDevices);
349
 
281
 
350
-                eventEmitter.emit(
282
+                this.eventEmitter.emit(
351
                     RTCEvents.DEVICE_LIST_AVAILABLE,
283
                     RTCEvents.DEVICE_LIST_AVAILABLE,
352
                     availableDevices);
284
                     availableDevices);
353
 
285
 
373
     enumerateDevices(callback) {
305
     enumerateDevices(callback) {
374
         navigator.mediaDevices.enumerateDevices()
306
         navigator.mediaDevices.enumerateDevices()
375
             .then(devices => {
307
             .then(devices => {
376
-                updateKnownDevices(devices);
308
+                this._updateKnownDevices(devices);
377
                 callback(devices);
309
                 callback(devices);
378
             })
310
             })
379
             .catch(error => {
311
             .catch(error => {
380
                 logger.warn(`Failed to  enumerate devices. ${error}`);
312
                 logger.warn(`Failed to  enumerate devices. ${error}`);
381
-                updateKnownDevices([]);
313
+                this._updateKnownDevices([]);
382
                 callback([]);
314
                 callback([]);
383
             });
315
             });
384
     }
316
     }
407
             navigator.mediaDevices.getUserMedia(constraints)
339
             navigator.mediaDevices.getUserMedia(constraints)
408
                 .then(stream => {
340
                 .then(stream => {
409
                     logger.log('onUserMediaSuccess');
341
                     logger.log('onUserMediaSuccess');
410
-                    updateGrantedPermissions(umDevices, stream);
342
+                    this._updateGrantedPermissions(umDevices, stream);
411
                     if (!timeoutExpired) {
343
                     if (!timeoutExpired) {
412
                         if (typeof gumTimeout !== 'undefined') {
344
                         if (typeof gumTimeout !== 'undefined') {
413
                             clearTimeout(gumTimeout);
345
                             clearTimeout(gumTimeout);
427
                     }
359
                     }
428
 
360
 
429
                     if (jitsiError.name === JitsiTrackErrors.PERMISSION_DENIED) {
361
                     if (jitsiError.name === JitsiTrackErrors.PERMISSION_DENIED) {
430
-                        updateGrantedPermissions(umDevices, undefined);
362
+                        this._updateGrantedPermissions(umDevices, undefined);
431
                     }
363
                     }
432
 
364
 
433
                     // else {
365
                     // else {
497
         return missingDevices;
429
         return missingDevices;
498
     }
430
     }
499
 
431
 
432
+    /**
433
+     * Event handler for the 'devicechange' event.
434
+     *
435
+     * @param {MediaDeviceInfo[]} devices - list of media devices.
436
+     * @emits RTCEvents.DEVICE_LIST_CHANGED
437
+     */
438
+    _onMediaDevicesListChanged(devicesReceived) {
439
+        availableDevices = devicesReceived.slice(0);
440
+        logger.info('list of media devices has changed:', availableDevices);
441
+
442
+        sendDeviceListToAnalytics(availableDevices);
443
+
444
+        // Used by tracks to update the real device id before the consumer of lib-jitsi-meet receives the
445
+        // new device list.
446
+        this.eventEmitter.emit(RTCEvents.DEVICE_LIST_WILL_CHANGE, availableDevices);
447
+
448
+        this.eventEmitter.emit(RTCEvents.DEVICE_LIST_CHANGED, availableDevices);
449
+    }
450
+
451
+    /**
452
+     * Update known devices.
453
+     *
454
+     * @param {Array<Object>} pds - The new devices.
455
+     * @returns {void}
456
+     *
457
+     * NOTE: Use this function as a shared callback to handle both the devicechange event and the
458
+     * polling implementations.
459
+     * This prevents duplication and works around a chrome bug (verified to occur on 68) where devicechange
460
+     * fires twice in a row, which can cause async post devicechange processing to collide.
461
+     */
462
+    _updateKnownDevices(pds) {
463
+        if (compareAvailableMediaDevices(pds)) {
464
+            this._onMediaDevicesListChanged(pds);
465
+        }
466
+    }
467
+
468
+    /**
469
+     * Updates the granted permissions based on the options we requested and the
470
+     * streams we received.
471
+     * @param um the options we requested to getUserMedia.
472
+     * @param stream the stream we received from calling getUserMedia.
473
+     */
474
+    _updateGrantedPermissions(um, stream) {
475
+        const audioTracksReceived
476
+            = Boolean(stream) && stream.getAudioTracks().length > 0;
477
+        const videoTracksReceived
478
+            = Boolean(stream) && stream.getVideoTracks().length > 0;
479
+        const grantedPermissions = {};
480
+
481
+        if (um.indexOf('video') !== -1) {
482
+            grantedPermissions.video = videoTracksReceived;
483
+        }
484
+        if (um.indexOf('audio') !== -1) {
485
+            grantedPermissions.audio = audioTracksReceived;
486
+        }
487
+
488
+        this.eventEmitter.emit(RTCEvents.PERMISSIONS_CHANGED, grantedPermissions);
489
+    }
490
+
500
     /**
491
     /**
501
      * Gets streams from specified device types. This function intentionally
492
      * Gets streams from specified device types. This function intentionally
502
      * ignores errors for upstream to catch and handle instead.
493
      * ignores errors for upstream to catch and handle instead.
792
 
783
 
793
                 logger.log(`Audio output device set to ${deviceId}`);
784
                 logger.log(`Audio output device set to ${deviceId}`);
794
 
785
 
795
-                eventEmitter.emit(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
786
+                this.eventEmitter.emit(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
796
                     deviceId);
787
                     deviceId);
797
             });
788
             });
798
     }
789
     }

+ 1
- 1
modules/RTCStats/RTCStats.ts View File

2
 
2
 
3
 import rtcstatsInit from '@jitsi/rtcstats/rtcstats';
3
 import rtcstatsInit from '@jitsi/rtcstats/rtcstats';
4
 import traceInit from '@jitsi/rtcstats/trace-ws';
4
 import traceInit from '@jitsi/rtcstats/trace-ws';
5
-import EventEmitter from 'events';
6
 
5
 
7
 import {
6
 import {
8
     CONFERENCE_JOINED,
7
     CONFERENCE_JOINED,
12
 import JitsiConference from '../../JitsiConference';
11
 import JitsiConference from '../../JitsiConference';
13
 import { IRTCStatsConfiguration } from './interfaces';
12
 import { IRTCStatsConfiguration } from './interfaces';
14
 import { RTC_STATS_PC_EVENT, RTC_STATS_WC_DISCONNECTED } from './RTCStatsEvents';
13
 import { RTC_STATS_PC_EVENT, RTC_STATS_WC_DISCONNECTED } from './RTCStatsEvents';
14
+import EventEmitter from '../util/EventEmitter';
15
 
15
 
16
 const logger = getLogger(__filename);
16
 const logger = getLogger(__filename);
17
 
17
 

+ 1
- 2
modules/detection/NoAudioSignalDetection.js View File

1
-import EventEmitter from 'events';
2
-
3
 import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
1
 import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
4
 import * as JitsiTrackEvents from '../../JitsiTrackEvents';
2
 import * as JitsiTrackEvents from '../../JitsiTrackEvents';
3
+import EventEmitter from '../util/EventEmitter';
5
 
4
 
6
 import * as DetectionEvents from './DetectionEvents';
5
 import * as DetectionEvents from './DetectionEvents';
7
 
6
 

+ 1
- 2
modules/detection/TrackVADEmitter.js View File

1
-import EventEmitter from 'events';
2
-
3
 import RTC from '../RTC/RTC';
1
 import RTC from '../RTC/RTC';
2
+import EventEmitter from '../util/EventEmitter';
4
 import { createAudioContext } from '../webaudio/WebAudioUtils';
3
 import { createAudioContext } from '../webaudio/WebAudioUtils';
5
 
4
 
6
 import { VAD_SCORE_PUBLISHED } from './DetectionEvents';
5
 import { VAD_SCORE_PUBLISHED } from './DetectionEvents';

+ 1
- 1
modules/detection/VADAudioAnalyser.js View File

1
 import { getLogger } from '@jitsi/logger';
1
 import { getLogger } from '@jitsi/logger';
2
-import { EventEmitter } from 'events';
3
 
2
 
4
 import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
3
 import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
4
+import EventEmitter from '../util/EventEmitter';
5
 
5
 
6
 import { DETECTOR_STATE_CHANGE, VAD_SCORE_PUBLISHED } from './DetectionEvents';
6
 import { DETECTOR_STATE_CHANGE, VAD_SCORE_PUBLISHED } from './DetectionEvents';
7
 import TrackVADEmitter from './TrackVADEmitter';
7
 import TrackVADEmitter from './TrackVADEmitter';

+ 1
- 2
modules/detection/VADNoiseDetection.js View File

1
-import { EventEmitter } from 'events';
2
-
1
+import EventEmitter from '../util/EventEmitter';
3
 import { calculateAverage, filterPositiveValues } from '../util/MathUtil';
2
 import { calculateAverage, filterPositiveValues } from '../util/MathUtil';
4
 
3
 
5
 import { DETECTOR_STATE_CHANGE, VAD_NOISY_DEVICE } from './DetectionEvents';
4
 import { DETECTOR_STATE_CHANGE, VAD_NOISY_DEVICE } from './DetectionEvents';

+ 2
- 1
modules/detection/VADReportingService.js View File

1
 import { getLogger } from '@jitsi/logger';
1
 import { getLogger } from '@jitsi/logger';
2
-import EventEmitter from 'events';
2
+
3
+import EventEmitter from '../util/EventEmitter';
3
 
4
 
4
 import * as DetectionEvents from './DetectionEvents';
5
 import * as DetectionEvents from './DetectionEvents';
5
 import TrackVADEmitter from './TrackVADEmitter';
6
 import TrackVADEmitter from './TrackVADEmitter';

+ 1
- 2
modules/detection/VADTalkMutedDetection.js View File

1
-import { EventEmitter } from 'events';
2
-
1
+import EventEmitter from '../util/EventEmitter';
3
 import { calculateAverage } from '../util/MathUtil';
2
 import { calculateAverage } from '../util/MathUtil';
4
 
3
 
5
 import { DETECTOR_STATE_CHANGE, VAD_TALK_WHILE_MUTED } from './DetectionEvents';
4
 import { DETECTOR_STATE_CHANGE, VAD_TALK_WHILE_MUTED } from './DetectionEvents';

+ 1
- 2
modules/statistics/SpeakerStatsCollector.spec.js View File

1
-import EventEmitter from 'events';
2
-
3
 import JitsiConference from '../../JitsiConference';
1
 import JitsiConference from '../../JitsiConference';
4
 import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
2
 import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
5
 import JitsiParticipant from '../../JitsiParticipant';
3
 import JitsiParticipant from '../../JitsiParticipant';
4
+import EventEmitter from '../util/EventEmitter';
6
 
5
 
7
 import SpeakerStats from './SpeakerStats';
6
 import SpeakerStats from './SpeakerStats';
8
 import SpeakerStatsCollector from './SpeakerStatsCollector';
7
 import SpeakerStatsCollector from './SpeakerStatsCollector';

+ 1
- 2
modules/statistics/statistics.js View File

1
-import EventEmitter from 'events';
2
-
3
 import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
1
 import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
4
 import { JitsiTrackEvents } from '../../JitsiTrackEvents';
2
 import { JitsiTrackEvents } from '../../JitsiTrackEvents';
5
 import { FEEDBACK } from '../../service/statistics/AnalyticsEvents';
3
 import { FEEDBACK } from '../../service/statistics/AnalyticsEvents';
6
 import * as StatisticsEvents from '../../service/statistics/Events';
4
 import * as StatisticsEvents from '../../service/statistics/Events';
7
 import RTCStats from '../RTCStats/RTCStats';
5
 import RTCStats from '../RTCStats/RTCStats';
8
 import browser from '../browser';
6
 import browser from '../browser';
7
+import EventEmitter from '../util/EventEmitter';
9
 import WatchRTC from '../watchRTC/WatchRTC';
8
 import WatchRTC from '../watchRTC/WatchRTC';
10
 
9
 
11
 import analytics from './AnalyticsAdapter';
10
 import analytics from './AnalyticsAdapter';

+ 18
- 0
modules/util/EventEmitter.js View File

1
+import { EventEmitter as NodeEventEmitter } from 'events';
2
+
3
+/**
4
+ * The class creates our own EventEmitter instance
5
+ */
6
+export default class EventEmitter extends NodeEventEmitter {
7
+    /**
8
+     * Creates new instance.
9
+     * @constructor
10
+     */
11
+    constructor() {
12
+        super();
13
+
14
+        // aliases for addListener/removeListener
15
+        this.addEventListener = this.addListener;
16
+        this.removeEventListener = this.removeListener;
17
+    }
18
+}

+ 27
- 9
modules/util/Listenable.js View File

1
-import EventEmitter from 'events';
1
+import EventEmitter from './EventEmitter';
2
 
2
 
3
 /**
3
 /**
4
  * The class implements basic event operations - add/remove listener.
4
  * The class implements basic event operations - add/remove listener.
8
 export default class Listenable {
8
 export default class Listenable {
9
     /**
9
     /**
10
      * Creates new instance.
10
      * Creates new instance.
11
-     * @param {EventEmitter} eventEmitter
12
      * @constructor
11
      * @constructor
13
      */
12
      */
14
-    constructor(eventEmitter = new EventEmitter()) {
15
-        this.eventEmitter = eventEmitter;
13
+    constructor() {
14
+        this.eventEmitter = new EventEmitter();
16
 
15
 
17
         // aliases for addListener/removeListener
16
         // aliases for addListener/removeListener
18
         this.addEventListener = this.on = this.addListener;
17
         this.addEventListener = this.on = this.addListener;
20
     }
19
     }
21
 
20
 
22
     /**
21
     /**
23
-     * Adds new listener.
22
+     * Adds new cancellable listener.
24
      * @param {String} eventName the name of the event
23
      * @param {String} eventName the name of the event
25
      * @param {Function} listener the listener.
24
      * @param {Function} listener the listener.
26
      * @returns {Function} - The unsubscribe function.
25
      * @returns {Function} - The unsubscribe function.
27
      */
26
      */
28
-    addListener(eventName, listener) {
29
-        this.eventEmitter.addListener(eventName, listener);
27
+    addCancellableListener(eventName, listener) {
28
+        this.addListener(eventName, listener);
29
+
30
+        return () => this.removeListener(eventName, listener);
31
+    }
30
 
32
 
31
-        return () => this.removeEventListener(eventName, listener);
33
+    /**
34
+     * Adds new listener.
35
+     * @param {String} eventName the name of the event
36
+     * @param {Function} listener the listener.
37
+     * @returns {EventEmitter} - The unsubscribe function.
38
+     */
39
+    addListener(eventName, listener) {
40
+        return this.eventEmitter.addListener(eventName, listener);
32
     }
41
     }
33
 
42
 
34
     /**
43
     /**
36
      * @param {String} eventName the name of the event that triggers the
45
      * @param {String} eventName the name of the event that triggers the
37
      * listener
46
      * listener
38
      * @param {Function} listener the listener.
47
      * @param {Function} listener the listener.
48
+     * @returns {EventEmitter} - The unsubscribe function.
39
      */
49
      */
40
     removeListener(eventName, listener) {
50
     removeListener(eventName, listener) {
41
-        this.eventEmitter.removeListener(eventName, listener);
51
+        return this.eventEmitter.removeListener(eventName, listener);
52
+    }
53
+
54
+    /**
55
+     * Emits an event.
56
+     * @param {string} event - event name
57
+     */
58
+    emit(event, ...args) {
59
+        this.eventEmitter.emit(event, ...args);
42
     }
60
     }
43
 }
61
 }

+ 1
- 1
modules/xmpp/ResumeTask.js View File

51
         this._resumeRetryN += 1;
51
         this._resumeRetryN += 1;
52
 
52
 
53
         this._networkOnlineListener
53
         this._networkOnlineListener
54
-            = NetworkInfo.addEventListener(
54
+            = NetworkInfo.addCancellableListener(
55
                 NETWORK_INFO_EVENT,
55
                 NETWORK_INFO_EVENT,
56
                 ({ isOnline }) => {
56
                 ({ isOnline }) => {
57
                     if (isOnline) {
57
                     if (isOnline) {

+ 3
- 2
package.json View File

69
     "build:webpack": "LIB_JITSI_MEET_COMMIT_HASH=$(git rev-parse --short HEAD 2>/dev/null) webpack",
69
     "build:webpack": "LIB_JITSI_MEET_COMMIT_HASH=$(git rev-parse --short HEAD 2>/dev/null) webpack",
70
     "build:webpack-dev": "webpack --mode development",
70
     "build:webpack-dev": "webpack --mode development",
71
     "build:tsc": "tsc --build --clean && tsc",
71
     "build:tsc": "tsc --build --clean && tsc",
72
-    "gen-types": "tsc --declaration --declarationDir types/auto --emitDeclarationOnly",
72
+    "gen-types": "tsc --declaration --emitDeclarationOnly --out index.js",
73
     "lint": "eslint .",
73
     "lint": "eslint .",
74
     "lint-fix": "eslint . --fix",
74
     "lint-fix": "eslint . --fix",
75
     "postinstall": "patch-package",
75
     "postinstall": "patch-package",
83
   "module": "dist/esm/JitsiMeetJS.js",
83
   "module": "dist/esm/JitsiMeetJS.js",
84
   "files": [
84
   "files": [
85
     "dist",
85
     "dist",
86
-    "types"
86
+    "types",
87
+    "index.d.ts"
87
   ],
88
   ],
88
   "license": "Apache-2.0"
89
   "license": "Apache-2.0"
89
 }
90
 }

+ 1
- 4
types/hand-crafted/JitsiMediaDevices.d.ts View File

8
   isMultipleAudioInputSupported: () => boolean;
8
   isMultipleAudioInputSupported: () => boolean;
9
   getAudioOutputDevice: () => string;
9
   getAudioOutputDevice: () => string;
10
   setAudioOutputDevice: ( deviceId: string ) => Promise<unknown>; // TODO:
10
   setAudioOutputDevice: ( deviceId: string ) => Promise<unknown>; // TODO:
11
-  addEventListener: ( event: string, handler: unknown ) => void; // TODO: identify the enum for the event types and the strongly typed handlers
12
-  removeEventListener: ( event: string, handler: unknown ) => void; // TODO: identify the enum for the event types and the strongly typed handlers
13
-  emitEvent: ( event: string, ...args: unknown[] ) => void; // TODO: identify the enum for the event types
14
 }
11
 }
15
 
12
 
16
 declare var _default: JitsiMediaDevices;
13
 declare var _default: JitsiMediaDevices;
17
-export default _default;
14
+export default _default;

Loading…
Cancel
Save