Unsubscribe specific subscription with Deezer Javascript SDK DZ.Event.unsubscribe

138 views Asked by At

In the Deezer Javascript SDK, the DZ.Event.unsubscribe method can be used to unsubscribe all subscriptions for a specific event, but is there any way to unsubscribe a specific subscription instead of all of them?

The DZ.Event.subscribe method doesn't return any ID that could be given in the unsubscription call, and sending the subscription callback function to the unsubscription method doesn't work either:

function callback(args) {
  console.log('GOT player_position 1', args);
}
DZ.Event.subscribe('player_position', callback);
DZ.Event.subscribe('player_position', args => {
  console.log('GOT player_position 2', args);
});
DZ.Event.unsubscribe('player_position', callback); // Unsubscribes both subscriptions
1

There are 1 answers

0
Pierrickouw On BEST ANSWER

There are currently only nasty ways to do this with access to DZ.Event.callbacks

For instance you can:

//declare your own unsubscribe function
var unsubscribe = (event, callback) => {
    if(DZ.Event.callbacks[event]) {
        let index = DZ.Event.callbacks[event].indexOf(callback); //find the index of your callback
        if(index !== -1) {
            DZ.Event.callbacks[event].splice(index, 1); //remove it
        }
    }
}