Question

I am currently working with a cordova plugin which uses the AudioStreamer framework to control and stream audio. I am looking to intercept callbacks sent from the js portion of the plugin in the main javascript code in my application, for example 'pause_success'. When a calls the pause method of the plugin this:

[Error] TypeError: 'undefined' is not a function (evaluating 'window.plugins.stream.pause_success()')
    global code (index.html, line 1)

i have tried:

window.plugins.stream.pause_success = function(success){
    if(success){
        alert('success');
    }

};

which does nothing

and:

window.plugins.stream.pause_success(function (){
    if(success){
        alert('success');
    }

});

Which results in the following error

[Error] RangeError: Maximum call stack size exceeded.
    pause_success (StreamManager.js, line 60)
    pause_success (StreamManager.js, line 60)
    pause_success (StreamManager.js, line 60)
    pause_success (StreamManager.js, line 60)
    pause_success (StreamManager.js, line 60)
    pause_success (StreamManager.js, line 60)
    pause_success (StreamManager.js, line 60)
    pause_success (StreamManager.js, line 60)
    pause_success (StreamManager.js, line 60)
    pause_success (StreamManager.js, line 60)
    pause_success (StreamManager.js, line 60)
    pause_success (StreamManager.js, line 60)
    etc etc

The code for the success callback on the plugin side is as follows:

StreamManager.prototype.pause_success = function(){
      if(this.pause_success){
        this.pause_success();
      }
    }

with the actual pause function as follows:

StreamManager.prototype.pause = function(options, success, fail){
  this.pause_success = success;
  this.pause_error = fail;
    cordova.exec(null, null, "Stream", "pause", [options]);
}

Does anyone know the correct way to intercept these callbacks or is it just something done improperly on the plugin side?

The plugin repository can be found here: https://github.com/feedhenry/fh-cordova-plugin-stream

Thanks for your time.

Was it helpful?

Solution

My understanding (but again I don't know this plugin) is that you should call the action function and pass it the name of the action, a callback to call in case of success and a callback to call in case of failure.

You don't need to modify the plugin's function or try to intercept the callback since the plugin calls the callback you specify.

For example it could look something like that:

function pauseSuccess(){
    alert("pause success!")
}
function pauseError(){
    alert("pause error!")
}
function pause(){
    StreamManager.action({act:'pause'},pauseSuccess,pauseError);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top