@@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
66Please see LICENSE files in the repository root for full details.
77*/
88
9- import React , { type JSX , useCallback , useEffect , useState } from "react" ;
9+ import React , { type JSX , useCallback , useEffect , useRef , useState } from "react" ;
1010import { type Room , type MatrixEvent , type RoomMember , RoomEvent , EventType } from "matrix-js-sdk/src/matrix" ;
1111import { Button , ToggleInput , Tooltip , TooltipProvider } from "@vector-im/compound-web" ;
1212import VideoCallIcon from "@vector-im/compound-design-tokens/assets/web/icons/video-call-solid" ;
@@ -133,13 +133,15 @@ export function IncomingCallToast({ notificationEvent }: Props): JSX.Element {
133133 setConnectedCalls ( Array . from ( CallStore . instance . connectedCalls ) ) ;
134134 } ) ;
135135 const otherCallIsOngoing = connectedCalls . find ( ( call ) => call . roomId !== roomId ) ;
136- // Start ringing if not already.
136+ const playInstance = useRef < Promise < void > > ( null ) ;
137137 useEffect ( ( ) => {
138+ // Start ringing if not already.
139+ // This section can race, so we use a ref to keep track of whether we have started trying to play.
138140 const isRingToast = notificationContent . notification_type == "ring" ;
139- if ( isRingToast && ! LegacyCallHandler . instance . isPlaying ( AudioID . Ring ) ) {
140- LegacyCallHandler . instance . play ( AudioID . Ring ) ;
141+ if ( isRingToast && ! playInstance . current && ! LegacyCallHandler . instance . isPlaying ( AudioID . Ring ) ) {
142+ playInstance . current = LegacyCallHandler . instance . play ( AudioID . Ring ) ;
141143 }
142- } , [ notificationContent . notification_type ] ) ;
144+ } , [ notificationContent . notification_type , playInstance ] ) ;
143145
144146 // Stop ringing on dismiss.
145147 const dismissToast = useCallback ( ( ) : void => {
0 commit comments