@@ -25,6 +25,7 @@ import {
25
25
Part
26
26
} from '../types' ;
27
27
import { LiveSession } from './live-session' ;
28
+ import { Deferred } from '@firebase/util' ;
28
29
29
30
const SERVER_INPUT_SAMPLE_RATE = 16_000 ;
30
31
const SERVER_OUTPUT_SAMPLE_RATE = 24_000 ;
@@ -138,12 +139,8 @@ interface RunnerDependencies {
138
139
export class AudioConversationRunner {
139
140
/** A flag to indicate if the conversation has been stopped. */
140
141
private isStopped = false ;
141
- /** A resolver function for the `stopPromise`. */
142
- private stopResolver ! : ( ) => void ;
143
- /** A promise that resolves when `stop()` is called, used to unblock the receive loop. */
144
- private readonly stopPromise = new Promise < void > (
145
- resolve => ( this . stopResolver = resolve )
146
- ) ;
142
+ /** A deferred that contains a promise that is resolved when stop() is called, to unblock the receive loop. */
143
+ private readonly stopDeferred = new Deferred < void > ( ) ;
147
144
/** A promise that tracks the lifecycle of the main `runReceiveLoop`. */
148
145
private readonly receiveLoopPromise : Promise < void > ;
149
146
@@ -199,7 +196,8 @@ export class AudioConversationRunner {
199
196
return ;
200
197
}
201
198
this . isStopped = true ;
202
- this . stopResolver ( ) ; // Unblock the receive loop
199
+ this . stopDeferred . resolve ( ) ;
200
+ // this.stopResolver(); // Unblock the receive loop
203
201
await this . receiveLoopPromise ; // Wait for the loop and cleanup to finish
204
202
}
205
203
@@ -310,7 +308,7 @@ export class AudioConversationRunner {
310
308
while ( ! this . isStopped ) {
311
309
const result = await Promise . race ( [
312
310
messageGenerator . next ( ) ,
313
- this . stopPromise
311
+ this . stopDeferred . promise
314
312
] ) ;
315
313
316
314
if ( this . isStopped || ! result || result . done ) {
0 commit comments