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 waitForSubscription #268

Merged
merged 2 commits into from
Sep 12, 2024
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/shaggy-poets-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/rtc-node": patch
---

add waitForSubscription
1 change: 1 addition & 0 deletions packages/livekit-rtc/src/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
this.emit(RoomEvent.LocalTrackUnpublished, publication, this.localParticipant);
} else if (ev.case == 'localTrackSubscribed') {
const publication = this.localParticipant.trackPublications.get(ev.value.trackSid);
publication.resolveFirstSubscription();
this.emit(RoomEvent.LocalTrackSubscribed, publication.track);
} else if (ev.case == 'trackPublished') {
const participant = this.remoteParticipants.get(ev.value.participantIdentity);
Expand Down
18 changes: 18 additions & 0 deletions packages/livekit-rtc/src/track_publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,26 @@ export abstract class TrackPublication {
}

export class LocalTrackPublication extends TrackPublication {
private firstSubscription: Promise<void>;
private firstSubscriptionResolver: (() => void) | null = null;

constructor(ownedInfo: OwnedTrackPublication) {
super(ownedInfo);
this.firstSubscription = new Promise<void>((resolve) => {
this.firstSubscriptionResolver = resolve;
});
}

async waitForSubscription(): Promise<void> {
await this.firstSubscription;
}

/** @internal */
resolveFirstSubscription(): void {
if (this.firstSubscriptionResolver) {
this.firstSubscriptionResolver();
this.firstSubscriptionResolver = null;
}
}
}

Expand Down
Loading