perf: clean up delay code

This commit is contained in:
Marc Rousavy 2021-06-28 20:50:35 +02:00
parent da84a0fd42
commit f1a93664ae

View File

@ -10,9 +10,6 @@ import type { Point } from './Point';
import type { TakeSnapshotOptions } from './Snapshot'; import type { TakeSnapshotOptions } from './Snapshot';
import type { RecordVideoOptions, VideoFile } from './VideoFile'; import type { RecordVideoOptions, VideoFile } from './VideoFile';
// a custom delay to wait for after `componentDidMount` because view initialization takes longer on Android.
const NATIVE_VIEW_INIT_DELAY = Platform.OS === 'android' ? 300 : 0;
//#region Types //#region Types
export type CameraPermissionStatus = 'authorized' | 'not-determined' | 'denied' | 'restricted'; export type CameraPermissionStatus = 'authorized' | 'not-determined' | 'denied' | 'restricted';
export type CameraPermissionRequestResult = 'authorized' | 'denied'; export type CameraPermissionRequestResult = 'authorized' | 'denied';
@ -366,11 +363,16 @@ export class Camera extends React.PureComponent<CameraProps> {
* @internal * @internal
*/ */
componentDidMount(): void { componentDidMount(): void {
const frameProcessor = this.props.frameProcessor; if (this.props.frameProcessor != null) {
if (frameProcessor != null) { if (Platform.OS === 'android') {
setTimeout(() => { // on Android the View is not fully mounted yet (`findViewById` returns null), so we wait 300ms.
this.setFrameProcessor(frameProcessor); setTimeout(() => {
}, NATIVE_VIEW_INIT_DELAY); if (this.props.frameProcessor != null) this.setFrameProcessor(this.props.frameProcessor);
}, 300);
} else {
// on other platforms (iOS) the View we can assume that the View is immediatelly available.
this.setFrameProcessor(this.props.frameProcessor);
}
} }
} }