@@ -25,6 +25,7 @@ import {
2525 Part
2626} from '../types' ;
2727import { LiveSession } from './live-session' ;
28+ import { Deferred } from '@firebase/util' ;
2829
2930const SERVER_INPUT_SAMPLE_RATE = 16_000 ;
3031const SERVER_OUTPUT_SAMPLE_RATE = 24_000 ;
@@ -138,12 +139,8 @@ interface RunnerDependencies {
138139export class AudioConversationRunner {
139140 /** A flag to indicate if the conversation has been stopped. */
140141 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 > ( ) ;
147144 /** A promise that tracks the lifecycle of the main `runReceiveLoop`. */
148145 private readonly receiveLoopPromise : Promise < void > ;
149146
@@ -199,7 +196,8 @@ export class AudioConversationRunner {
199196 return ;
200197 }
201198 this . isStopped = true ;
202- this . stopResolver ( ) ; // Unblock the receive loop
199+ this . stopDeferred . resolve ( ) ;
200+ // this.stopResolver(); // Unblock the receive loop
203201 await this . receiveLoopPromise ; // Wait for the loop and cleanup to finish
204202 }
205203
@@ -310,7 +308,7 @@ export class AudioConversationRunner {
310308 while ( ! this . isStopped ) {
311309 const result = await Promise . race ( [
312310 messageGenerator . next ( ) ,
313- this . stopPromise
311+ this . stopDeferred . promise
314312 ] ) ;
315313
316314 if ( this . isStopped || ! result || result . done ) {
0 commit comments