From acc06670c736c32d1ccef53da6c1ff7c53a3673c Mon Sep 17 00:00:00 2001 From: Zita Szupera Date: Wed, 25 Sep 2024 12:56:17 +0200 Subject: [PATCH] review fixes --- .../ts-quickstart/src/closed-captions.ts | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/sample-apps/client/ts-quickstart/src/closed-captions.ts b/sample-apps/client/ts-quickstart/src/closed-captions.ts index a5b9791a66..3837c2cfae 100644 --- a/sample-apps/client/ts-quickstart/src/closed-captions.ts +++ b/sample-apps/client/ts-quickstart/src/closed-captions.ts @@ -7,9 +7,19 @@ import { export class ClosedCaptionManager { status: 'on' | 'off' = 'off'; private unsubscribe?: () => void; - private captionRefreshInterval?: ReturnType; + private captionTimeout?: ReturnType; private captions: (CallClosedCaption & { speaker_name?: string })[] = []; private captionContainer?: HTMLElement; + /** + * A single caption can stay visible on the screen for this duration + * + * This is the maximum duration, new captions can push a caption out of the screen sooner + */ + private captionTimeoutMs = 2700; + /** + * The maximum number of captions that can be visible on the screen + */ + private numberOfCaptionsVisible = 2; constructor(private call: Call) {} @@ -55,13 +65,14 @@ export class ClosedCaptionManager { const speakerName = speaker?.name || speaker?.userId; this.captions.push({ ...caption, speaker_name: speakerName }); this.updateDisplayedCaptions(); + this.captionTimeout = setTimeout(() => { + this.captions = this.captions.slice(1); + this.updateDisplayedCaptions(); + this.captionTimeout = undefined; + }, this.captionTimeoutMs); } }, ); - this.captionRefreshInterval = setInterval(() => { - this.captions = this.captions.slice(1); - this.updateDisplayedCaptions(); - }, 2700); } hideCaptions() { @@ -71,7 +82,7 @@ export class ClosedCaptionManager { cleanup() { this.unsubscribe?.(); - clearInterval(this.captionRefreshInterval); + clearTimeout(this.captionTimeout); } private updateDisplayedCaptions() { @@ -81,9 +92,11 @@ export class ClosedCaptionManager { ); return; } - const displayedCaptions = this.captions.slice(-2); + const displayedCaptions = this.captions.slice( + -1 * this.numberOfCaptionsVisible, + ); this.captionContainer.innerHTML = displayedCaptions .map((c) => `${c.speaker_name}: ${c.text}`) - .join('\n'); + .join('
'); } }