Skip to content

Commit

Permalink
fix: Optionally require Reanimated
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Sep 7, 2023
1 parent 021417a commit 401ea32
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Camera.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import type { PhotoFile, TakePhotoOptions } from './PhotoFile';
import type { Point } from './Point';
import type { TakeSnapshotOptions } from './Snapshot';
import type { CameraVideoCodec, RecordVideoOptions, VideoFile } from './VideoFile';
import { createWorkletRuntime } from 'react-native-reanimated';
import { makeShareableCloneRecursive } from 'react-native-reanimated/src/reanimated2/shareables';
import { FrameProcessorContext } from './FrameProcessorContext';

//#region Types
export type CameraPermissionStatus = 'authorized' | 'not-determined' | 'denied' | 'restricted';
Expand Down Expand Up @@ -438,7 +437,7 @@ export class Camera extends React.PureComponent<CameraProps> {
private setFrameProcessor(frameProcessor: (frame: Frame) => void): void {
this.assertFrameProcessorsEnabled();
// @ts-expect-error JSI functions aren't typed
global.setFrameProcessor(this.handle, makeShareableCloneRecursive(frameProcessor), createWorkletRuntime('VisionCamera'));
global.setFrameProcessor(this.handle, FrameProcessorContext.createWorklet(frameProcessor), FrameProcessorContext.workletRuntime);
}

private unsetFrameProcessor(): void {
Expand Down
20 changes: 20 additions & 0 deletions src/FrameProcessorContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let workletRuntime = null

Check warning on line 1 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Insert `;`
let createWorklet = () => {

Check warning on line 2 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Missing return type on function.
throw new Error("Reanimated V3 is not installed, Frame Processors are not available!")

Check warning on line 3 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Replace `"Reanimated路V3路is路not路installed,路Frame路Processors路are路not路available!")` with `'Reanimated路V3路is路not路installed,路Frame路Processors路are路not路available!');`

Check warning on line 3 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Strings must use singlequote.
}

Check warning on line 4 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Insert `;`

try {
const reanimated = require('react-native-reanimated')

Check failure on line 7 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Unsafe assignment of an `any` value.

Check failure on line 7 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Require statement not part of import statement.

Check warning on line 7 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Insert `;`
if (reanimated.createWorkletRuntime == null) {

Check warning on line 8 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Unnecessary { after 'if' condition.
console.warn("Frame Processors are disabled because you're using an incompatible version of Reanimated.")

Check warning on line 9 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Insert `;`
}
workletRuntime = reanimated.createWorkletRuntime('VisionCamera')

Check failure on line 11 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Unsafe assignment of an `any` value.

Check warning on line 11 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Insert `;`
createWorklet = reanimated.makeShareableCloneRecursive

Check failure on line 12 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Unsafe assignment of an `any` value.

Check warning on line 12 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Insert `;`
} catch {
// Frame Processors are not enabled
}

export const FrameProcessorContext = {
workletRuntime: workletRuntime,

Check failure on line 18 in src/FrameProcessorContext.ts

View workflow job for this annotation

GitHub Actions / Lint JS (eslint, prettier)

Unsafe assignment of an `any` value.
createWorklet: createWorklet
}

0 comments on commit 401ea32

Please sign in to comment.