Skip to content

Commit

Permalink
fix: Throw if VisionCameraProxy is lazy used
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed May 2, 2024
1 parent af2707e commit 5243678
Showing 1 changed file with 9 additions and 28 deletions.
37 changes: 9 additions & 28 deletions package/src/frame-processors/VisionCameraProxy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IWorkletContext } from 'react-native-worklets-core'
import { createModuleProxy } from '../dependencies/ModuleProxy'
import { CameraModule } from '../NativeCameraModule'
import type { Frame } from '../types/Frame'
import { FrameProcessorsUnavailableError } from './FrameProcessorsUnavailableError'
import type { FrameProcessorPlugin, ParameterType } from './initFrameProcessorPlugin'

interface TVisionCameraProxy {
Expand Down Expand Up @@ -30,9 +30,12 @@ interface TVisionCameraProxy {
workletContext: IWorkletContext | undefined
}

let proxy: TVisionCameraProxy

try {
/**
* The JSI Proxy for the Frame Processors Runtime.
*
* This will be replaced with a CxxTurboModule in the future.
*/
export const VisionCameraProxy = createModuleProxy<TVisionCameraProxy>(() => {
// 1. Load react-native-worklets-core
require('react-native-worklets-core')
// 2. If react-native-worklets-core could be loaded, try to install Frame Processor bindings
Expand All @@ -44,27 +47,5 @@ try {
const globalProxy = global.VisionCameraProxy as TVisionCameraProxy | undefined
if (globalProxy == null) throw new Error('global.VisionCameraProxy is not installed! Was installFrameProcessorBindings() called?')

proxy = globalProxy
} catch (e) {
// global.VisionCameraProxy is not injected!
// Just use dummy implementations that will throw when the user tries to use Frame Processors.
proxy = {
initFrameProcessorPlugin: () => {
throw new FrameProcessorsUnavailableError(e)
},
removeFrameProcessor: () => {
throw new FrameProcessorsUnavailableError(e)
},
setFrameProcessor: () => {
throw new FrameProcessorsUnavailableError(e)
},
workletContext: undefined,
}
}

/**
* The JSI Proxy for the Frame Processors Runtime.
*
* This will be replaced with a CxxTurboModule in the future.
*/
export const VisionCameraProxy = proxy
return globalProxy
})

0 comments on commit 5243678

Please sign in to comment.