Skip to content

Commit

Permalink
Remove deprecated ResourceManager
Browse files Browse the repository at this point in the history
Fixes #608
  • Loading branch information
markcellus committed Jun 28, 2023
1 parent 59db646 commit aa52f5d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
31 changes: 22 additions & 9 deletions src/youtube-video.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import ResourceManager from 'resource-manager-js';

declare global {
// eslint-disable-next-line no-unused-vars
interface Window {
Expand All @@ -25,9 +23,13 @@ export class YoutubeVideoElement extends HTMLElement {
private scriptPath: string = 'https://www.youtube.com/iframe_api';
private mediaError: Error = undefined;

id: string;

constructor() {
super();
videos.set(this, this.id);
const id = this.getAttribute('id') || `ytPlayer-${videos.size}`;
videos.set(this, id);
this.id = id;
}

connectedCallback() {
Expand Down Expand Up @@ -73,10 +75,6 @@ export class YoutubeVideoElement extends HTMLElement {
return this.hasAttribute('playsinline');
}

get id(): string {
return this.getAttribute('id') || `ytPlayer-${videos.size}`;
}

get controls(): boolean {
return this.hasAttribute('controls') || true;
}
Expand Down Expand Up @@ -200,6 +198,10 @@ export class YoutubeVideoElement extends HTMLElement {
}
}

private get loadScriptId() {
return `youtube-video-${this.id}`;
}

private loadYTScript(): Promise<void> {
// Load the IFrame Player API code asynchronously.
if (!YoutubeVideoElement.scriptLoadPromise) {
Expand All @@ -217,15 +219,26 @@ export class YoutubeVideoElement extends HTMLElement {
if (originalOnYouTubeIframeAPIReady) {
originalOnYouTubeIframeAPIReady(...args);
}
resolve();
};
return ResourceManager.loadScript(this.scriptPath);

// load youtube's script tag
const script = document.createElement('script');
script.id = this.loadScriptId;
script.src = this.scriptPath;
const firstScriptTag =
document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(script, firstScriptTag);
});
}
return YoutubeVideoElement.scriptLoadPromise;
}

private unloadYTScript() {
ResourceManager.unloadScript(this.scriptPath);
const script = document.querySelector(`#${this.loadScriptId}`);
if (script) {
script.remove();
}
YoutubeVideoElement.scriptLoadPromise = null;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/youtube-video-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('Youtube Video Tests', function () {
testContainer.removeChild(videoEl);
});

it('should load proper iFrame player api script when load() is called and remove it from the dom when removed', async function () {
it('loads proper iFrame player api script when load() is called and remove it from the dom when removed', async function () {
var videoEl = document.createElement(
'youtube-video'
) as YoutubeVideoElement;
Expand Down

0 comments on commit aa52f5d

Please sign in to comment.