Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid JS function binding when calling native module #15

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ expo-speech-recognition implements the iOS [`SFSpeechRecognizer`](https://develo
- [getSpeechRecognitionServices()](#getspeechrecognitionservices-string-android-only)
- [getDefaultRecognitionService()](#getdefaultrecognitionservice--packagename-string--android-only)
- [getAssistantService()](#getassistantservice--packagename-string--android-only)
- [supportsOnDeviceRecognition()](#supportsondevicerecognition-boolean-android-only)
- [supportsOnDeviceRecognition()](#supportsondevicerecognition-boolean)
- [supportsRecording()](#supportsrecording-boolean-android-only)
- [androidTriggerOfflineModelDownload()](#androidtriggerofflinemodeldownload-locale-string--promise-status-opened_dialog--download_success--download_canceled-message-string-)
- [setCategoryIOS()](#setcategoryios-void-ios-only)
Expand Down
40 changes: 18 additions & 22 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import {
type ExpoSpeechRecognitionOptions,
type AndroidIntentOptions,
useSpeechRecognitionEvent,
AudioEncodingAndroidValue,
type AudioEncodingAndroidValue,
TaskHintIOS,
AVAudioSessionCategory,
type AVAudioSessionCategoryValue,
AVAudioSessionCategoryOptions,
type AVAudioSessionCategoryOptionsValue,
SetCategoryOptions,
type SetCategoryOptions,
AVAudioSessionMode,
type AVAudioSessionModeValue,
ExpoWebSpeechRecognition,
Expand Down Expand Up @@ -131,10 +131,6 @@ export default function App() {
});
};

const stopListening = () => {
ExpoSpeechRecognitionModule.stop();
};

return (
<SafeAreaView style={styles.container}>
<StatusBar style="dark" translucent={false} />
Expand Down Expand Up @@ -191,12 +187,12 @@ export default function App() {
<BigButton
title="Stop"
disabled={status !== "recognizing"}
onPress={stopListening}
onPress={ExpoSpeechRecognitionModule.stop}
/>
<BigButton
title="Abort"
disabled={status !== "recognizing"}
onPress={() => ExpoSpeechRecognitionModule.abort()}
onPress={ExpoSpeechRecognitionModule.abort}
/>
</View>
)}
Expand Down Expand Up @@ -956,7 +952,7 @@ function WebSpeechAPIDemo() {
transcript: string;
}>(null);

const reconizer = useMemo(() => new ExpoWebSpeechRecognition(), []);
const recognizer = useMemo(() => new ExpoWebSpeechRecognition(), []);

useEffect(() => {
if (!listening) {
Expand All @@ -982,16 +978,16 @@ function WebSpeechAPIDemo() {
setListening(false);
};

reconizer.addEventListener("result", handleResult);
reconizer.addEventListener("error", handleError);
reconizer.addEventListener("end", handleEnd);
recognizer.addEventListener("result", handleResult);
recognizer.addEventListener("error", handleError);
recognizer.addEventListener("end", handleEnd);

return () => {
reconizer.removeEventListener("result", handleResult);
reconizer.removeEventListener("error", handleError);
reconizer.removeEventListener("end", handleEnd);
recognizer.removeEventListener("result", handleResult);
recognizer.removeEventListener("error", handleError);
recognizer.removeEventListener("end", handleEnd);
};
}, [listening]);
}, [listening, recognizer]);

const startListeningWeb = () => {
setListening(true);
Expand All @@ -1003,10 +999,10 @@ function WebSpeechAPIDemo() {
console.log("Permissions not granted", result);
return;
}
reconizer.lang = "en-US";
reconizer.continuous = true;
reconizer.interimResults = true;
reconizer.start();
recognizer.lang = "en-US";
recognizer.continuous = true;
recognizer.interimResults = true;
recognizer.start();
});
};

Expand All @@ -1023,12 +1019,12 @@ function WebSpeechAPIDemo() {
<BigButton
color="#B1B695"
title="Stop Recognition"
onPress={() => reconizer.stop()}
onPress={() => recognizer.stop()}
/>
<BigButton
color="#B1B695"
title="Abort Recognition"
onPress={() => reconizer.abort()}
onPress={() => recognizer.abort()}
/>
</View>
)}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "expo-speech-recognition",
"version": "0.2.16",
"version": "0.2.17",
"description": "Speech Recognition for React Native Expo projects",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
26 changes: 24 additions & 2 deletions src/ExpoSpeechRecognitionModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,31 @@ import type { ExpoSpeechRecognitionModuleType } from "./ExpoSpeechRecognitionMod

// It loads the native module object from the JSI or falls back to
// the bridge module (from NativeModulesProxy) if the remote debugger is on.
export const ExpoSpeechRecognitionModule =
const ExpoSpeechRecognitionNativeModule =
requireNativeModule<ExpoSpeechRecognitionModuleType>("ExpoSpeechRecognition");

export const ExpoSpeechRecognitionModule = {
...ExpoSpeechRecognitionNativeModule,
// Avoid any function bindings when calling the native module
stop: () => ExpoSpeechRecognitionNativeModule.stop(),
abort: () => ExpoSpeechRecognitionNativeModule.abort(),
requestPermissionsAsync: () =>
ExpoSpeechRecognitionNativeModule.requestPermissionsAsync(),
getPermissionsAsync: () =>
ExpoSpeechRecognitionNativeModule.getPermissionsAsync(),
getStateAsync: () => ExpoSpeechRecognitionNativeModule.getStateAsync(),
getAssistantService: () =>
ExpoSpeechRecognitionNativeModule.getAssistantService(),
getDefaultRecognitionService: () =>
ExpoSpeechRecognitionNativeModule.getDefaultRecognitionService(),
getSpeechRecognitionServices: () =>
ExpoSpeechRecognitionNativeModule.getSpeechRecognitionServices(),
supportsOnDeviceRecognition: () =>
ExpoSpeechRecognitionNativeModule.supportsOnDeviceRecognition(),
supportsRecording: () =>
ExpoSpeechRecognitionNativeModule.supportsRecording(),
};

export const ExpoSpeechRecognitionModuleEmitter = new EventEmitter(
ExpoSpeechRecognitionModule ?? NativeModulesProxy.ExpoSpeechRecognition,
ExpoSpeechRecognitionNativeModule ?? NativeModulesProxy.ExpoSpeechRecognition,
);