Skip to content

Commit 09b09eb

Browse files
authored
Make useSession wait for agent only if an agent was dispatched (#1226)
1 parent ffd6617 commit 09b09eb

File tree

4 files changed

+53
-33
lines changed

4 files changed

+53
-33
lines changed

.changeset/slick-cougars-pick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@livekit/components-react': patch
3+
---
4+
5+
Make useSession wait for agent only if an agent was dispatched

packages/react/src/hooks/useSession.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
TokenSourceFixed,
1111
TokenSourceFetchOptions,
1212
RoomConnectOptions,
13+
decodeTokenPayload,
1314
} from 'livekit-client';
1415
import { EventEmitter } from 'events';
1516

@@ -451,12 +452,18 @@ export function useSession(
451452
};
452453
signal?.addEventListener('abort', onSignalAbort);
453454

455+
let tokenDispatchesAgent = false;
454456
await Promise.all([
455457
// FIXME: swap the below line in once the new `livekit-client` changes are published
456458
// room.connect(tokenSource, { tokenSourceOptions }),
457-
tokenSourceFetch().then(({ serverUrl, participantToken }) =>
458-
room.connect(serverUrl, participantToken, roomConnectOptions),
459-
),
459+
tokenSourceFetch().then(({ serverUrl, participantToken }) => {
460+
const participantTokenPayload = decodeTokenPayload(participantToken);
461+
const participantTokenAgentDispatchCount =
462+
participantTokenPayload.roomConfig?.agents?.length ?? 0;
463+
tokenDispatchesAgent = participantTokenAgentDispatchCount > 0;
464+
465+
return room.connect(serverUrl, participantToken, roomConnectOptions);
466+
}),
460467

461468
// Start microphone (with preconnect buffer) by default
462469
tracks.microphone?.enabled
@@ -469,7 +476,9 @@ export function useSession(
469476
]);
470477

471478
await waitUntilConnected(signal);
472-
await agent.waitUntilAvailable(signal);
479+
if (tokenDispatchesAgent) {
480+
await agent.waitUntilAvailable(signal);
481+
}
473482

474483
signal?.removeEventListener('abort', onSignalAbort);
475484
},

pnpm-lock.yaml

Lines changed: 34 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ packages:
55
- 'tooling/*'
66

77
catalog:
8-
livekit-client: ^2.15.8
8+
livekit-client: ^2.15.14
99
"@livekit/protocol": ^1.42.0

0 commit comments

Comments
 (0)