Skip to content

Commit

Permalink
feat: Completely refactor the FrameProcessorPlugins.ts file into mu…
Browse files Browse the repository at this point in the history
…ltiple files (#2830)

* feat: Completely refactor the `FrameProcessorPlugins.ts` file into multiple files

* Refactor

* Prepare

* Update some docs

* fix: Fix invalid `options` param (undefined)

* Update FRAME_PROCESSORS_CREATE_OVERVIEW.mdx
  • Loading branch information
mrousavy committed May 1, 2024
1 parent e75bb9f commit 2e2d9b4
Show file tree
Hide file tree
Showing 18 changed files with 378 additions and 307 deletions.
2 changes: 1 addition & 1 deletion docs/docs/guides/FRAME_PROCESSORS_CREATE_OVERVIEW.mdx
Expand Up @@ -64,7 +64,7 @@ Returns a `string` in JS:
```js
export function detectObject(frame: Frame): string {
'worklet'
const result = FrameProcessorPlugins.detectObject(frame)
const result = detectObject(frame)
console.log(result) // <-- "cat"
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/guides/FRAME_PROCESSOR_CREATE_FINAL.mdx
Expand Up @@ -9,9 +9,9 @@ sidebar_label: Finish creating your Frame Processor Plugin
To make the Frame Processor Plugin available to the Frame Processor Worklet Runtime, create the following wrapper function in JS/TS:

```ts
import { VisionCameraProxy, Frame } from 'react-native-vision-camera'
import { initFrameProcessorPlugin, Frame } from 'react-native-vision-camera'

const plugin = VisionCameraProxy.initFrameProcessorPlugin('scanFaces')
const plugin = initFrameProcessorPlugin('scanFaces')

/**
* Scans faces.
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/guides/FRAME_PROCESSOR_CREATE_PLUGIN_ANDROID.mdx
Expand Up @@ -109,7 +109,7 @@ public class FaceDetectorFrameProcessorPluginPackage implements ReactPackage {
```

:::note
The Frame Processor Plugin will be exposed to JS through the `VisionCameraProxy` object. In this case, it would be `VisionCameraProxy.initFrameProcessorPlugin("detectFaces")`.
The Frame Processor Plugin can be initialized from JavaScript using `initFrameProcessorPlugin("detectFaces")`.
:::

6. Register the package in MainApplication.java
Expand Down Expand Up @@ -181,7 +181,7 @@ class FaceDetectorFrameProcessorPluginPackage : ReactPackage {
```

:::note
The Frame Processor Plugin will be exposed to JS through the `VisionCameraProxy` object. In this case, it would be `VisionCameraProxy.initFrameProcessorPlugin("detectFaces")`.
The Frame Processor Plugin can be initialized from JavaScript using `initFrameProcessorPlugin("detectFaces")`.
:::

6. Register the package in MainApplication.java
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/guides/FRAME_PROCESSOR_CREATE_PLUGIN_IOS.mdx
Expand Up @@ -72,7 +72,7 @@ VISION_EXPORT_FRAME_PROCESSOR(FaceDetectorFrameProcessorPlugin, detectFaces)
```
:::note
The Frame Processor Plugin will be exposed to JS through the `VisionCameraProxy` object. In this case, it would be `VisionCameraProxy.initFrameProcessorPlugin("detectFaces")`.
The Frame Processor Plugin can be initialized from JavaScript using `initFrameProcessorPlugin("detectFaces")`.
:::
4. **Implement your Frame Processing.** See the [Example Plugin (Objective-C)](https://github.com/mrousavy/react-native-vision-camera/blob/main/package/example/ios/FrameProcessors%20Plugins/Example%20Plugin/ExampleFrameProcessorPlugin.m) for reference.
Expand Down
@@ -1,7 +1,7 @@
import type { Frame } from 'react-native-vision-camera'
import { VisionCameraProxy } from 'react-native-vision-camera'
import { initFrameProcessorPlugin } from 'react-native-vision-camera'

const plugin = VisionCameraProxy.initFrameProcessorPlugin('example_kotlin_swift_plugin', { foo: 'bar' })
const plugin = initFrameProcessorPlugin('example_kotlin_swift_plugin', { foo: 'bar' })

export function exampleKotlinSwiftPlugin(frame: Frame): string[] {
'worklet'
Expand Down
4 changes: 2 additions & 2 deletions package/example/src/frame-processors/ExamplePlugin.ts
@@ -1,7 +1,7 @@
import type { Frame } from 'react-native-vision-camera'
import { VisionCameraProxy } from 'react-native-vision-camera'
import { initFrameProcessorPlugin } from 'react-native-vision-camera'

const plugin = VisionCameraProxy.initFrameProcessorPlugin('example_plugin')
const plugin = initFrameProcessorPlugin('example_plugin')

interface Result {
example_array: (string | number | boolean)[]
Expand Down
2 changes: 1 addition & 1 deletion package/src/Camera.tsx
Expand Up @@ -8,7 +8,7 @@ import { CameraModule } from './NativeCameraModule'
import type { PhotoFile, TakePhotoOptions } from './types/PhotoFile'
import type { Point } from './types/Point'
import type { RecordVideoOptions, VideoFile } from './types/VideoFile'
import { VisionCameraProxy } from './FrameProcessorPlugins'
import { VisionCameraProxy } from './frame-processors/VisionCameraProxy'
import { CameraDevices } from './CameraDevices'
import type { EmitterSubscription, NativeSyntheticEvent, NativeMethods } from 'react-native'
import type { TakeSnapshotOptions } from './types/Snapshot'
Expand Down
291 changes: 0 additions & 291 deletions package/src/FrameProcessorPlugins.ts

This file was deleted.

11 changes: 11 additions & 0 deletions package/src/frame-processors/FrameProcessorsUnavailableError.ts
@@ -0,0 +1,11 @@
import { CameraRuntimeError } from '../CameraError'

export class FrameProcessorsUnavailableError extends CameraRuntimeError {
constructor(reason: unknown) {
super(
'system/frame-processors-unavailable',
'Frame Processors are not available, react-native-worklets-core is not installed! ' +
`Error: ${reason instanceof Error ? reason.message : reason}`,
)
}
}

0 comments on commit 2e2d9b4

Please sign in to comment.