From 52058716f627f45473bc799c35c9798e1548be3e Mon Sep 17 00:00:00 2001 From: Robin Date: Fri, 9 Aug 2024 13:06:33 -0400 Subject: [PATCH 1/2] Don't keep someone in the spotlight if they've left the call --- src/state/CallViewModel.ts | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/state/CallViewModel.ts b/src/state/CallViewModel.ts index 2cb28f2e..ca37b4e4 100644 --- a/src/state/CallViewModel.ts +++ b/src/state/CallViewModel.ts @@ -424,21 +424,25 @@ export class CallViewModel extends ViewModel { ), scan<(readonly [UserMedia, boolean])[], UserMedia, null>( (prev, mediaItems) => { - const stickyPrev = prev === null || prev.vm.local ? null : prev; + // Only remote users that are still in the call should be sticky + const stickyPrev = + prev === null || prev.vm.local + ? null + : mediaItems.find(([m]) => m === prev); + const stillSpeaking = stickyPrev?.[1]; // Decide who to spotlight: - // If the previous speaker (not the local user) is still speaking, - // stick with them rather than switching eagerly to someone else - return ( - mediaItems.find(([m, s]) => m === stickyPrev && s)?.[0] ?? - // Otherwise, select any remote user who is speaking - mediaItems.find(([m, s]) => !m.vm.local && s)?.[0] ?? - // Otherwise, stick with the person who was last speaking - stickyPrev ?? - // Otherwise, spotlight an arbitrary remote user - mediaItems.find(([m]) => !m.vm.local)?.[0] ?? - // Otherwise, spotlight the local user - mediaItems.find(([m]) => m.vm.local)![0] - ); + // If the previous speaker is still speaking, stick with them rather + // than switching eagerly to someone else + return stillSpeaking + ? stickyPrev[0] + : // Otherwise, select any remote user who is speaking + (mediaItems.find(([m, s]) => !m.vm.local && s)?.[0] ?? + // Otherwise, stick with the person who was last speaking + stickyPrev?.[0] ?? + // Otherwise, spotlight an arbitrary remote user + mediaItems.find(([m]) => !m.vm.local)?.[0] ?? + // Otherwise, spotlight the local user + mediaItems.find(([m]) => m.vm.local)![0]); }, null, ), From ed99af0be6dec159c995df1c916eeaf519b62583 Mon Sep 17 00:00:00 2001 From: Robin Date: Fri, 9 Aug 2024 13:38:59 -0400 Subject: [PATCH 2/2] Improve readability --- src/state/CallViewModel.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/state/CallViewModel.ts b/src/state/CallViewModel.ts index ca37b4e4..c68869f9 100644 --- a/src/state/CallViewModel.ts +++ b/src/state/CallViewModel.ts @@ -425,20 +425,17 @@ export class CallViewModel extends ViewModel { scan<(readonly [UserMedia, boolean])[], UserMedia, null>( (prev, mediaItems) => { // Only remote users that are still in the call should be sticky - const stickyPrev = - prev === null || prev.vm.local - ? null - : mediaItems.find(([m]) => m === prev); - const stillSpeaking = stickyPrev?.[1]; + const [stickyMedia, stickySpeaking] = + (!prev?.vm.local && mediaItems.find(([m]) => m === prev)) || []; // Decide who to spotlight: // If the previous speaker is still speaking, stick with them rather // than switching eagerly to someone else - return stillSpeaking - ? stickyPrev[0] + return stickySpeaking + ? stickyMedia! : // Otherwise, select any remote user who is speaking (mediaItems.find(([m, s]) => !m.vm.local && s)?.[0] ?? // Otherwise, stick with the person who was last speaking - stickyPrev?.[0] ?? + stickyMedia ?? // Otherwise, spotlight an arbitrary remote user mediaItems.find(([m]) => !m.vm.local)?.[0] ?? // Otherwise, spotlight the local user