Skip to content

Commit

Permalink
simplifying returns and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
luicfrr committed Apr 19, 2024
1 parent 92bc1eb commit 1f8b7da
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
45 changes: 25 additions & 20 deletions example/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import Animated, {
useSharedValue,
withTiming
} from 'react-native-reanimated'
import { Worklets } from 'react-native-worklets-core'

/**
* Entry point component
Expand Down Expand Up @@ -113,11 +112,33 @@ function FaceDetection(): JSX.Element {
} )
} ) )

const handleFacesDetected = Worklets.createRunInJsFn( ( {
useEffect( () => {
if ( hasPermission ) return
requestPermission()
}, [] )

/**
* Hanldes camera mount error event
*
* @param {any} error Error event
*/
function handleCameraMountError(
error: any
) {
console.error( 'camera mount error', error )
}

/**
* Handle detection result
*
* @param {DetectionResult} result Detection result
* @returns {void}
*/
function handleFacesDetected( {
faces,
frame
}: DetectionResult ) => {
console.log( 'faces', faces, 'frame', frame )
}: DetectionResult ): void {
console.log( 'faces', faces.length, 'frame', frame.toString() )
// if no faces are detected we do nothing
if ( Object.keys( faces ).length <= 0 ) return

Expand All @@ -137,22 +158,6 @@ function FaceDetection(): JSX.Element {
if ( camera.current ) {
// take photo, capture video, etc...
}
} )

useEffect( () => {
if ( hasPermission ) return
requestPermission()
}, [] )

/**
* Hanldes camera mount error event
*
* @param {any} error Error event
*/
function handleCameraMountError(
error: any
) {
console.error( 'camera mount error', error )
}

return ( <>
Expand Down
17 changes: 13 additions & 4 deletions src/Camera.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ import type {
FrameInternal
} from 'react-native-vision-camera'
import type {
CallbackType,
DetectionResult,
FaceDetectionOptions
} from './FaceDetector'

type WorkletType = (
frame: FrameInternal
) => Promise<void>

type CallbackType = (
result: DetectionResult
) => void | Promise<void>

type ComponentType = {
faceDetectionOptions?: FaceDetectionOptions
faceDetectionCallback: CallbackType
Expand Down Expand Up @@ -81,6 +85,10 @@ export const Camera = React.forwardRef( ( {
console.log( log )
}
} )
/**
* Runs on detection callback on js thread
*/
const runOnJs = Worklets.createRunInJsFn( faceDetectionCallback )
/**
* Async context that will handle face detection
*/
Expand All @@ -89,9 +97,10 @@ export const Camera = React.forwardRef( ( {
) => {
'worklet'
try {
detectFaces( {
frame,
callback: faceDetectionCallback
const result = detectFaces( frame )
frame.incrementRefCount()
runOnJs( result ).finally( () => {
frame.decrementRefCount()
} )
} catch ( error: any ) {
logOnJs( 'Execution error:', error )
Expand Down
26 changes: 6 additions & 20 deletions src/FaceDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,16 @@ type FaceDetectorPlugin = {
/**
* Detect faces on frame
*
* @param {DetectFacesType} props Detect faces prop
* @param {Frame} frame Frame to detect faces
*/
detectFaces: ( props: DetectFacesType ) => DetectionResult
}

type DetectFacesType = {
/** Current frame */
frame: Frame
/** Optional callback */
callback?: CallbackType
detectFaces: ( frame: Frame ) => DetectionResult
}

type Point = {
x: number
y: number
}

export type CallbackType = (
result: DetectionResult
) => void | Promise<void>

export interface DetectionResult {
faces: Face[]
frame: Frame
Expand Down Expand Up @@ -157,15 +146,12 @@ function createFaceDetectorPlugin(
}

return {
detectFaces: ( {
frame,
callback
}: DetectFacesType ): DetectionResult => {
detectFaces: (
frame: Frame
): DetectionResult => {
'worklet'
// @ts-ignore
const result: DetectionResult = plugin.call( frame )
callback?.( result )
return result
return plugin.call( frame ) as DetectionResult
}
}
}
Expand Down

0 comments on commit 1f8b7da

Please sign in to comment.