Skip to content

Commit

Permalink
fix: #155
Browse files Browse the repository at this point in the history
  • Loading branch information
guoxianzhe committed Oct 27, 2023
1 parent ec3e53e commit 9954449
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
32 changes: 23 additions & 9 deletions packages/agora-rtc-react/src/hooks/useJoin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { IAgoraRTCClient, IAgoraRTCError, UID } from "agora-rtc-sdk-ng";
import { useState } from "react";
import { useRef, useState } from "react";

import { AgoraRTCReactError } from "../error";
import { useRTCClient } from "../hooks/useRTCClient";
import type { AsyncTaskRunner } from "../misc/utils";
import { createAsyncTaskRunner, joinDisposers } from "../misc/utils";
import type { FetchArgs } from "../types";

import { useAsyncEffect, useIsUnmounted } from "./tools";
Expand Down Expand Up @@ -47,6 +49,7 @@ export function useJoin(
): { data: UID; isLoading: boolean; isConnected: boolean; error: AgoraRTCReactError | null } {
const resolvedClient = useRTCClient(client);
const isConnected = useIsConnected(client);
const runnerRef = useRef<AsyncTaskRunner | undefined>();

const [isLoading, setIsLoading] = useState(false);
const [joinResult, setJoinResult] = useState<UID>(0);
Expand Down Expand Up @@ -80,15 +83,26 @@ export function useJoin(
if (!isUnmountRef.current) {
setIsLoading(false);
}
return () => {
for (const track of resolvedClient.localTracks) {
if (track.isPlaying) {
track.stop();

const runner = (runnerRef.current ||= createAsyncTaskRunner());

return joinDisposers([
() => {
runner.dispose();
},
() => {
runner.run(() => resolvedClient.unpublish(resolvedClient.localTracks));
},
() => {
for (const track of resolvedClient.localTracks) {
if (track.isPlaying) {
track.stop();
}
track.close();
}
track.close();
}
return resolvedClient.leave();
};
runner.run(() => resolvedClient.leave());
},
]);
}
}, [ready, client]);
return {
Expand Down
5 changes: 5 additions & 0 deletions packages/agora-rtc-react/src/hooks/usePublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export function usePublish(
const track = filterTracks[i];
if (track) {
if (canPublish(track)) {
await resolvedClient.unpublish(
pubTracks.current.filter(
pubTrack => pubTrack?.trackMediaType === track.trackMediaType,
) as ILocalTrack[],
);
try {
if (!isUnmountRef.current) {
setIsLoading(true);
Expand Down
3 changes: 0 additions & 3 deletions packages/agora-rtc-react/src/hooks/useRemoteAudioTracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ export function useRemoteAudioTracks(
useAsyncEffect(async () => {
if (!isUnmountRef.current) {
setError(null);
if (!isConnected) {
setTracks([]);
}
}

if (!Array.isArray(users) || !isConnected) return;
Expand Down
3 changes: 0 additions & 3 deletions packages/agora-rtc-react/src/hooks/useRemoteVideoTracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ export function useRemoteVideoTracks(
useAsyncEffect(async () => {
if (!isUnmountRef.current) {
setError(null);
if (!isConnected) {
setTracks([]);
}
}

if (!Array.isArray(users) || !isConnected) return;
Expand Down

0 comments on commit 9954449

Please sign in to comment.