Convert reaction sounds to call view model / rxjs

This commit is contained in:
Half-Shot
2024-12-09 14:58:51 +00:00
parent fbfa8c3e1d
commit 081221731e
3 changed files with 54 additions and 30 deletions

View File

@@ -642,7 +642,7 @@ export const InCallView: FC<InCallViewProps> = ({
<RoomAudioRenderer /> <RoomAudioRenderer />
{renderContent()} {renderContent()}
<CallEventAudioRenderer vm={vm} /> <CallEventAudioRenderer vm={vm} />
<ReactionsAudioRenderer /> <ReactionsAudioRenderer vm={vm} />
<ReactionsOverlay vm={vm} /> <ReactionsOverlay vm={vm} />
{footer} {footer}
{layout.type !== "pip" && ( {layout.type !== "pip" && (

View File

@@ -13,6 +13,7 @@ import { GenericReaction, ReactionSet } from "../reactions";
import { useAudioContext } from "../useAudioContext"; import { useAudioContext } from "../useAudioContext";
import { prefetchSounds } from "../soundUtils"; import { prefetchSounds } from "../soundUtils";
import { useLatest } from "../useLatest"; import { useLatest } from "../useLatest";
import { CallViewModel } from "../state/CallViewModel";
const soundMap = Object.fromEntries([ const soundMap = Object.fromEntries([
...ReactionSet.filter((v) => v.sound !== undefined).map((v) => [ ...ReactionSet.filter((v) => v.sound !== undefined).map((v) => [
@@ -22,8 +23,11 @@ const soundMap = Object.fromEntries([
[GenericReaction.name, GenericReaction.sound], [GenericReaction.name, GenericReaction.sound],
]); ]);
export function ReactionsAudioRenderer(): ReactNode { export function ReactionsAudioRenderer({
const { reactions } = useReactions(); vm,
}: {
vm: CallViewModel;
}): ReactNode {
const [shouldPlay] = useSetting(playReactionsSound); const [shouldPlay] = useSetting(playReactionsSound);
const [soundCache, setSoundCache] = useState<ReturnType< const [soundCache, setSoundCache] = useState<ReturnType<
typeof prefetchSounds typeof prefetchSounds
@@ -33,7 +37,6 @@ export function ReactionsAudioRenderer(): ReactNode {
latencyHint: "interactive", latencyHint: "interactive",
}); });
const audioEngineRef = useLatest(audioEngineCtx); const audioEngineRef = useLatest(audioEngineCtx);
const oldReactions = useDeferredValue(reactions);
useEffect(() => { useEffect(() => {
if (!shouldPlay || soundCache) { if (!shouldPlay || soundCache) {
@@ -46,26 +49,19 @@ export function ReactionsAudioRenderer(): ReactNode {
}, [soundCache, shouldPlay]); }, [soundCache, shouldPlay]);
useEffect(() => { useEffect(() => {
if (!shouldPlay || !audioEngineRef.current) { const sub = vm.audibleReactions.subscribe((newReactions) => {
return; for (const reactionName of newReactions) {
} if (soundMap[reactionName]) {
const oldReactionSet = new Set( audioEngineRef.current?.playSound(reactionName);
Object.values(oldReactions).map((r) => r.name), } else {
); // Fallback sounds.
for (const reactionName of new Set( audioEngineRef.current?.playSound("generic");
Object.values(reactions).map((r) => r.name), }
)) {
if (oldReactionSet.has(reactionName)) {
// Don't replay old reactions
return;
} }
if (soundMap[reactionName]) { });
audioEngineRef.current.playSound(reactionName); return (): void => {
} else { sub.unsubscribe();
// Fallback sounds. };
audioEngineRef.current.playSound("generic"); }, [vm, audioEngineRef]);
}
}
}, [audioEngineRef, shouldPlay, oldReactions, reactions]);
return null; return null;
} }

View File

@@ -26,6 +26,7 @@ import {
Subject, Subject,
combineLatest, combineLatest,
concat, concat,
distinct,
distinctUntilChanged, distinctUntilChanged,
filter, filter,
forkJoin, forkJoin,
@@ -66,7 +67,11 @@ import {
} from "./MediaViewModel"; } from "./MediaViewModel";
import { accumulate, finalizeValue } from "../utils/observable"; import { accumulate, finalizeValue } from "../utils/observable";
import { ObservableScope } from "./ObservableScope"; import { ObservableScope } from "./ObservableScope";
import { duplicateTiles, showReactions } from "../settings/settings"; import {
duplicateTiles,
playReactionsSound,
showReactions,
} from "../settings/settings";
import { isFirefox } from "../Platform"; import { isFirefox } from "../Platform";
import { setPipEnabled } from "../controls"; import { setPipEnabled } from "../controls";
import { GridTileViewModel, SpotlightTileViewModel } from "./TileViewModel"; import { GridTileViewModel, SpotlightTileViewModel } from "./TileViewModel";
@@ -1101,12 +1106,9 @@ export class CallViewModel extends ViewModel {
this.reactions.next(data.reactions); this.reactions.next(data.reactions);
} }
public readonly visibleReactions = combineLatest([ public readonly visibleReactions = showReactions.value
this.reactions, .pipe(switchMap((show) => (show ? this.reactions : of({}))))
showReactions.value,
])
.pipe( .pipe(
map(([reactions, setting]) => (setting ? reactions : {})),
scan< scan<
Record<string, ReactionOption>, Record<string, ReactionOption>,
{ sender: string; emoji: string; startX: number }[] { sender: string; emoji: string; startX: number }[]
@@ -1123,6 +1125,32 @@ export class CallViewModel extends ViewModel {
) )
.pipe(this.scope.state()); .pipe(this.scope.state());
public readonly audibleReactions = playReactionsSound.value
.pipe(
switchMap((show) =>
show ? this.reactions : of<Record<string, ReactionOption>>({}),
),
)
.pipe(
map((reactions) => Object.values(reactions).map((v) => v.name)),
scan<string[], { playing: string[]; newSounds: string[] }>(
(acc, latest) => {
return {
playing: latest.filter(
(v) => acc.playing.includes(v) || acc.newSounds.includes(v),
),
newSounds: latest.filter(
(v) => !acc.playing.includes(v) && !acc.newSounds.includes(v),
),
};
},
{ playing: [], newSounds: [] },
),
map((v) => v.newSounds),
distinct(),
)
.pipe(this.scope.state());
public constructor( public constructor(
// A call is permanently tied to a single Matrix room and LiveKit room // A call is permanently tied to a single Matrix room and LiveKit room
private readonly matrixRTCSession: MatrixRTCSession, private readonly matrixRTCSession: MatrixRTCSession,