[SOLVED] Help with listen() and unlisten() helpers

Edit: This was indeed a silly mistake on my end :stuck_out_tongue:

I’m running into a little trouble using the provided JS Helpers. What I’m trying to do is unbind a PubSub topic listener when the video has been paused, and then rebind a listener when the video is unpaused.

What happens with the code below is:

  1. Refresh the channel page, and I can see onContext() fire in the console; its first changed contents include “isPaused” but the matching context value is false. At this point I can send a PubSub message from my live_config and it’s received as expected on the channel page.
  2. The video is paused, and I can see the appropriate changed/context values. I send another PubSub message, but it isn’t received. So far so good.
  3. The video is unpaused, the appropriate changed/context values are seen. Another PubSub message is sent, but now it’s being received four times. If the video is paused/unpaused again, and another message sent, it is received eight times in total, and so on.

What am I misunderstanding, or how can I help troubleshoot more?

const broadcastListener = (target, contentType, message) => {
  console.log("PubSub received:", message);
};

window.Twitch.ext.onContext((context, changed) => {
  if (changed.includes("isPaused") && context.isPaused) {
    window.Twitch.ext.unlisten("broadcast", broadcastListener);
  } else if (changed.includes("isPaused") && !context.isPaused) {
    window.Twitch.ext.listen("broadcast", broadcastListener);
  }
});

oncontext is called like every 5 seconds, so you ARE making a call to listen lots of times.

window.Twitch.ext.unlisten("broadcast", broadcastListener);
var isPaused = false;
window.Twitch.ext.onContext((context, changed) => {
   if (changed.includes("isPaused") && context.isPaused) {
     isPaused = true;
   } else if (changed.includes("isPaused") && !context.isPaused) {
     isPaused = false;
   }
 });

Then add a if (isPaused) {return;} to your broadcastListener

How so? I’m only issuing listen/unlisten calls if "isPaused" appears in the array of context properties that were changed. In testing those changed properties appeared to report correctly.

Hmm I’d have to test it…

Personally I’d just set a flag (in the context callback) and test the flag when the EBS transmits and the (listen) callback fires.

Rather than just listen/unlisten.

So listen once.