From b10b2c10fc6084f13a3a667b970910c0d7541b34 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Mon, 28 Jun 2021 18:18:35 +0200 Subject: [PATCH] fix: Frame Processor not setting on first render --- src/Camera.tsx | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/Camera.tsx b/src/Camera.tsx index 7ce6650..afd1b4a 100644 --- a/src/Camera.tsx +++ b/src/Camera.tsx @@ -330,6 +330,7 @@ export class Camera extends React.PureComponent { } //#endregion + //#region Lifecycle /** * @internal */ @@ -339,15 +340,30 @@ export class Camera extends React.PureComponent { throw new Error('Frame Processors are not enabled. Make sure you install react-native-reanimated 2.2.0 or above!'); } + private setFrameProcessor(frameProcessor: (frame: Frame) => void): void { + this.assertFrameProcessorsEnabled(); + // @ts-expect-error JSI functions aren't typed + global.setFrameProcessor(this.handle, frameProcessor); + } + + private unsetFrameProcessor(): void { + this.assertFrameProcessorsEnabled(); + // @ts-expect-error JSI functions aren't typed + global.setFrameProcessor(this.handle, this.props.frameProcessor); + } + /** * @internal */ componentWillUnmount(): void { - if (this.lastFrameProcessor != null || this.props.frameProcessor != null) { - this.assertFrameProcessorsEnabled(); - // @ts-expect-error JSI functions aren't typed - global.unsetFrameProcessor(this.handle); - } + if (this.lastFrameProcessor != null || this.props.frameProcessor != null) this.unsetFrameProcessor(); + } + + /** + * @internal + */ + componentDidMount(): void { + if (this.props.frameProcessor != null) this.setFrameProcessor(this.props.frameProcessor); } /** @@ -355,24 +371,14 @@ export class Camera extends React.PureComponent { */ componentDidUpdate(): void { if (this.props.frameProcessor !== this.lastFrameProcessor) { - this.assertFrameProcessorsEnabled(); - // frameProcessor argument changed. Update native to reflect the change. - if (this.props.frameProcessor != null) { - // 1. Spawn threaded JSI Runtime (if not already done) - // 2. Add video data output to Camera stream (if not already done) - // 3. Workletize the frameProcessor and prepare it for being called with frames - // @ts-expect-error JSI functions aren't typed - global.setFrameProcessor(this.handle, this.props.frameProcessor); - } else { - // 1. Destroy the threaded runtime - // 2. remove the frame processor - // 3. Remove the video data output - // @ts-expect-error JSI functions aren't typed - global.unsetFrameProcessor(this.handle); - } + // frameProcessor argument identity changed. Update native to reflect the change. + if (this.props.frameProcessor != null) this.setFrameProcessor(this.props.frameProcessor); + else this.unsetFrameProcessor(); + this.lastFrameProcessor = this.props.frameProcessor; } } + //#endregion /** * @internal