Skip to content

Commit

Permalink
Improve track acquiring and handling in usePreviewTracks (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO authored Apr 18, 2024
1 parent 2da35e7 commit ccd551f
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 37 deletions.
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
6 changes: 6 additions & 0 deletions .changeset/many-geese-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@livekit/components-core": minor
"@livekit/components-react": minor
---

Require livekit-client ^2.1.0 peer dependency
2 changes: 1 addition & 1 deletion docs/storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@livekit/components-react": "workspace:*",
"@livekit/components-styles": "workspace:*",
"livekit-client": "^2.0.10",
"livekit-client": "^2.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@livekit/components-react": "workspace:*",
"@livekit/components-styles": "workspace:*",
"livekit-client": "^2.0.10",
"livekit-client": "^2.1.0",
"livekit-server-sdk": "^1.2.7",
"next": "^12.3.4",
"react": "^18.2.0",
Expand Down
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
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"rxjs": "7.8.1"
},
"peerDependencies": {
"livekit-client": "^2.0.10",
"livekit-client": "^2.1.0",
"@livekit/protocol": "^1.12.0",
"tslib": "^2.6.2"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"usehooks-ts": "2.16.0"
},
"peerDependencies": {
"livekit-client": "^2.0.10",
"livekit-client": "^2.1.0",
"@livekit/protocol": "^1.12.0",
"react": ">=18",
"react-dom": ">=18",
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 @@ -11,6 +11,7 @@ import {
facingModeFromLocalTrack,
Track,
VideoPresets,
Mutex,
} from 'livekit-client';
import * as React from 'react';
import { MediaDeviceMenu } from './MediaDeviceMenu';
Expand Down Expand Up @@ -58,29 +59,33 @@ export function usePreviewTracks(
) {
const [tracks, setTracks] = React.useState<LocalTrack[]>();

const trackLock = React.useMemo(() => new Mutex(), []);

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
30 changes: 15 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ccd551f

Please sign in to comment.