Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RoomEvent.Connected, fix connect future #431

Merged
merged 2 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?.();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ha, TIL!

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 @@ -1142,6 +1145,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?.());
}
}