Skip to content

Commit

Permalink
Fixed a build bug
Browse files Browse the repository at this point in the history
  • Loading branch information
heff committed Apr 3, 2020
1 parent 89e40ac commit 33e78d3
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions index.js
Expand Up @@ -60,6 +60,27 @@ function getIdFromURL(url) {
return (match && match[1]) ? match[1] : url;
}

// Handle multiple players with one YT API load
let ytReady = false;
let ytReadyQueue = [];

function onYTReady(callback) {
if (ytReady) {
callback();
} else {
ytReadyQueue.push(callback);
}
}

function handleYoutubeAPILoad() {
ytReady = true;
ytReadyQueue.forEach((callback) => {
callback();
});
ytReadyQueue = [];
}


class YoutubeVideoElement extends HTMLElement {
constructor() {
super();
Expand All @@ -74,24 +95,6 @@ class YoutubeVideoElement extends HTMLElement {
}
}

// Handle multiple players with one YT API load
static ytReady = false;
static ytReadyQueue = [];
static onYTReady = function(callback) {
if (this.ytReady) {
callback();
} else {
this.ytReadyQueue.push(callback);
}
};
static handleYoutubeAPILoad() {
this.ytReady = true;
this.ytReadyQueue.forEach((callback) => {
callback();
});
this.ytReadyQueue = [];
}

load() {
// Destroy previous videos
this.ytPlayer = null;
Expand All @@ -113,7 +116,7 @@ class YoutubeVideoElement extends HTMLElement {
this.shadowRoot.querySelector('#iframeContainer').appendChild(iframeTemplate.content.cloneNode(true));
const iframe = this.shadowRoot.querySelector('iframe');

YoutubeVideoElement.onYTReady(()=>{
onYTReady(() => {
const onPlayerReady = (event) => {
this.dispatchEvent(new Event('volumechange'));

Expand Down Expand Up @@ -257,7 +260,7 @@ function loadYoutubeAPI() {
const firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(YouTubeScriptTag, firstScriptTag);

window.onYouTubeIframeAPIReady = YoutubeVideoElement.handleYoutubeAPILoad.bind(YoutubeVideoElement);
window.onYouTubeIframeAPIReady = handleYoutubeAPILoad;
}

if (window.customElements.get('youtube-video') || window.YoutubeVideoElement) {
Expand Down

0 comments on commit 33e78d3

Please sign in to comment.