Skip to content

Commit 2b8f2ab

Browse files
committed
Use Deffered for stop promise and resolver
1 parent 774fecc commit 2b8f2ab

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

packages/ai/integration/live.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,5 @@ describe('Live', function () {
323323
});
324324
*/
325325
});
326-
327-
describe('startAudioConversation', () => {
328-
it('');
329-
});
330326
});
331327
});

packages/ai/src/methods/live-session-helpers.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
Part
2626
} from '../types';
2727
import { LiveSession } from './live-session';
28+
import { Deferred } from '@firebase/util';
2829

2930
const SERVER_INPUT_SAMPLE_RATE = 16_000;
3031
const SERVER_OUTPUT_SAMPLE_RATE = 24_000;
@@ -138,12 +139,8 @@ interface RunnerDependencies {
138139
export 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

Comments
 (0)