livekit-client 1.1.4 → 1.1.5

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.
@@ -10070,7 +10070,7 @@ function computeBitrate(currentStats, prevStats) {
10070
10070
  return (bytesNow - bytesPrev) * 8 * 1000 / (currentStats.timestamp - prevStats.timestamp);
10071
10071
  }
10072
10072
 
10073
- var version$1 = "1.1.4";
10073
+ var version$1 = "1.1.5";
10074
10074
 
10075
10075
  const version = version$1;
10076
10076
  const protocolVersion = 8;
@@ -10180,6 +10180,15 @@ function getEmptyAudioStreamTrack() {
10180
10180
 
10181
10181
  return emptyAudioStreamTrack;
10182
10182
  }
10183
+ class Future {
10184
+ constructor() {
10185
+ this.promise = new Promise((resolve, reject) => {
10186
+ this.resolve = resolve;
10187
+ this.reject = reject;
10188
+ });
10189
+ }
10190
+
10191
+ }
10183
10192
 
10184
10193
  const defaultId = 'default';
10185
10194
  class DeviceManager {
@@ -19427,14 +19436,18 @@ class Room extends events.exports.EventEmitter {
19427
19436
  this.audioEnabled = true;
19428
19437
 
19429
19438
  this.connect = async (url, token, opts) => {
19430
- var _a, _b; // guard against calling connect
19439
+ var _a, _b, _c, _d;
19431
19440
 
19432
-
19433
- if (this.state !== ConnectionState.Disconnected) {
19441
+ if (this.state === ConnectionState.Connected) {
19442
+ // when the state is reconnecting or connected, this function returns immediately
19434
19443
  livekitLogger.warn("already connected to room ".concat(this.name));
19435
19444
  return;
19436
19445
  }
19437
19446
 
19447
+ if (this.connectFuture) {
19448
+ return this.connectFuture.promise;
19449
+ }
19450
+
19438
19451
  this.setAndEmitConnectionState(ConnectionState.Connecting);
19439
19452
 
19440
19453
  if (!this.abortController || this.abortController.signal.aborted) {
@@ -19501,49 +19514,47 @@ class Room extends events.exports.EventEmitter {
19501
19514
  this.emit(RoomEvent.SignalConnected);
19502
19515
  } catch (err) {
19503
19516
  this.recreateEngine();
19504
- this.setAndEmitConnectionState(ConnectionState.Disconnected);
19517
+ this.setAndEmitConnectionState(ConnectionState.Disconnected, new ConnectionError('could not establish signal connection'));
19505
19518
  throw err;
19506
19519
  } // don't return until ICE connected
19507
19520
 
19508
19521
 
19509
- return new Promise((resolve, reject) => {
19510
- var _a, _b;
19522
+ const connectTimeout = setTimeout(() => {
19523
+ // timeout
19524
+ this.recreateEngine();
19525
+ this.setAndEmitConnectionState(ConnectionState.Disconnected, new ConnectionError('could not connect PeerConnection after timeout'));
19526
+ }, maxICEConnectTimeout);
19511
19527
 
19512
- const connectTimeout = setTimeout(() => {
19513
- // timeout
19514
- this.recreateEngine();
19515
- this.setAndEmitConnectionState(ConnectionState.Disconnected);
19516
- reject(new ConnectionError('could not connect after timeout'));
19517
- }, maxICEConnectTimeout);
19518
-
19519
- const abortHandler = () => {
19520
- livekitLogger.warn('closing engine');
19521
- clearTimeout(connectTimeout);
19522
- this.recreateEngine();
19523
- this.setAndEmitConnectionState(ConnectionState.Disconnected);
19524
- reject(new ConnectionError('room connection has been cancelled'));
19525
- };
19528
+ const abortHandler = () => {
19529
+ livekitLogger.warn('closing engine');
19530
+ clearTimeout(connectTimeout);
19531
+ this.recreateEngine();
19532
+ this.setAndEmitConnectionState(ConnectionState.Disconnected, new ConnectionError('room connection has been cancelled'));
19533
+ };
19526
19534
 
19527
- if ((_a = this.abortController) === null || _a === void 0 ? void 0 : _a.signal.aborted) {
19528
- abortHandler();
19529
- }
19535
+ if ((_c = this.abortController) === null || _c === void 0 ? void 0 : _c.signal.aborted) {
19536
+ abortHandler();
19537
+ }
19530
19538
 
19531
- (_b = this.abortController) === null || _b === void 0 ? void 0 : _b.signal.addEventListener('abort', abortHandler);
19532
- this.engine.once(EngineEvent.Connected, () => {
19533
- var _a, _b;
19539
+ (_d = this.abortController) === null || _d === void 0 ? void 0 : _d.signal.addEventListener('abort', abortHandler);
19540
+ this.engine.once(EngineEvent.Connected, () => {
19541
+ var _a, _b;
19534
19542
 
19535
- clearTimeout(connectTimeout);
19536
- (_a = this.abortController) === null || _a === void 0 ? void 0 : _a.signal.removeEventListener('abort', abortHandler); // also hook unload event
19543
+ clearTimeout(connectTimeout);
19544
+ (_a = this.abortController) === null || _a === void 0 ? void 0 : _a.signal.removeEventListener('abort', abortHandler); // also hook unload event
19537
19545
 
19538
- if (isWeb()) {
19539
- window.addEventListener('beforeunload', this.onBeforeUnload);
19540
- (_b = navigator.mediaDevices) === null || _b === void 0 ? void 0 : _b.addEventListener('devicechange', this.handleDeviceChange);
19541
- }
19546
+ if (isWeb()) {
19547
+ window.addEventListener('beforeunload', this.onBeforeUnload);
19548
+ (_b = navigator.mediaDevices) === null || _b === void 0 ? void 0 : _b.addEventListener('devicechange', this.handleDeviceChange);
19549
+ }
19542
19550
 
19543
- this.setAndEmitConnectionState(ConnectionState.Connected);
19544
- resolve(this);
19545
- });
19551
+ this.setAndEmitConnectionState(ConnectionState.Connected);
19546
19552
  });
19553
+
19554
+ if (this.connectFuture) {
19555
+ /** @ts-ignore */
19556
+ return this.connectFuture.promise;
19557
+ }
19547
19558
  };
19548
19559
  /**
19549
19560
  * disconnects the room, emits [[RoomEvent.Disconnected]]
@@ -20277,12 +20288,41 @@ class Room extends events.exports.EventEmitter {
20277
20288
  }
20278
20289
  }
20279
20290
 
20280
- setAndEmitConnectionState(state) {
20291
+ setAndEmitConnectionState(state, error) {
20281
20292
  if (state === this.state) {
20282
20293
  // unchanged
20283
20294
  return false;
20284
20295
  }
20285
20296
 
20297
+ switch (state) {
20298
+ case ConnectionState.Connecting:
20299
+ case ConnectionState.Reconnecting:
20300
+ if (!this.connectFuture) {
20301
+ // reuse existing connect future if possible
20302
+ this.connectFuture = new Future();
20303
+ }
20304
+
20305
+ break;
20306
+
20307
+ case ConnectionState.Connected:
20308
+ if (this.connectFuture) {
20309
+ this.connectFuture.resolve();
20310
+ this.connectFuture = undefined;
20311
+ }
20312
+
20313
+ break;
20314
+
20315
+ case ConnectionState.Disconnected:
20316
+ if (this.connectFuture) {
20317
+ error !== null && error !== void 0 ? error : error = new Error('disconnected from Room');
20318
+ this.connectFuture.reject(error);
20319
+ this.connectFuture = undefined;
20320
+ }
20321
+
20322
+ break;
20323
+
20324
+ }
20325
+
20286
20326
  this.state = state;
20287
20327
  this.emit(RoomEvent.ConnectionStateChanged, this.state);
20288
20328
  return true;