expo-speech-recognition 0.2.25 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +23 -34
  3. package/build/ExpoSpeechRecognitionModule.d.ts +0 -2
  4. package/build/ExpoSpeechRecognitionModule.d.ts.map +1 -1
  5. package/build/ExpoSpeechRecognitionModule.js +6 -18
  6. package/build/ExpoSpeechRecognitionModule.js.map +1 -1
  7. package/build/ExpoSpeechRecognitionModule.types.d.ts +5 -2
  8. package/build/ExpoSpeechRecognitionModule.types.d.ts.map +1 -1
  9. package/build/ExpoSpeechRecognitionModule.types.js.map +1 -1
  10. package/build/ExpoSpeechRecognitionModule.web.d.ts +43 -10
  11. package/build/ExpoSpeechRecognitionModule.web.d.ts.map +1 -1
  12. package/build/ExpoSpeechRecognitionModule.web.js +109 -105
  13. package/build/ExpoSpeechRecognitionModule.web.js.map +1 -1
  14. package/build/ExpoWebSpeechRecognition.d.ts.map +1 -1
  15. package/build/ExpoWebSpeechRecognition.js +4 -4
  16. package/build/ExpoWebSpeechRecognition.js.map +1 -1
  17. package/build/index.d.ts +3 -4
  18. package/build/index.d.ts.map +1 -1
  19. package/build/index.js +3 -3
  20. package/build/index.js.map +1 -1
  21. package/build/useSpeechRecognitionEvent.d.ts +2 -2
  22. package/build/useSpeechRecognitionEvent.d.ts.map +1 -1
  23. package/build/useSpeechRecognitionEvent.js +5 -9
  24. package/build/useSpeechRecognitionEvent.js.map +1 -1
  25. package/package.json +4 -4
  26. package/src/ExpoSpeechRecognitionModule.ts +6 -32
  27. package/src/ExpoSpeechRecognitionModule.types.ts +8 -2
  28. package/src/ExpoSpeechRecognitionModule.web.ts +172 -145
  29. package/src/ExpoWebSpeechRecognition.ts +5 -8
  30. package/src/index.ts +22 -33
  31. package/src/useSpeechRecognitionEvent.ts +11 -19
@@ -1,5 +1,5 @@
1
- import { useEffect, useRef } from "react";
2
- import { ExpoSpeechRecognitionModuleEmitter } from "./ExpoSpeechRecognitionModule";
1
+ import { ExpoSpeechRecognitionModule } from "./ExpoSpeechRecognitionModule";
2
+ import { useEventListener } from "expo";
3
3
  /**
4
4
  * This hook allows you to listen to native events emitted by the `ExpoSpeechRecognitionModule`.
5
5
  *
@@ -9,12 +9,8 @@ import { ExpoSpeechRecognitionModuleEmitter } from "./ExpoSpeechRecognitionModul
9
9
  * @param listener The listener function to call when the event is emitted
10
10
  */
11
11
  export function useSpeechRecognitionEvent(eventName, listener) {
12
- const listenerRef = useRef(listener);
13
- listenerRef.current = listener;
14
- useEffect(() => {
15
- const handler = listenerRef.current;
16
- const subscription = ExpoSpeechRecognitionModuleEmitter.addListener(eventName, handler);
17
- return subscription.remove;
18
- }, [eventName]);
12
+ return useEventListener(ExpoSpeechRecognitionModule, eventName,
13
+ // @ts-expect-error
14
+ listener);
19
15
  }
20
16
  //# sourceMappingURL=useSpeechRecognitionEvent.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useSpeechRecognitionEvent.js","sourceRoot":"","sources":["../src/useSpeechRecognitionEvent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAC1C,OAAO,EAAE,kCAAkC,EAAE,MAAM,+BAA+B,CAAC;AAGnF;;;;;;;GAOG;AACH,MAAM,UAAU,yBAAyB,CAGvC,SAAY,EACZ,QAA8D;IAE9D,MAAM,WAAW,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;IACrC,WAAW,CAAC,OAAO,GAAG,QAAQ,CAAC;IAE/B,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC;QACpC,MAAM,YAAY,GAAG,kCAAkC,CAAC,WAAW,CACjE,SAAS,EACT,OAAO,CACR,CAAC;QACF,OAAO,YAAY,CAAC,MAAM,CAAC;IAC7B,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;AAClB,CAAC","sourcesContent":["import { useEffect, useRef } from \"react\";\nimport { ExpoSpeechRecognitionModuleEmitter } from \"./ExpoSpeechRecognitionModule\";\nimport type { ExpoSpeechRecognitionNativeEventMap } from \"./ExpoSpeechRecognitionModule.types\";\n\n/**\n * This hook allows you to listen to native events emitted by the `ExpoSpeechRecognitionModule`.\n *\n * Note: this is not the same as the `SpeechRecognition` event listener on the web speech API.\n *\n * @param eventName The name of the event to listen to\n * @param listener The listener function to call when the event is emitted\n */\nexport function useSpeechRecognitionEvent<\n K extends keyof ExpoSpeechRecognitionNativeEventMap,\n>(\n eventName: K,\n listener: (ev: ExpoSpeechRecognitionNativeEventMap[K]) => void,\n) {\n const listenerRef = useRef(listener);\n listenerRef.current = listener;\n\n useEffect(() => {\n const handler = listenerRef.current;\n const subscription = ExpoSpeechRecognitionModuleEmitter.addListener(\n eventName,\n handler,\n );\n return subscription.remove;\n }, [eventName]);\n}\n"]}
1
+ {"version":3,"file":"useSpeechRecognitionEvent.js","sourceRoot":"","sources":["../src/useSpeechRecognitionEvent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,+BAA+B,CAAC;AAE5E,OAAO,EAAE,gBAAgB,EAAE,MAAM,MAAM,CAAC;AAExC;;;;;;;GAOG;AACH,MAAM,UAAU,yBAAyB,CAEvC,SAAY,EAAE,QAA8C;IAC5D,OAAO,gBAAgB,CACrB,2BAA2B,EAC3B,SAAS;IACT,mBAAmB;IACnB,QAAQ,CACT,CAAC;AACJ,CAAC","sourcesContent":["import { ExpoSpeechRecognitionModule } from \"./ExpoSpeechRecognitionModule\";\nimport type { ExpoSpeechRecognitionNativeEvents } from \"./ExpoSpeechRecognitionModule.types\";\nimport { useEventListener } from \"expo\";\n\n/**\n * This hook allows you to listen to native events emitted by the `ExpoSpeechRecognitionModule`.\n *\n * Note: this is not the same as the `SpeechRecognition` event listener on the web speech API.\n *\n * @param eventName The name of the event to listen to\n * @param listener The listener function to call when the event is emitted\n */\nexport function useSpeechRecognitionEvent<\n K extends keyof ExpoSpeechRecognitionNativeEvents,\n>(eventName: K, listener: ExpoSpeechRecognitionNativeEvents[K]) {\n return useEventListener(\n ExpoSpeechRecognitionModule,\n eventName,\n // @ts-expect-error\n listener,\n );\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-speech-recognition",
3
- "version": "0.2.25",
3
+ "version": "1.0.0",
4
4
  "description": "Speech Recognition for React Native Expo projects",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -38,9 +38,9 @@
38
38
  "devDependencies": {
39
39
  "@changesets/cli": "^2.27.9",
40
40
  "@types/dom-speech-recognition": "^0.0.4",
41
- "@types/react": "^18.3.3",
42
- "expo-module-scripts": "^3.5.2",
43
- "expo-modules-core": "^1.12.21"
41
+ "@types/react": "^18.3.12",
42
+ "expo": "~52.0.0",
43
+ "expo-module-scripts": "^4.0.2"
44
44
  },
45
45
  "prettier": {
46
46
  "trailingComma": "all"
@@ -1,40 +1,14 @@
1
- import {
2
- EventEmitter,
3
- NativeModulesProxy,
4
- requireNativeModule,
5
- } from "expo-modules-core";
1
+ import { requireNativeModule } from "expo";
6
2
 
7
3
  import type { ExpoSpeechRecognitionModuleType } from "./ExpoSpeechRecognitionModule.types";
8
4
 
9
5
  // It loads the native module object from the JSI or falls back to
10
6
  // the bridge module (from NativeModulesProxy) if the remote debugger is on.
11
- const ExpoSpeechRecognitionNativeModule =
7
+ export const ExpoSpeechRecognitionModule =
12
8
  requireNativeModule<ExpoSpeechRecognitionModuleType>("ExpoSpeechRecognition");
13
9
 
14
- export const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {
15
- ...ExpoSpeechRecognitionNativeModule,
16
- // Avoid any function bindings when calling the native module
17
- stop: () => ExpoSpeechRecognitionNativeModule.stop(),
18
- abort: () => ExpoSpeechRecognitionNativeModule.abort(),
19
- requestPermissionsAsync: () =>
20
- ExpoSpeechRecognitionNativeModule.requestPermissionsAsync(),
21
- getPermissionsAsync: () =>
22
- ExpoSpeechRecognitionNativeModule.getPermissionsAsync(),
23
- getStateAsync: () => ExpoSpeechRecognitionNativeModule.getStateAsync(),
24
- getAssistantService: () =>
25
- ExpoSpeechRecognitionNativeModule.getAssistantService(),
26
- getDefaultRecognitionService: () =>
27
- ExpoSpeechRecognitionNativeModule.getDefaultRecognitionService(),
28
- getSpeechRecognitionServices: () =>
29
- ExpoSpeechRecognitionNativeModule.getSpeechRecognitionServices(),
30
- supportsOnDeviceRecognition: () =>
31
- ExpoSpeechRecognitionNativeModule.supportsOnDeviceRecognition(),
32
- supportsRecording: () =>
33
- ExpoSpeechRecognitionNativeModule.supportsRecording(),
34
- isRecognitionAvailable: () =>
35
- ExpoSpeechRecognitionNativeModule.isRecognitionAvailable(),
36
- };
10
+ const stop = ExpoSpeechRecognitionModule.stop;
11
+ const abort = ExpoSpeechRecognitionModule.abort;
37
12
 
38
- export const ExpoSpeechRecognitionModuleEmitter = new EventEmitter(
39
- ExpoSpeechRecognitionNativeModule ?? NativeModulesProxy.ExpoSpeechRecognition,
40
- );
13
+ ExpoSpeechRecognitionModule.abort = () => abort();
14
+ ExpoSpeechRecognitionModule.stop = () => stop();
@@ -1,5 +1,5 @@
1
1
  import type { PermissionResponse } from "expo-modules-core";
2
- import type { NativeModule } from "react-native";
2
+ import type { NativeModule } from "expo";
3
3
 
4
4
  import type {
5
5
  AudioEncodingAndroid,
@@ -541,7 +541,13 @@ export type AndroidIntentOptions = {
541
541
  EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS: number;
542
542
  };
543
543
 
544
- export interface ExpoSpeechRecognitionModuleType extends NativeModule {
544
+ export type ExpoSpeechRecognitionNativeEvents = {
545
+ [K in keyof ExpoSpeechRecognitionNativeEventMap]: (
546
+ event: ExpoSpeechRecognitionNativeEventMap[K],
547
+ ) => void;
548
+ };
549
+
550
+ export declare class ExpoSpeechRecognitionModuleType extends NativeModule<ExpoSpeechRecognitionNativeEvents> {
545
551
  /**
546
552
  * Starts speech recognition.
547
553
  */
@@ -1,15 +1,118 @@
1
- import type { PermissionResponse } from "expo-modules-core";
1
+ import type { EventSubscription, PermissionResponse } from "expo-modules-core";
2
+ import { registerWebModule, NativeModule } from "expo";
2
3
  import type {
3
- ExpoSpeechRecognitionModuleType,
4
4
  ExpoSpeechRecognitionNativeEventMap,
5
+ ExpoSpeechRecognitionNativeEvents,
6
+ ExpoSpeechRecognitionOptions,
5
7
  } from "./ExpoSpeechRecognitionModule.types";
6
8
 
7
9
  let _speechRecognitionRef: SpeechRecognition | null = null;
8
10
 
11
+ type NativeEventListener = (
12
+ event: SpeechRecognitionEventMap[keyof SpeechRecognitionEventMap],
13
+ ) => void;
14
+
9
15
  // It loads the native module object from the JSI or falls back to
10
16
  // the bridge module (from NativeModulesProxy) if the remote debugger is on.
11
- export const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {
12
- start: (options) => {
17
+ class ExpoSpeechRecognitionModuleWeb extends NativeModule<ExpoSpeechRecognitionNativeEvents> {
18
+ _clientListeners: Map<
19
+ // Original listener
20
+ ExpoSpeechRecognitionNativeEvents[keyof ExpoSpeechRecognitionNativeEvents],
21
+ // Native listener
22
+ NativeEventListener
23
+ > = new Map();
24
+
25
+ _nativeListeners: Map<string, Set<NativeEventListener>> = new Map();
26
+
27
+ // Convert the web event to the native event
28
+ bindEventListener = <T extends keyof SpeechRecognitionEventMap>(
29
+ eventName: T,
30
+ ev: SpeechRecognitionEventMap[T],
31
+ ) => {
32
+ const eventPayload = webToNativeEventMap[eventName]?.(ev);
33
+
34
+ this.emit(
35
+ eventName,
36
+ // @ts-expect-error payload typings are incorrect
37
+ eventPayload,
38
+ );
39
+ };
40
+
41
+ addListener<EventName extends keyof ExpoSpeechRecognitionNativeEventMap>(
42
+ eventName: EventName,
43
+ listener: ExpoSpeechRecognitionNativeEvents[EventName],
44
+ ): EventSubscription {
45
+ // Convert the web event to the native event
46
+
47
+ // @ts-expect-error Not all events are covered here
48
+ const nativeListener = (ev: SpeechRecognitionEventMap[EventName]) => {
49
+ const handler =
50
+ eventName in webToNativeEventMap
51
+ ? webToNativeEventMap[eventName as keyof SpeechRecognitionEventMap]
52
+ : null;
53
+
54
+ // @ts-expect-error payload typings are incorrect
55
+ const eventPayload = handler?.(ev);
56
+
57
+ // @ts-expect-error
58
+ listener(eventPayload);
59
+ };
60
+
61
+ // @ts-expect-error
62
+ _speechRecognitionRef?.addEventListener(eventName, nativeListener);
63
+ if (!this._nativeListeners.has(eventName)) {
64
+ this._nativeListeners.set(eventName, new Set());
65
+ }
66
+ // Add the original listener to the enhanced listeners
67
+ // @ts-expect-error
68
+ this._nativeListeners.get(eventName)?.add(nativeListener);
69
+ // Map the original listener to the enhanced listener
70
+ // @ts-expect-error
71
+ this._clientListeners.set(listener, nativeListener);
72
+
73
+ const handle = super.addListener(eventName, listener);
74
+
75
+ return {
76
+ remove: () => {
77
+ // @ts-expect-error
78
+ this._nativeListeners.get(eventName)?.delete(nativeListener);
79
+ this._clientListeners.delete(listener);
80
+ handle.remove();
81
+ },
82
+ };
83
+ }
84
+
85
+ removeAllListeners(
86
+ eventName: keyof ExpoSpeechRecognitionNativeEventMap,
87
+ ): void {
88
+ // Go through _listeners and remove all listeners for this event
89
+ if (this._nativeListeners.has(eventName)) {
90
+ const nativeListeners = this._nativeListeners.get(eventName);
91
+ if (!nativeListeners) {
92
+ return;
93
+ }
94
+
95
+ // Remove the enhanced listeners
96
+ for (const [listener, nativeListener] of this._clientListeners) {
97
+ // if nativeListener in listeners, remove it
98
+ if (nativeListeners.has(nativeListener)) {
99
+ // Remove the enhanced listener
100
+ this.removeListener(eventName, listener);
101
+ }
102
+ }
103
+
104
+ // ...and the native listeners
105
+ for (const listener of nativeListeners) {
106
+ // @ts-expect-error
107
+ this.removeListener(eventName, listener);
108
+ }
109
+
110
+ // Clean up
111
+ this._nativeListeners.delete(eventName);
112
+ }
113
+ }
114
+
115
+ start(options: ExpoSpeechRecognitionOptions) {
13
116
  const SpeechRecognitionClass =
14
117
  typeof webkitSpeechRecognition !== "undefined"
15
118
  ? webkitSpeechRecognition
@@ -22,36 +125,42 @@ export const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {
22
125
 
23
126
  // Re-subscribe to events so that we don't lose them
24
127
  // This covers the case where the user has already subscribed to an event prior to calling `start()`
25
- ExpoSpeechRecognitionModuleEmitter._nativeListeners.forEach(
26
- (listeners, eventName) => {
27
- for (const listener of listeners) {
28
- // May already be subscribed
29
- _speechRecognitionRef?.removeEventListener(eventName, listener);
30
- _speechRecognitionRef?.addEventListener(eventName, listener);
31
- }
32
- },
33
- );
128
+ this._nativeListeners.forEach((listeners, eventName) => {
129
+ for (const listener of listeners) {
130
+ // May already be subscribed
131
+ _speechRecognitionRef?.removeEventListener(eventName, listener);
132
+ _speechRecognitionRef?.addEventListener(eventName, listener);
133
+ }
134
+ });
34
135
 
35
136
  // Start the speech recognition!
36
137
  _speechRecognitionRef.start();
37
- },
38
- getStateAsync: () => {
138
+ }
139
+
140
+ getStateAsync() {
39
141
  console.warn(
40
142
  "getStateAsync is not supported on web. Returning 'inactive'.",
41
143
  );
42
144
  return Promise.resolve("inactive");
43
- },
44
- stop: () => _speechRecognitionRef?.stop(),
45
- abort: () => _speechRecognitionRef?.abort(),
46
- requestPermissionsAsync: () => {
145
+ }
146
+
147
+ stop() {
148
+ _speechRecognitionRef?.stop();
149
+ }
150
+ abort() {
151
+ _speechRecognitionRef?.abort();
152
+ }
153
+
154
+ requestPermissionsAsync() {
47
155
  return Promise.resolve({
48
156
  granted: true,
49
157
  canAskAgain: false,
50
158
  expires: "never",
51
159
  status: "granted",
52
160
  } as PermissionResponse);
53
- },
54
- getPermissionsAsync: () => {
161
+ }
162
+
163
+ getPermissionsAsync() {
55
164
  console.warn(
56
165
  "getPermissionsAsync is not supported on web. Returning a denied permission response.",
57
166
  );
@@ -61,8 +170,9 @@ export const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {
61
170
  expires: "never",
62
171
  status: "denied",
63
172
  } as PermissionResponse);
64
- },
65
- getSupportedLocales: async () => {
173
+ }
174
+
175
+ async getSupportedLocales() {
66
176
  console.warn(
67
177
  "getSupportedLocales is not supported on web. Returning an empty array.",
68
178
  );
@@ -70,37 +180,37 @@ export const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {
70
180
  locales: [] as string[],
71
181
  installedLocales: [] as string[],
72
182
  };
73
- },
74
- addListener: () => {
75
- console.warn(
76
- "addListener is not supported. Use ExpoSpeechRecognitionModuleEmitter(eventName, listener) instead.",
77
- );
78
- },
79
- removeListeners: () => {
80
- console.warn(
81
- "removeListeners is not supported. Use ExpoSpeechRecognitionModuleEmitter.removeListener(eventName, listener) instead.",
82
- );
83
- },
84
- getSpeechRecognitionServices: () => {
183
+ }
184
+
185
+ // addListener() {}
186
+
187
+ // removeListeners() {}
188
+
189
+ getSpeechRecognitionServices() {
85
190
  return [] as string[];
86
- },
87
- getDefaultRecognitionService: () => {
191
+ }
192
+
193
+ getDefaultRecognitionService() {
88
194
  return {
89
195
  packageName: "",
90
196
  };
91
- },
92
- getAssistantService: () => {
197
+ }
198
+
199
+ getAssistantService() {
93
200
  return {
94
201
  packageName: "",
95
202
  };
96
- },
97
- supportsOnDeviceRecognition: () => {
203
+ }
204
+
205
+ supportsOnDeviceRecognition() {
98
206
  return false;
99
- },
100
- supportsRecording: () => {
207
+ }
208
+
209
+ supportsRecording() {
101
210
  return false;
102
- },
103
- androidTriggerOfflineModelDownload: () => {
211
+ }
212
+
213
+ androidTriggerOfflineModelDownload() {
104
214
  console.warn(
105
215
  "androidTriggerOfflineModelDownload is not supported on web. Returning false.",
106
216
  );
@@ -108,11 +218,13 @@ export const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {
108
218
  status: "opened_dialog",
109
219
  message: "Offline model download is not supported on web.",
110
220
  });
111
- },
112
- setCategoryIOS: () => {
221
+ }
222
+
223
+ setCategoryIOS() {
113
224
  console.warn("setCategoryIOS is not supported on web.");
114
- },
115
- getAudioSessionCategoryAndOptionsIOS: () => {
225
+ }
226
+
227
+ getAudioSessionCategoryAndOptionsIOS() {
116
228
  console.warn(
117
229
  "getAudioSessionCategoryAndOptionsIOS is not supported on web.",
118
230
  );
@@ -121,18 +233,20 @@ export const ExpoSpeechRecognitionModule: ExpoSpeechRecognitionModuleType = {
121
233
  categoryOptions: ["defaultToSpeaker", "allowBluetooth"],
122
234
  mode: "measurement",
123
235
  };
124
- },
125
- setAudioSessionActiveIOS: () => {
236
+ }
237
+
238
+ setAudioSessionActiveIOS() {
126
239
  console.warn("setAudioSessionActiveIOS is not supported on web.");
127
- },
128
- isRecognitionAvailable: () => {
240
+ }
241
+
242
+ isRecognitionAvailable() {
129
243
  const hasSpeechRecognitionAPI =
130
244
  typeof webkitSpeechRecognition !== "undefined" ||
131
245
  typeof SpeechRecognition !== "undefined";
132
246
 
133
247
  return hasSpeechRecognitionAPI;
134
- },
135
- };
248
+ }
249
+ }
136
250
 
137
251
  /**
138
252
  * Convert the web SpeechRecognitionEventMap to the native event map for compatibility
@@ -172,93 +286,6 @@ const webToNativeEventMap: {
172
286
  soundend: (ev) => null,
173
287
  };
174
288
 
175
- export const ExpoSpeechRecognitionModuleEmitter = {
176
- _nativeListeners: new Map() as Map<string, Set<(event: any) => void>>,
177
- _clientListeners: new Map() as Map<
178
- // Original listener
179
- (event: any) => void,
180
- // Native listener
181
- (event: any) => void
182
- >,
183
- addListener<T extends keyof SpeechRecognitionEventMap>(
184
- eventName: T,
185
- listener: (ev: ExpoSpeechRecognitionNativeEventMap[T]) => void,
186
- ) {
187
- // Convert the web event to the native event
188
- const nativeListener = (ev: SpeechRecognitionEventMap[T]) => {
189
- const eventPayload = webToNativeEventMap[eventName]?.(ev);
190
- listener(eventPayload);
191
- };
192
-
193
- _speechRecognitionRef?.addEventListener(eventName, nativeListener);
194
- if (!ExpoSpeechRecognitionModuleEmitter._nativeListeners.has(eventName)) {
195
- ExpoSpeechRecognitionModuleEmitter._nativeListeners.set(
196
- eventName,
197
- new Set(),
198
- );
199
- }
200
- // Add the original listener to the enhanced listeners
201
- ExpoSpeechRecognitionModuleEmitter._nativeListeners
202
- .get(eventName)
203
- ?.add(nativeListener);
204
-
205
- // Map the original listener to the enhanced listener
206
- ExpoSpeechRecognitionModuleEmitter._clientListeners.set(
207
- listener,
208
- nativeListener,
209
- );
210
-
211
- return {
212
- remove: () => {
213
- _speechRecognitionRef?.removeEventListener(eventName, nativeListener);
214
- ExpoSpeechRecognitionModuleEmitter._nativeListeners
215
- .get(eventName)
216
- ?.delete(nativeListener);
217
- ExpoSpeechRecognitionModuleEmitter._clientListeners.delete(listener);
218
- },
219
- };
220
- },
221
- removeListener: (eventName: string, listener: (event: any) => void) => {
222
- const resolvedListener =
223
- ExpoSpeechRecognitionModuleEmitter._clientListeners.get(listener) ??
224
- listener;
225
-
226
- _speechRecognitionRef?.removeEventListener(eventName, resolvedListener);
227
- if (ExpoSpeechRecognitionModuleEmitter._nativeListeners.has(eventName)) {
228
- ExpoSpeechRecognitionModuleEmitter._nativeListeners
229
- .get(eventName)
230
- ?.delete(listener);
231
- }
232
- ExpoSpeechRecognitionModuleEmitter._clientListeners.delete(listener);
233
- },
234
- removeAllListeners: (eventName: string) => {
235
- // Go through _listeners and remove all listeners for this event
236
- if (ExpoSpeechRecognitionModuleEmitter._nativeListeners.has(eventName)) {
237
- const nativeListeners =
238
- ExpoSpeechRecognitionModuleEmitter._nativeListeners.get(eventName);
239
- if (!nativeListeners) {
240
- return;
241
- }
242
-
243
- // Remove the enhanced listeners
244
- for (const [
245
- listener,
246
- nativeListener,
247
- ] of ExpoSpeechRecognitionModuleEmitter._clientListeners) {
248
- // if nativeListener in listeners, remove it
249
- if (nativeListeners.has(nativeListener)) {
250
- // Remove the enhanced listener
251
- ExpoSpeechRecognitionModuleEmitter._clientListeners.delete(listener);
252
- }
253
- }
254
-
255
- // Remove the native listeners
256
- for (const listener of nativeListeners) {
257
- ExpoSpeechRecognitionModuleEmitter.removeListener(eventName, listener);
258
- }
259
-
260
- // Clean up
261
- ExpoSpeechRecognitionModuleEmitter._nativeListeners.delete(eventName);
262
- }
263
- },
264
- };
289
+ export const ExpoSpeechRecognitionModule = registerWebModule(
290
+ ExpoSpeechRecognitionModuleWeb,
291
+ );
@@ -1,11 +1,7 @@
1
- import type { Subscription } from "expo-modules-core";
2
-
3
1
  // Import the native module. On web, it will be resolved to ExpoSpeechRecognition.web.ts
4
2
  // and on native platforms to ExpoSpeechRecognition.ts
5
- import {
6
- ExpoSpeechRecognitionModule,
7
- ExpoSpeechRecognitionModuleEmitter,
8
- } from "./ExpoSpeechRecognitionModule";
3
+ import type { EventSubscription } from "expo-modules-core";
4
+ import { ExpoSpeechRecognitionModule } from "./ExpoSpeechRecognitionModule";
9
5
  import type {
10
6
  ExpoSpeechRecognitionNativeEventMap,
11
7
  ExpoSpeechRecognitionOptions,
@@ -210,7 +206,7 @@ export class ExpoWebSpeechRecognition implements SpeechRecognition {
210
206
  androidRecognitionServicePackage: ExpoSpeechRecognitionOptions["androidRecognitionServicePackage"];
211
207
 
212
208
  // keyed by listener function
213
- #subscriptionMap: Map<SpeechListener<any>, Subscription[]> = new Map();
209
+ #subscriptionMap: Map<SpeechListener<any>, EventSubscription[]> = new Map();
214
210
 
215
211
  start() {
216
212
  ExpoSpeechRecognitionModule.requestPermissionsAsync().then(() => {
@@ -381,8 +377,9 @@ export class ExpoWebSpeechRecognition implements SpeechRecognition {
381
377
  wrappedListener as (this: SpeechRecognition, ev: Event) => unknown,
382
378
  );
383
379
 
384
- const subscription = ExpoSpeechRecognitionModuleEmitter.addListener(
380
+ const subscription = ExpoSpeechRecognitionModule.addListener(
385
381
  enhancedEvent.eventName,
382
+ // @ts-expect-error
386
383
  enhancedEvent.nativeListener,
387
384
  );
388
385
 
package/src/index.ts CHANGED
@@ -1,8 +1,4 @@
1
- import {
2
- ExpoSpeechRecognitionModule,
3
- ExpoSpeechRecognitionModuleEmitter,
4
- } from "./ExpoSpeechRecognitionModule";
5
- import type { ExpoSpeechRecognitionNativeEventMap } from "./ExpoSpeechRecognitionModule.types";
1
+ import { ExpoSpeechRecognitionModule } from "./ExpoSpeechRecognitionModule";
6
2
 
7
3
  // Export the SpeechRecognition APIs
8
4
  export {
@@ -12,10 +8,7 @@ export {
12
8
  } from "./ExpoWebSpeechRecognition";
13
9
 
14
10
  // Native module
15
- export {
16
- ExpoSpeechRecognitionModule,
17
- ExpoSpeechRecognitionModuleEmitter,
18
- } from "./ExpoSpeechRecognitionModule";
11
+ export { ExpoSpeechRecognitionModule } from "./ExpoSpeechRecognitionModule";
19
12
 
20
13
  // Hooks
21
14
  export { useSpeechRecognitionEvent } from "./useSpeechRecognitionEvent";
@@ -63,28 +56,24 @@ export const getDefaultRecognitionService =
63
56
  export const getAssistantService =
64
57
  ExpoSpeechRecognitionModule.getAssistantService;
65
58
 
66
- export const addSpeechRecognitionListener = <
67
- T extends keyof ExpoSpeechRecognitionNativeEventMap,
68
- >(
69
- eventName: T,
70
- listener: (ev: ExpoSpeechRecognitionNativeEventMap[T]) => void,
71
- ) => ExpoSpeechRecognitionModuleEmitter.addListener(eventName, listener);
72
-
73
- export {
74
- type ExpoSpeechRecognitionOptions,
75
- type AndroidIntentOptions,
76
- type ExpoSpeechRecognitionNativeEventMap,
77
- type AVAudioSessionCategoryOptionsValue,
78
- type AVAudioSessionModeValue,
79
- type AVAudioSessionCategoryValue,
80
- type AudioEncodingAndroidValue,
81
- type AudioSourceOptions,
82
- type RecordingOptions,
83
- type IOSTaskHintValue,
84
- type SetCategoryOptions,
85
- type ExpoSpeechRecognitionErrorCode,
86
- type ExpoSpeechRecognitionErrorEvent,
87
- type ExpoSpeechRecognitionResultEvent,
88
- type ExpoSpeechRecognitionResult,
89
- type ExpoSpeechRecognitionResultSegment,
59
+ export const addSpeechRecognitionListener =
60
+ ExpoSpeechRecognitionModule.addListener;
61
+
62
+ export type {
63
+ ExpoSpeechRecognitionOptions,
64
+ AndroidIntentOptions,
65
+ ExpoSpeechRecognitionNativeEventMap,
66
+ AVAudioSessionCategoryOptionsValue,
67
+ AVAudioSessionModeValue,
68
+ AVAudioSessionCategoryValue,
69
+ AudioEncodingAndroidValue,
70
+ AudioSourceOptions,
71
+ RecordingOptions,
72
+ IOSTaskHintValue,
73
+ SetCategoryOptions,
74
+ ExpoSpeechRecognitionErrorCode,
75
+ ExpoSpeechRecognitionErrorEvent,
76
+ ExpoSpeechRecognitionResultEvent,
77
+ ExpoSpeechRecognitionResult,
78
+ ExpoSpeechRecognitionResultSegment,
90
79
  } from "./ExpoSpeechRecognitionModule.types";
@@ -1,6 +1,6 @@
1
- import { useEffect, useRef } from "react";
2
- import { ExpoSpeechRecognitionModuleEmitter } from "./ExpoSpeechRecognitionModule";
3
- import type { ExpoSpeechRecognitionNativeEventMap } from "./ExpoSpeechRecognitionModule.types";
1
+ import { ExpoSpeechRecognitionModule } from "./ExpoSpeechRecognitionModule";
2
+ import type { ExpoSpeechRecognitionNativeEvents } from "./ExpoSpeechRecognitionModule.types";
3
+ import { useEventListener } from "expo";
4
4
 
5
5
  /**
6
6
  * This hook allows you to listen to native events emitted by the `ExpoSpeechRecognitionModule`.
@@ -11,20 +11,12 @@ import type { ExpoSpeechRecognitionNativeEventMap } from "./ExpoSpeechRecognitio
11
11
  * @param listener The listener function to call when the event is emitted
12
12
  */
13
13
  export function useSpeechRecognitionEvent<
14
- K extends keyof ExpoSpeechRecognitionNativeEventMap,
15
- >(
16
- eventName: K,
17
- listener: (ev: ExpoSpeechRecognitionNativeEventMap[K]) => void,
18
- ) {
19
- const listenerRef = useRef(listener);
20
- listenerRef.current = listener;
21
-
22
- useEffect(() => {
23
- const handler = listenerRef.current;
24
- const subscription = ExpoSpeechRecognitionModuleEmitter.addListener(
25
- eventName,
26
- handler,
27
- );
28
- return subscription.remove;
29
- }, [eventName]);
14
+ K extends keyof ExpoSpeechRecognitionNativeEvents,
15
+ >(eventName: K, listener: ExpoSpeechRecognitionNativeEvents[K]) {
16
+ return useEventListener(
17
+ ExpoSpeechRecognitionModule,
18
+ eventName,
19
+ // @ts-expect-error
20
+ listener,
21
+ );
30
22
  }