fix: Fix system/view-not-found error caused by setFrameProcessor race condition (#459)

* Remove `onLayout` hack

* Add `system/view-not-found` error
This commit is contained in:
Marc Rousavy 2021-09-27 14:35:35 +02:00 committed by GitHub
parent 17b1d1fda4
commit cbaffc20e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 18 deletions

View File

@ -1,13 +1,5 @@
import React from 'react';
import {
requireNativeComponent,
NativeModules,
NativeSyntheticEvent,
findNodeHandle,
NativeMethods,
Platform,
LayoutChangeEvent,
} from 'react-native';
import { requireNativeComponent, NativeModules, NativeSyntheticEvent, findNodeHandle, NativeMethods, Platform } from 'react-native';
import type { FrameProcessorPerformanceSuggestion } from '.';
import type { CameraDevice } from './CameraDevice';
import type { ErrorWithCause } from './CameraError';
@ -93,14 +85,18 @@ export class Camera extends React.PureComponent<CameraProps> {
this.onInitialized = this.onInitialized.bind(this);
this.onError = this.onError.bind(this);
this.onFrameProcessorPerformanceSuggestionAvailable = this.onFrameProcessorPerformanceSuggestionAvailable.bind(this);
this.onLayout = this.onLayout.bind(this);
this.ref = React.createRef<RefType>();
this.lastFrameProcessor = undefined;
}
private get handle(): number | null {
const nodeHandle = findNodeHandle(this.ref.current);
if (nodeHandle == null) console.error('Camera: findNodeHandle(ref) returned null! Does the Camera view exist in the native view tree?');
if (nodeHandle == null || nodeHandle === -1) {
throw new CameraRuntimeError(
'system/view-not-found',
"Could not get the Camera's native view tag! Does the Camera View exist in the native view-tree?",
);
}
return nodeHandle;
}
@ -370,17 +366,15 @@ export class Camera extends React.PureComponent<CameraProps> {
global.unsetFrameProcessor(this.handle);
}
private onLayout(event: LayoutChangeEvent): void {
if (!this.isNativeViewMounted) {
componentDidMount(): void {
requestAnimationFrame(() => {
this.isNativeViewMounted = true;
if (this.props.frameProcessor != null) {
// user passed a `frameProcessor` but we didn't set it yet because the native view was not mounted yet. set it now.
this.setFrameProcessor(this.props.frameProcessor);
this.lastFrameProcessor = this.props.frameProcessor;
}
}
this.props.onLayout?.(event);
});
}
/** @internal */
@ -419,7 +413,6 @@ export class Camera extends React.PureComponent<CameraProps> {
onError={this.onError}
onFrameProcessorPerformanceSuggestionAvailable={this.onFrameProcessorPerformanceSuggestionAvailable}
enableFrameProcessor={frameProcessor != null}
onLayout={this.onLayout}
/>
);
}

View File

@ -43,7 +43,7 @@ export type CaptureError =
| 'capture/photo-not-enabled'
| 'capture/aborted'
| 'capture/unknown';
export type SystemError = 'system/no-camera-manager';
export type SystemError = 'system/no-camera-manager' | 'system/view-not-found';
export type UnknownError = 'unknown/unknown';
/**