Skip to content

Commit 74be0c6

Browse files
fix: requested changes
1 parent fd5aca7 commit 74be0c6

File tree

5 files changed

+28
-14
lines changed

5 files changed

+28
-14
lines changed

apps/computer-vision/app/object_detection_live/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export default function ObjectDetectionLiveScreen() {
4242
useEffect(() => {
4343
setGlobalGenerating(model.isGenerating);
4444
}, [model.isGenerating, setGlobalGenerating]);
45-
4645
const [detectionCount, setDetectionCount] = useState(0);
4746
const [fps, setFps] = useState(0);
4847
const lastFrameTimeRef = useRef(Date.now());

docs/docs/05-utilities/04-error-handling.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ These errors occur when trying to perform operations on a model in an invalid st
8383

8484
These errors occur when invalid configuration or input is provided.
8585

86-
| Error Code | Description | When It Occurs | How to Handle |
87-
| ---------------------- | ------------------------------------ | ------------------------------------------------------------------------- | ---------------------------------------------------- |
88-
| `InvalidConfig` | Configuration parameters are invalid | Setting parameters outside valid ranges (e.g., `topp` outside [0, 1]) | Check parameter constraints and provide valid values |
89-
| `InvalidUserInput` | Input provided to API is invalid | Passing empty arrays, null values, or malformed data to methods | Validate input before calling methods |
90-
| `InvalidModelSource` | Model source type is invalid | Providing wrong type for model source (e.g., object when string expected) | Ensure model source matches expected type |
91-
| `LanguageNotSupported` | Language not supported by model | Passing unsupported language to multilingual OCR or Speech-to-Text models | Use a supported language or different model |
92-
| `WrongDimensions` | Input tensor dimensions don't match | Providing input with incorrect shape for the model | Check model's expected input dimensions |
93-
| `UnexpectedNumInputs` | Wrong number of inputs provided | Passing more or fewer inputs than model expects | Match the number of inputs to model metadata |
86+
| Error Code | Description | When It Occurs | How to Handle |
87+
| ---------------------- | ------------------------------------ | --------------------------------------------------------------------------------------- | -------------------------------------------------------- |
88+
| `InvalidConfig` | Configuration parameters are invalid | Setting parameters outside valid ranges (e.g., `topp` outside [0, 1]) | Check parameter constraints and provide valid values |
89+
| `InvalidUserInput` | Input provided to API is invalid | Passing empty arrays, null values, or malformed data to methods | Validate input before calling methods |
90+
| `InvalidModelSource` | Model source type is invalid | Providing wrong type for model source (e.g., object when string expected) | Ensure model source matches expected type |
91+
| `LanguageNotSupported` | Language not supported by model | Passing unsupported language to multilingual OCR or Speech-to-Text models | Use a supported language or different model |
92+
| `PlatformNotSupported` | Current platform is not supported | Using features (e.g., camera frame processing) on an unsupported platform or OS version | Ensure you're running on a supported platform/OS version |
93+
| `WrongDimensions` | Input tensor dimensions don't match | Providing input with incorrect shape for the model | Check model's expected input dimensions |
94+
| `UnexpectedNumInputs` | Wrong number of inputs provided | Passing more or fewer inputs than model expects | Match the number of inputs to model metadata |
9495

9596
### File Operations Errors
9697

packages/react-native-executorch/src/hooks/computer_vision/useObjectDetection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ export const useObjectDetection = ({
2020
module: ObjectDetectionModule,
2121
model,
2222
preventLoad: preventLoad,
23-
}) as ObjectDetectionType;
23+
});
2424
};

packages/react-native-executorch/src/hooks/useModule.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { useEffect, useState } from 'react';
22
import { RnExecutorchErrorCode } from '../errors/ErrorCodes';
33
import { RnExecutorchError, parseUnknownError } from '../errors/errorUtils';
44

5+
type RunOnFrame<M> = M extends { runOnFrame: infer R } ? R : never;
6+
57
interface Module {
68
load: (...args: any[]) => Promise<void>;
79
forward: (...args: any[]) => Promise<any>;
@@ -31,7 +33,7 @@ export const useModule = <
3133
const [isGenerating, setIsGenerating] = useState(false);
3234
const [downloadProgress, setDownloadProgress] = useState(0);
3335
const [moduleInstance] = useState(() => new module());
34-
const [runOnFrame, setRunOnFrame] = useState<any>(null);
36+
const [runOnFrame, setRunOnFrame] = useState<RunOnFrame<M> | null>(null);
3537

3638
useEffect(() => {
3739
if (preventLoad) return;
@@ -48,9 +50,21 @@ export const useModule = <
4850
});
4951
if (isMounted) setIsReady(true);
5052

51-
// Use "state trick" to make the worklet serializable for VisionCamera
53+
// VisionCamera worklets run on a separate JS thread and can only capture
54+
// serializable values (plain functions, primitives). The module instance
55+
// is a class object and is not serializable, so accessing runOnFrame
56+
// directly inside a worklet would fail at runtime.
57+
//
58+
// By extracting the method and storing it in React state, it becomes a
59+
// standalone function reference that the worklet thread can capture and
60+
// call safely.
61+
//
62+
// Note: setState(fn) triggers React's updater form — it calls fn(prevState)
63+
// and stores the return value, not fn itself. Since runOnFrame is a function,
64+
// we wrap it: setState(() => worklet) so React stores the worklet as the
65+
// state value rather than invoking it.
5266
if ('runOnFrame' in moduleInstance) {
53-
const worklet = moduleInstance.runOnFrame;
67+
const worklet = moduleInstance.runOnFrame as RunOnFrame<M>;
5468
if (worklet) {
5569
setRunOnFrame(() => worklet);
5670
}

packages/react-native-executorch/src/types/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export type Triple<T> = readonly [T, T, T];
169169
* };
170170
* ```
171171
*/
172-
export interface PixelData extends Omit<TensorPtr, 'dataPtr' | 'scalarType'> {
172+
export interface PixelData extends Pick<TensorPtr, 'sizes'> {
173173
/**
174174
* RGB pixel data as Uint8Array.
175175
* Expected format: RGB (3 channels), not RGBA or BGRA.

0 commit comments

Comments
 (0)