@@ -27,6 +27,7 @@ export const useTrackManager = ({
2727 logger,
2828} : TrackManagerConfig ) : TrackManager => {
2929 const currentTrackIdRef = useRef < string | null > ( null ) ;
30+ const connectionPromiseRef = useRef < Promise < string > | null > ( null ) ;
3031
3132 const {
3233 startDevice,
@@ -43,7 +44,10 @@ export const useTrackManager = ({
4344 // every time it changes.
4445 const getDeviceTrack = useCurrentCallback ( ( ) => deviceTrack ) ;
4546
46- const getCurrentTrackId = ( ) : string | null => {
47+ const getCurrentTrackId = async ( ) : Promise < string | null > => {
48+ if ( connectionPromiseRef . current ) {
49+ await connectionPromiseRef . current ;
50+ }
4751 const refTrackId = currentTrackIdRef . current ;
4852 if ( ! refTrackId ) return null ;
4953 const currentTrack = getRemoteOrLocalTrack ( tsClient , refTrackId ) ;
@@ -57,7 +61,7 @@ export const useTrackManager = ({
5761 const [ newTrack , error ] = result ;
5862 if ( error ) return error ;
5963
60- const currentTrackId = getCurrentTrackId ( ) ;
64+ const currentTrackId = await getCurrentTrackId ( ) ;
6165 if ( ! currentTrackId ) return ;
6266
6367 await tsClient . replaceTrack ( currentTrackId , newTrack ) ;
@@ -66,7 +70,7 @@ export const useTrackManager = ({
6670 const setTrackMiddleware = useCurrentCallback ( async ( middleware : TrackMiddleware ) => {
6771 const processedTrack = await applyMiddleware ( middleware ) ;
6872
69- const currentTrackId = getCurrentTrackId ( ) ;
73+ const currentTrackId = await getCurrentTrackId ( ) ;
7074 if ( ! currentTrackId ) return ;
7175
7276 await tsClient . replaceTrack ( currentTrackId , processedTrack ) ;
@@ -90,8 +94,9 @@ export const useTrackManager = ({
9094 const [ maxBandwidth , simulcastConfig ] = getConfigAndBandwidthFromProps ( props . sentQualities , bandwidthLimits ) ;
9195
9296 try {
93- const remoteTrackId = await tsClient . addTrack ( track , trackMetadata , simulcastConfig , maxBandwidth ) ;
94-
97+ const addTrackJob = tsClient . addTrack ( track , trackMetadata , simulcastConfig , maxBandwidth ) ;
98+ connectionPromiseRef . current = addTrackJob ;
99+ const remoteTrackId = await addTrackJob ;
95100 currentTrackIdRef . current = remoteTrackId ;
96101 } catch ( err ) {
97102 if ( err instanceof TrackTypeError ) {
@@ -119,7 +124,7 @@ export const useTrackManager = ({
119124 * @see {@link TrackManager#toggleMute } for more details.
120125 */
121126 const toggleMute = useCurrentCallback ( async ( ) => {
122- const currentTrackId = getCurrentTrackId ( ) ;
127+ const currentTrackId = await getCurrentTrackId ( ) ;
123128 const isTrackCurrentlyEnabled = Boolean ( deviceTrack ?. enabled ) ;
124129 if ( ! currentTrackId ) {
125130 logger . warn ( "Toggling mute is only possible while connected to a room." ) ;
@@ -139,7 +144,7 @@ export const useTrackManager = ({
139144 * @see {@link TrackManager#toggleDevice } for more details.
140145 */
141146 const toggleDevice = useCurrentCallback ( async ( ) => {
142- const currentTrackId = getCurrentTrackId ( ) ;
147+ const currentTrackId = await getCurrentTrackId ( ) ;
143148 if ( deviceTrack ) {
144149 stopDevice ( ) ;
145150 if ( currentTrackId ) {
0 commit comments