Skip to content

Commit

Permalink
fix: requestPermission should be no-op when permission is already gra…
Browse files Browse the repository at this point in the history
…nted
  • Loading branch information
oliverlaz committed Oct 2, 2023
1 parent 86d2583 commit 6fb0bc7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
17 changes: 6 additions & 11 deletions packages/react-sdk/src/hooks/useRequestPermission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { useCall, useHasPermissions } from '@stream-io/video-react-bindings';
export const useRequestPermission = (permission: OwnCapability) => {
const call = useCall();
const hasPermission = useHasPermissions(permission);
const canRequestPermission =
!!call?.permissionsContext.canRequest(permission);
const [isAwaitingPermission, setIsAwaitingPermission] = useState(false); // TODO: load with possibly pending state

useEffect(() => {
Expand All @@ -16,9 +14,12 @@ export const useRequestPermission = (permission: OwnCapability) => {
}, [hasPermission]);

const requestPermission = useCallback(async () => {
if (isAwaitingPermission || !canRequestPermission) return false;
if (hasPermission) return true;

const canRequestPermission =
!!call?.permissionsContext.canRequest(permission);
if (isAwaitingPermission || !canRequestPermission) return false;

setIsAwaitingPermission(true);

try {
Expand All @@ -31,18 +32,12 @@ export const useRequestPermission = (permission: OwnCapability) => {
}

return false;
}, [
call,
canRequestPermission,
hasPermission,
isAwaitingPermission,
permission,
]);
}, [call, hasPermission, isAwaitingPermission, permission]);

return {
requestPermission,
hasPermission,
canRequestPermission,
canRequestPermission: !!call?.permissionsContext.canRequest(permission),
isAwaitingPermission,
};
};
2 changes: 1 addition & 1 deletion packages/react-sdk/src/hooks/useToggleAudioMuteState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const useToggleAudioMuteState = () => {
if (canPublish) return publishAudioStream();
}

if (!isAudioMutedReference.current) stopPublishingAudio();
if (!isAudioMutedReference.current) await stopPublishingAudio();
}, [publishAudioStream, requestPermission, stopPublishingAudio]);

return { toggleAudioMuteState, isAwaitingPermission };
Expand Down
2 changes: 1 addition & 1 deletion packages/react-sdk/src/hooks/useToggleScreenShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const useToggleScreenShare = () => {
}
}

call?.stopPublish(SfuModels.TrackType.SCREEN_SHARE);
await call?.stopPublish(SfuModels.TrackType.SCREEN_SHARE);
}, [call, requestPermission]);

return { toggleScreenShare, isAwaitingPermission, isScreenSharing };
Expand Down
2 changes: 1 addition & 1 deletion packages/react-sdk/src/hooks/useToggleVideoMuteState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const useToggleVideoMuteState = () => {
if (canPublish) return publishVideoStream();
}

if (!isVideoMutedReference.current) stopPublishingVideo();
if (!isVideoMutedReference.current) await stopPublishingVideo();
}, [publishVideoStream, requestPermission, stopPublishingVideo]);

return { toggleVideoMuteState, isAwaitingPermission };
Expand Down

0 comments on commit 6fb0bc7

Please sign in to comment.