innFactory / react-native-dialogflow

A React-Native Bridge for the Google Dialogflow (API.AI) SDK
https://innfactory.de/softwareentwicklung/ui-ux/sprachsteuerung-mit-api-ai-in-einer-react-native-app/
MIT License
200 stars 64 forks source link

Every call to startListening adds a listener but never removes it #39

Closed gunslingerfry closed 6 years ago

gunslingerfry commented 6 years ago

The result is that speech result n calls requestQuery n times. This is my naive fix on the Android side. I assume a similar solution would work for IOS as well but I haven't tested it.

dialogflow2.startListening = function (onResult, onError) {                                                                                                                
    let resultWrapper = function(result) {                                                                                                                                 
        // create a wrapper around the function to remove listener                                                                                                         
        dialogflow2.subscription.remove();                                                                                                                                 
        // then call the original callback                                                                                                                                 
        onResult(result);                                                                                                                                                  
    };                                                                                                                                                                     

    dialogflow2.subscription = NativeAppEventEmitter.addListener(                                                                                                          
        'onSpeechResults',                                                                                                                                                 
        (result) => {                                                                                                                                                      
            if (result.value) {                                                                                                                                            
                // dialogflow2.requestQuery(result.value[0], onResult, onError);                                                                                           
                dialogflow2.requestQuery(result.value[0], resultWrapper, onError);                                                                                         
            }                                                                                                                                                              

        }                                                                                                                                                                  
    );                                                                                                                                                                     

    Voice.start(dialogflow2.languageTag);                                                                                                                                  
}       
spoeck commented 6 years ago

Thanks for reporting the issue and also for your solution. I fixed it with removing the listener initialisation from the startListening method. Now the listener is set when setConfiguration is called (usually only one times).

Should be fixed with v3.2.0