The addPlayer code has a lot of interaction with videoPlayer, so adding that as a global makes good sense.
add-player.js
/*global utils videoPlayer */
const addPlayer = (function makeAddPlayer() {
There are two other things that are referenced though, one being YT and the other being manageCover.
YT is only accessed to get the numeric value of the playing and ended player state. Even though it is possible to not access YT and just put in the appropriate values, and those values aren’t likely to change, but it is more reliable to make use of the PlayerState information that is already there, and it is also appropriate for the addPlaer code to access the PlayerState information. Due to that, adding YT to the list of globals makes good sense.
add-player.js
/*global utils videoPlayer YT */
const addPlayer = (function makeAddPlayer() {
With manageCover though, that’s used when the player is ready.
return function playerReadyCallback() {
manageCover.init(cover, showVideo);
cover.dispatchEvent(afterPlayerReady);
};
In this case it’s quite inappropriate for the addPlayer code to know anything about manageCover. We should remove that and put it into the afterPlayerReady event instead.
To achieve that we can adjust the afterPlayerReady code to be a function instead, and we want to have cover available, so we need to check evt to find out how we can get cover.
add-players.js
addPlayer.init({
afterAddPlayer: toggleSpinner,
afterPlayerReady: function (evt) {
toggleSpinner();
console.log(evt);
}
});
Because we are wanting different cover information on the event, we shouldn’t define the event types immediately up front.
add-player.js
// const events = {
// afterAddPlayer: new Event("afterAddPlayer"),
// afterPlayerReady: new Event("afterPlayerReady")
// };
const events = {};
We can define those events in the addEvents function instead.
function addEvents(cover) {
events.afterAddPlayer = new Event("afterAddPlayer");
events.afterPlayerReady = new Event("afterPlayerReady");
cover.addEventListener("afterAddPlayer", config.afterAddPlayer);
cover.addEventListener("afterPlayerReady", config.afterPlayerReady);
}
Adding the cover information to the events is now easy to achieve, by using CustomEvent instead.
function addEvents(cover) {
events.afterAddPlayer = new Event("afterAddPlayer");
events.afterPlayerReady = new CustomEvent("afterPlayerReady", {
detail: {cover}
});
cover.addEventListener("afterAddPlayer", config.afterAddPlayer);
cover.addEventListener("afterPlayerReady", config.afterPlayerReady);
}
With a custom event, all the custom stuff must be accessed via the detail property.
The addPlayers code can now access the cover information.
addPlayer.init({
afterAddPlayer: toggleSpinner,
afterPlayerReady: function (evt) {
toggleSpinner(evt);
console.log({cover: evt.detail});
}
});
So we can now move the manageCover code out of addPlayer, and in to addPlayers instead.
add-player.js
function afterReadyWrapper(cover, afterPlayerReady) {
return function playerReadyCallback() {
cover.dispatchEvent(afterPlayerReady);
};
}
add-players.js
function showVideo(cover) {
const videoWrapper = cover.nextElementSibling;
utils.show(videoWrapper);
videoPlayer.play(videoWrapper);
}
addPlayer.init({
afterAddPlayer: toggleSpinner,
afterPlayerReady: function (evt) {
const cover = evt.detail.cover;
toggleSpinner(evt);
manageCover.init(cover, showVideo);
}
});
Lastly because it is a CustomEvent being used, we should add that to the list of globals too.
add-player.js
/*global utils videoPlayer YT CustomEvent */
const addPlayer = (function makeAddPlayer() {
And that is the addPlayer code more appropriately structured. The code at https://jsitor.com/1xqkXKIrsv has been updated, and there is only the addPlayers code that’s left to investigate.