dailymotion / dailymotion-sdk-js

Dailymotion JavaScript client API
https://developers.dailymotion.com/api/platform-api/
MIT License
34 stars 33 forks source link

Unexpected behavior about event `off` #100

Closed liamzzz0714 closed 2 years ago

liamzzz0714 commented 2 years ago

Problem

I'm using Dailymotion player and encountering some problems.

https://developers.dailymotion.com/player/#events As mentioned in the document off()

To remove a previously registered event listener from a player instance. If the listener is not specified, it will remove any existing listeners associated with the event

Though I did specify a event handler, it removed all listeners. thanks.

How to reproduce

async function main() {
  const script = document.createElement("script");
  script.src = `https://geo.dailymotion.com/libs/player.js`;
  await new Promise((resolve) => {
    script.addEventListener("load", resolve);
    document.head.append(script);
  });

  const rootElement = document.createElement("div");
  Object.assign(rootElement.style, {
    width: "400px",
    height: "300px",
  });
  document.body.prepend(rootElement);
  rootElement.id = "dm-player-v2";

  const player = await window.dailymotion.createPlayer(rootElement.id, {
    video: "x7tgad0",
  });
  console.log("init dm player", { player });
  const hello = (e) => console.log("hello", e);
  const world = (e) => console.log("world", e);
  player.on("pause", hello);
  player.on("pause", world);
  player.on("pause", (e) => console.log("bang", e));

  player.off("pause", hello); // remove all event handlers
  player.off("pause", ()=>{}); // remove all event handlers too
}
mccarthypaulmichael commented 2 years ago

Hey Liam,

Thanks for sharing the issue, to note this repo is our older JS SDK product and the support for it will be discontinued as highlighted in the doc. However, i do see you are using our latest embed type found here: https://developers.dailymotion.com/player/#player-library-script.

Kind regards