Skip to content

Commit 2d18c89

Browse files
authored
fix: non-null check before calling nativeModule in OCR controllers (#755)
## Description <!-- Provide a concise and descriptive summary of the changes implemented in this PR. --> ### Introduces a breaking change? - [ ] Yes - [x] No ### Type of change - [x] Bug fix (change which fixes an issue) - [ ] New feature (change which adds functionality) - [ ] Documentation update (improves or adds clarity to existing documentation) - [ ] Other (chores, tests, code style improvements etc.) ### Tested on - [ ] iOS - [ ] Android ### Testing instructions <!-- Provide step-by-step instructions on how to test your changes. Include setup details if necessary. --> ### Screenshots <!-- Add screenshots here, if applicable --> ### Related issues <!-- Link related issues here using #issue-number --> ### Checklist - [ ] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have updated the documentation accordingly - [ ] My changes generate no new warnings ### Additional notes <!-- Include any additional information, assumptions, or context that reviewers might need to understand this PR. -->
1 parent c02aa92 commit 2d18c89

4 files changed

Lines changed: 24 additions & 4 deletions

File tree

packages/react-native-executorch/src/controllers/LLMController.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ export class LLMController {
215215
);
216216
}
217217
this.onToken = () => {};
218-
this.nativeModule.unload();
218+
if (this.nativeModule) {
219+
this.nativeModule.unload();
220+
}
219221
this.isReadyCallback(false);
220222
this.isGeneratingCallback(false);
221223
}
@@ -245,10 +247,22 @@ export class LLMController {
245247
}
246248

247249
public interrupt() {
250+
if (!this.nativeModule) {
251+
throw new RnExecutorchError(
252+
RnExecutorchErrorCode.ModuleNotLoaded,
253+
"Cannot interrupt a model that's not loaded."
254+
);
255+
}
248256
this.nativeModule.interrupt();
249257
}
250258

251259
public getGeneratedTokenCount(): number {
260+
if (!this.nativeModule) {
261+
throw new RnExecutorchError(
262+
RnExecutorchErrorCode.ModuleNotLoaded,
263+
"Cannot get token count for a model that's not loaded."
264+
);
265+
}
252266
return this.nativeModule.getGeneratedTokenCount();
253267
}
254268

packages/react-native-executorch/src/controllers/OCRController.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ export class OCRController {
103103
'The model is currently generating. Please wait until previous model run is complete.'
104104
);
105105
}
106-
this.nativeModule.unload();
106+
if (this.nativeModule) {
107+
this.nativeModule.unload();
108+
}
107109
this.isReadyCallback(false);
108110
this.isGeneratingCallback(false);
109111
}

packages/react-native-executorch/src/controllers/VerticalOCRController.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ export class VerticalOCRController {
106106
'The model is currently generating. Please wait until previous model run is complete.'
107107
);
108108
}
109-
this.ocrNativeModule.unload();
109+
if (this.ocrNativeModule) {
110+
this.ocrNativeModule.unload();
111+
}
110112
this.isReadyCallback(false);
111113
this.isGeneratingCallback(false);
112114
}

packages/react-native-executorch/src/hooks/natural_language_processing/useLLM.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ export const useLLM = ({
6868
})();
6969

7070
return () => {
71-
controllerInstance.delete();
71+
if (controllerInstance.isReady) {
72+
controllerInstance.delete();
73+
}
7274
};
7375
}, [
7476
controllerInstance,

0 commit comments

Comments
 (0)