@@ -4,7 +4,7 @@ import { EventEmitter } from 'events';
4
4
import { useCallback , useEffect , useMemo , useState } from 'react' ;
5
5
6
6
import {
7
- TokenSourceBase ,
7
+ TokenSource ,
8
8
TokenSourceConfigurable ,
9
9
TokenSourceFixed ,
10
10
TokenSourceOptions ,
@@ -52,7 +52,7 @@ type SessionStateCommon = {
52
52
subtle : {
53
53
emitter : TypedEventEmitter < SessionCallbacks > ;
54
54
room : Room ;
55
- tokenSource : TokenSourceBase ,
55
+ tokenSource : TokenSource ,
56
56
agentConnectTimeoutMilliseconds ?: number ;
57
57
} ;
58
58
} ;
@@ -137,19 +137,6 @@ export function useSession(
137
137
138
138
const emitter = useMemo ( ( ) => new EventEmitter ( ) as TypedEventEmitter < SessionCallbacks > , [ ] ) ;
139
139
140
- // Flexible `TokenSource`s can have options injected
141
- useEffect ( ( ) => {
142
- const isConfigurable = tokenSource instanceof TokenSourceConfigurable ;
143
- if ( ! isConfigurable ) {
144
- return ;
145
- }
146
-
147
- tokenSource . setOptions ( tokenSourceOptions ) ;
148
- return ( ) => {
149
- tokenSource . clearOptions ( ) ;
150
- } ;
151
- } , [ tokenSource , tokenSourceOptions ] ) ;
152
-
153
140
const generateDerivedConnectionStateValues = useCallback ( < State extends UseSessionReturn [ "connectionState" ] > ( connectionState : State ) => ( {
154
141
isConnected : (
155
142
connectionState === ConnectionState . Connected ||
@@ -330,6 +317,15 @@ export function useSession(
330
317
} ,
331
318
} ) , [ conversationState , emitter , room , tokenSource ] ) ) ;
332
319
320
+ const tokenSourceGetToken = useCallback ( ( ) => {
321
+ const isConfigurable = tokenSource instanceof TokenSourceConfigurable ;
322
+ if ( isConfigurable ) {
323
+ return tokenSource . getToken ( tokenSourceOptions ) ;
324
+ } else {
325
+ return tokenSource . getToken ( ) ;
326
+ }
327
+ } , [ tokenSource ] ) ;
328
+
333
329
const start = useCallback ( async ( connectOptions : SessionConnectOptions = { } ) => {
334
330
const {
335
331
signal,
@@ -345,8 +341,8 @@ export function useSession(
345
341
346
342
await Promise . all ( [
347
343
// FIXME: swap the below line in once the new `livekit-client` changes are published
348
- // room.connect(options.credentials ),
349
- tokenSource . getToken ( ) . then ( ( { serverUrl, participantToken } ) => (
344
+ // room.connect(tokenSource ),
345
+ tokenSourceGetToken ( ) . then ( ( { serverUrl, participantToken } ) => (
350
346
room . connect ( serverUrl , participantToken )
351
347
) ) ,
352
348
@@ -360,16 +356,18 @@ export function useSession(
360
356
await agent . waitUntilAvailable ( signal ) ;
361
357
362
358
signal ?. removeEventListener ( 'abort' , onSignalAbort ) ;
363
- } , [ room , waitUntilDisconnected , tokenSource , waitUntilConnected , agent . waitUntilAvailable ] ) ;
359
+ } , [ room , waitUntilDisconnected , tokenSourceGetToken , waitUntilConnected , agent . waitUntilAvailable ] ) ;
364
360
365
361
const end = useCallback ( async ( ) => {
366
362
await room . disconnect ( ) ;
367
363
} , [ room ] ) ;
368
364
369
365
const prepareConnection = useCallback ( async ( ) => {
370
- const credentials = await tokenSource . getToken ( ) ;
366
+ const credentials = await tokenSourceGetToken ( ) ;
367
+ // FIXME: swap the below line in once the new `livekit-client` changes are published
368
+ // room.prepareConnection(tokenSource),
371
369
await room . prepareConnection ( credentials . serverUrl , credentials . participantToken ) ;
372
- } , [ tokenSource , room ] ) ;
370
+ } , [ tokenSourceGetToken , room ] ) ;
373
371
useEffect ( ( ) => {
374
372
prepareConnection ( ) . catch ( err => {
375
373
// FIXME: figure out a better logging solution?
0 commit comments