Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve track acquiring and handling in usePreviewTracks #830

Merged
merged 9 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-pens-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/components-react": patch
---

Improve track acquiring and handling in usePreviewTracks
2 changes: 2 additions & 0 deletions examples/nextjs/pages/prejoin.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import * as React from 'react';
import { PreJoin, setLogLevel } from '@livekit/components-react';
import type { NextPage } from 'next';
Expand Down
41 changes: 23 additions & 18 deletions packages/react/src/prefabs/PreJoin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { ParticipantPlaceholder } from '../assets/images';
import { useMediaDevices, usePersistentUserChoices } from '../hooks';
import { useWarnAboutMissingStyles } from '../hooks/useWarnAboutMissingStyles';
import { defaultUserChoices } from '@livekit/components-core';
import { Mutex } from '../utils';

/**
* Props for the PreJoin component.
Expand Down Expand Up @@ -58,29 +59,33 @@ export function usePreviewTracks(
) {
const [tracks, setTracks] = React.useState<LocalTrack[]>();

const trackLock = new Mutex();
lukasIO marked this conversation as resolved.
Show resolved Hide resolved

React.useEffect(() => {
let trackPromise: Promise<LocalTrack[]> | undefined = undefined;
let needsCleanup = false;
if (options.audio || options.video) {
trackPromise = createLocalTracks(options);
trackPromise
.then((tracks) => {
if (needsCleanup) {
tracks.forEach((tr) => tr.stop());
} else {
setTracks(tracks);
}
})
.catch(onError);
}
trackLock.lock().then((unlock) => {
tracks?.forEach((track) => {
track.stop();
});
if (options.audio || options.video) {
createLocalTracks(options)
.then((tracks) => {
if (needsCleanup) {
tracks.forEach((tr) => tr.stop());
} else {
setTracks(tracks);
}
})
.catch((e) => (onError ? onError(e) : log.error(e)))
.finally(unlock);
}
});

return () => {
needsCleanup = true;
trackPromise?.then((tracks) =>
tracks.forEach((track) => {
track.stop();
}),
);
tracks?.forEach((track) => {
track.stop();
});
};
}, [JSON.stringify(options)]);

Expand Down
35 changes: 35 additions & 0 deletions packages/react/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,38 @@ export function useForwardedRef<T extends HTMLElement>(ref: React.ForwardedRef<T

return innerRef;
}

export class Mutex {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we export & use the version from client-sdk-js?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private _locking: Promise<void>;

private _locks: number;

constructor() {
this._locking = Promise.resolve();
this._locks = 0;
}

isLocked() {
return this._locks > 0;
}

lock() {
this._locks += 1;

let unlockNext: () => void;

const willLock = new Promise<void>(
(resolve) =>
(unlockNext = () => {
this._locks -= 1;
resolve();
}),
);

const willUnlock = this._locking.then(() => unlockNext);

this._locking = this._locking.then(() => willLock);

return willUnlock;
}
}
Loading