Skip to content
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/fast-pots-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

fix: improve ordering of resolving waitForBufferStatus calls
25 changes: 19 additions & 6 deletions src/room/RTCEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1557,18 +1557,31 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
}
};

waitForBufferStatusLow(kind: DataChannelKind): TypedPromise<void, UnexpectedConnectionState> {
return new TypedPromise(async (resolve, reject) => {
async waitForBufferStatusLow(kind: DataChannelKind) {
return new TypedPromise<void, UnexpectedConnectionState>(async (resolve, reject) => {
if (this.isClosed) {
reject(new UnexpectedConnectionState('engine closed'));
}
if (this.isBufferStatusLow(kind)) {
resolve();
} else {
const onClosing = () => reject(new UnexpectedConnectionState('engine closed'));
this.once(EngineEvent.Closing, onClosing);
while (!this.dcBufferStatus.get(kind)) {
await sleep(10);
const dc = this.dataChannelForKind(kind);
if (!dc) {
reject(new UnexpectedConnectionState(`DataChannel not found, kind: ${kind}`));
return;
}
this.off(EngineEvent.Closing, onClosing);
resolve();
dc.addEventListener(
'bufferedamountlow',
() => {
this.off(EngineEvent.Closing, onClosing);
resolve();
},
{
once: true,
},
);
}
});
}
Expand Down
Loading