Skip to content

Commit b10b2c1

Browse files
committed
fix: Frame Processor not setting on first render
1 parent 53338fa commit b10b2c1

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/Camera.tsx

+26-20
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ export class Camera extends React.PureComponent<CameraProps> {
330330
}
331331
//#endregion
332332

333+
//#region Lifecycle
333334
/**
334335
* @internal
335336
*/
@@ -339,40 +340,45 @@ export class Camera extends React.PureComponent<CameraProps> {
339340
throw new Error('Frame Processors are not enabled. Make sure you install react-native-reanimated 2.2.0 or above!');
340341
}
341342

343+
private setFrameProcessor(frameProcessor: (frame: Frame) => void): void {
344+
this.assertFrameProcessorsEnabled();
345+
// @ts-expect-error JSI functions aren't typed
346+
global.setFrameProcessor(this.handle, frameProcessor);
347+
}
348+
349+
private unsetFrameProcessor(): void {
350+
this.assertFrameProcessorsEnabled();
351+
// @ts-expect-error JSI functions aren't typed
352+
global.setFrameProcessor(this.handle, this.props.frameProcessor);
353+
}
354+
342355
/**
343356
* @internal
344357
*/
345358
componentWillUnmount(): void {
346-
if (this.lastFrameProcessor != null || this.props.frameProcessor != null) {
347-
this.assertFrameProcessorsEnabled();
348-
// @ts-expect-error JSI functions aren't typed
349-
global.unsetFrameProcessor(this.handle);
350-
}
359+
if (this.lastFrameProcessor != null || this.props.frameProcessor != null) this.unsetFrameProcessor();
360+
}
361+
362+
/**
363+
* @internal
364+
*/
365+
componentDidMount(): void {
366+
if (this.props.frameProcessor != null) this.setFrameProcessor(this.props.frameProcessor);
351367
}
352368

353369
/**
354370
* @internal
355371
*/
356372
componentDidUpdate(): void {
357373
if (this.props.frameProcessor !== this.lastFrameProcessor) {
358-
this.assertFrameProcessorsEnabled();
359-
// frameProcessor argument changed. Update native to reflect the change.
360-
if (this.props.frameProcessor != null) {
361-
// 1. Spawn threaded JSI Runtime (if not already done)
362-
// 2. Add video data output to Camera stream (if not already done)
363-
// 3. Workletize the frameProcessor and prepare it for being called with frames
364-
// @ts-expect-error JSI functions aren't typed
365-
global.setFrameProcessor(this.handle, this.props.frameProcessor);
366-
} else {
367-
// 1. Destroy the threaded runtime
368-
// 2. remove the frame processor
369-
// 3. Remove the video data output
370-
// @ts-expect-error JSI functions aren't typed
371-
global.unsetFrameProcessor(this.handle);
372-
}
374+
// frameProcessor argument identity changed. Update native to reflect the change.
375+
if (this.props.frameProcessor != null) this.setFrameProcessor(this.props.frameProcessor);
376+
else this.unsetFrameProcessor();
377+
373378
this.lastFrameProcessor = this.props.frameProcessor;
374379
}
375380
}
381+
//#endregion
376382

377383
/**
378384
* @internal

0 commit comments

Comments
 (0)