简体   繁体   中英

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

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

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
        }
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM