react-native-voice / voice

:microphone: React Native Voice Recognition library for iOS and Android (Online and Offline Support)
MIT License
1.76k stars 470 forks source link

Cannot update listeners while Voice is recording. #474

Open yrichard opened 5 months ago

yrichard commented 5 months ago

The current API doesn't allow to update listeners after Voice.start() while it's recording. This can be problematic if you're using hooks and your callback functions need to update as your component state is changing.

I've written the following patch for anyone interested, to be applied with patch-package:

diff --git a/node_modules/@react-native-community/voice/dist/index.js b/node_modules/@react-native-community/voice/dist/index.js
index a10af13..6852f11 100644
--- a/node_modules/@react-native-community/voice/dist/index.js
+++ b/node_modules/@react-native-community/voice/dist/index.js
@@ -52,7 +52,7 @@ class RCTVoice {
     }
     start(locale, options = {}) {
         if (!this._loaded && !this._listeners && voiceEmitter !== null) {
-            this._listeners = Object.keys(this._events).map((key) => voiceEmitter.addListener(key, this._events[key]));
+            this._listeners = Object.keys(this._events).map((key) => voiceEmitter.addListener(key, (event) => {return this._events[key](event);}));
         }
         return new Promise((resolve, reject) => {
             const callback = (error) => {`