perf: Make getCameraPermission and getMicrophonePermission synchronous (#2302)

This commit is contained in:
Marc Rousavy
2023-12-19 14:22:04 +01:00
committed by GitHub
parent 3d2feb6f6c
commit 591cf30a06
8 changed files with 28 additions and 66 deletions

View File

@@ -345,30 +345,18 @@ export class Camera extends React.PureComponent<CameraProps, CameraState> {
* the user has permitted the app to use the camera.
*
* To actually prompt the user for camera permission, use {@linkcode Camera.requestCameraPermission | requestCameraPermission()}.
*
* @throws {@linkcode CameraRuntimeError} When any kind of error occured while getting the current permission status. Use the {@linkcode CameraRuntimeError.code | code} property to get the actual error
*/
public static async getCameraPermissionStatus(): Promise<CameraPermissionStatus> {
try {
return await CameraModule.getCameraPermissionStatus()
} catch (e) {
throw tryParseNativeCameraError(e)
}
public static getCameraPermissionStatus(): CameraPermissionStatus {
return CameraModule.getCameraPermissionStatus()
}
/**
* Gets the current Microphone-Recording Permission Status. Check this before mounting the Camera to ensure
* the user has permitted the app to use the microphone.
*
* To actually prompt the user for microphone permission, use {@linkcode Camera.requestMicrophonePermission | requestMicrophonePermission()}.
*
* @throws {@linkcode CameraRuntimeError} When any kind of error occured while getting the current permission status. Use the {@linkcode CameraRuntimeError.code | code} property to get the actual error
*/
public static async getMicrophonePermissionStatus(): Promise<CameraPermissionStatus> {
try {
return await CameraModule.getMicrophonePermissionStatus()
} catch (e) {
throw tryParseNativeCameraError(e)
}
public static getMicrophonePermissionStatus(): CameraPermissionStatus {
return CameraModule.getMicrophonePermissionStatus()
}
/**
* Shows a "request permission" alert to the user, and resolves with the new camera permission status.

View File

@@ -1,4 +1,4 @@
import { useCallback, useEffect, useState } from 'react'
import { useCallback, useState } from 'react'
import { Camera } from '../Camera'
interface PermissionState {
@@ -31,7 +31,7 @@ interface PermissionState {
* ```
*/
export function useCameraPermission(): PermissionState {
const [hasPermission, setHasPermission] = useState(false)
const [hasPermission, setHasPermission] = useState(() => Camera.getCameraPermissionStatus() === 'granted')
const requestPermission = useCallback(async () => {
const result = await Camera.requestCameraPermission()
@@ -40,10 +40,6 @@ export function useCameraPermission(): PermissionState {
return hasPermissionNow
}, [])
useEffect(() => {
Camera.getCameraPermissionStatus().then((s) => setHasPermission(s === 'granted'))
}, [])
return {
hasPermission,
requestPermission,
@@ -65,7 +61,7 @@ export function useCameraPermission(): PermissionState {
* ```
*/
export function useMicrophonePermission(): PermissionState {
const [hasPermission, setHasPermission] = useState(false)
const [hasPermission, setHasPermission] = useState(() => Camera.getMicrophonePermissionStatus() === 'granted')
const requestPermission = useCallback(async () => {
const result = await Camera.requestMicrophonePermission()
@@ -74,10 +70,6 @@ export function useMicrophonePermission(): PermissionState {
return hasPermissionNow
}, [])
useEffect(() => {
Camera.getMicrophonePermissionStatus().then((s) => setHasPermission(s === 'granted'))
}, [])
return {
hasPermission,
requestPermission,