Skip to content

Commit

Permalink
Add RoomEvent.Connected, fix connect future (#431)
Browse files Browse the repository at this point in the history
* add connected event, fix connect future

* changeset
  • Loading branch information
lukasIO committed Sep 12, 2022
1 parent e4ead3d commit 4004426
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-rice-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Add RoomEvent.Connected, fix connectFuture rejection exception
18 changes: 11 additions & 7 deletions src/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
})
.on(EngineEvent.Resumed, () => {
this.setAndEmitConnectionState(ConnectionState.Connected);
this.reconnectFuture?.resolve();
this.reconnectFuture?.resolve?.();
this.reconnectFuture = undefined;
this.emit(RoomEvent.Reconnected);
this.updateSubscriptions();
Expand Down Expand Up @@ -218,7 +218,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
this.connectFuture = this.reconnectFuture;
return this.connectFuture.promise;
}
const connectPromise = new Promise<void>(async (resolve, reject) => {
const connectFn = async (resolve: () => void, reject: (reason: any) => void) => {
this.setAndEmitConnectionState(ConnectionState.Connecting);
if (!this.abortController || this.abortController.signal.aborted) {
this.abortController = new AbortController();
Expand Down Expand Up @@ -347,8 +347,11 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
this.setAndEmitConnectionState(ConnectionState.Connected);
resolve();
});
}).finally(() => (this.connectFuture = undefined));
this.connectFuture = new Future(connectPromise);
};
this.connectFuture = new Future(connectFn, () => {
this.connectFuture = undefined;
this.emit(RoomEvent.Connected);
});

return this.connectFuture.promise;
};
Expand All @@ -363,7 +366,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
log.warn('abort connection attempt');
this.abortController?.abort();
// in case the abort controller didn't manage to cancel the connection attempt, reject the connect promise explicitly
this.connectFuture?.reject(new ConnectionError('Client initiated disconnect'));
this.connectFuture?.reject?.(new ConnectionError('Client initiated disconnect'));
this.connectFuture = undefined;
}
// send leave
Expand Down Expand Up @@ -625,7 +628,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
});
this.setAndEmitConnectionState(ConnectionState.Connected);
this.emit(RoomEvent.Reconnected);
this.reconnectFuture?.resolve();
this.reconnectFuture?.resolve?.();
this.reconnectFuture = undefined;

// rehydrate participants
Expand Down Expand Up @@ -672,7 +675,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
}
// reject potentially ongoing reconnection attempt
if (this.connectFuture === this.reconnectFuture) {
this.connectFuture?.reject(undefined);
this.connectFuture?.reject?.(undefined);
this.connectFuture = undefined;
}
this.reconnectFuture = undefined;
Expand Down Expand Up @@ -1152,6 +1155,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
export default Room;

export type RoomEventCallbacks = {
connected: () => void;
reconnecting: () => void;
reconnected: () => void;
disconnected: (reason?: DisconnectReason) => void;
Expand Down
5 changes: 5 additions & 0 deletions src/room/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/

export enum RoomEvent {
/**
* When the connection to the server has been established
*/
Connected = 'connected',

/**
* When the connection to the server has been interrupted and it's attempting
* to reconnect.
Expand Down
29 changes: 18 additions & 11 deletions src/room/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,23 @@ export function getEmptyAudioStreamTrack() {
export class Future<T> {
promise: Promise<T>;

resolve!: (arg: T) => void;

reject!: (e: any) => void;

constructor(promise?: Promise<T>) {
this.promise =
promise ??
new Promise<T>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
resolve?: (arg: T) => void;

reject?: (e: any) => void;

onFinally?: () => void;

constructor(
futureBase?: (resolve: (arg: T) => void, reject: (e: any) => void) => void,
onFinally?: () => void,
) {
this.onFinally = onFinally;
this.promise = new Promise<T>(async (resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
if (futureBase) {
await futureBase(resolve, reject);
}
}).finally(() => this.onFinally?.());
}
}

0 comments on commit 4004426

Please sign in to comment.