diff --git a/packages/vimeo-video-element/vimeo-video-element.js b/packages/vimeo-video-element/vimeo-video-element.js index 6b5f231..261411f 100644 --- a/packages/vimeo-video-element/vimeo-video-element.js +++ b/packages/vimeo-video-element/vimeo-video-element.js @@ -138,17 +138,16 @@ class VimeoVideoElement extends MediaPlayedRangesMixin(globalThis.HTMLElement ?? } async load() { - if (this.#loadRequested) return; - const isFirstLoad = !this.#hasLoaded; - - if (this.#hasLoaded) this.loadComplete = new PublicPromise(); - this.#hasLoaded = true; + if (this.#loadRequested) return; // Wait 1 tick to allow other attributes to be set. await (this.#loadRequested = Promise.resolve()); this.#loadRequested = null; + if (this.#hasLoaded) this.loadComplete = new PublicPromise(); + this.#hasLoaded = true; // TODO: Identify how hasLoaded differs from isInit + this.#currentTime = 0; this.#duration = NaN; this.#muted = false; @@ -185,50 +184,61 @@ class VimeoVideoElement extends MediaPlayedRangesMixin(globalThis.HTMLElement ?? ...this.#config, }; - const onLoaded = async () => { - this.#readyState = 1; // HTMLMediaElement.HAVE_METADATA - this.dispatchEvent(new Event('loadedmetadata')); - - if (this.api) { - this.#muted = await this.api.getMuted(); - this.#volume = await this.api.getVolume(); - this.dispatchEvent(new Event('volumechange')); - - this.#duration = await this.api.getDuration(); - this.dispatchEvent(new Event('durationchange')); - } - - this.dispatchEvent(new Event('loadcomplete')); - this.loadComplete.resolve(); - }; - if (this.#isInit) { this.api = oldApi; await this.api.loadVideo({ ...options, url: this.src, }); - await onLoaded(); + await this.#onLoaded(); await this.loadComplete; return; - } + } else { + this.#isInit = true; - this.#isInit = true; + let iframe = this.shadowRoot?.querySelector('iframe'); - let iframe = this.shadowRoot?.querySelector('iframe'); + if (isFirstLoad && iframe) { + this.#config = JSON.parse(iframe.getAttribute('data-config') || '{}'); + } + + if (!this.shadowRoot) { + this.attachShadow({ mode: 'open' }); + this.shadowRoot.innerHTML = getTemplateHTML(namedNodeMapToObject(this.attributes), this); + iframe = this.shadowRoot.querySelector('iframe'); + } - if (isFirstLoad && iframe) { - this.#config = JSON.parse(iframe.getAttribute('data-config') || '{}'); + this.api = new VimeoPlayerAPI(iframe, options); + this.#setupApiListeners(); + await this.loadComplete; } + } + + disconnectedCallback() { + this.#loadRequested = null; + this.#hasLoaded = null; + this.#isInit = null; + super.disconnectedCallback?.() + } - if (!this.shadowRoot) { - this.attachShadow({ mode: 'open' }); - this.shadowRoot.innerHTML = getTemplateHTML(namedNodeMapToObject(this.attributes), this); - iframe = this.shadowRoot.querySelector('iframe'); + #onLoaded = async () => { + this.#readyState = 1; // HTMLMediaElement.HAVE_METADATA + this.dispatchEvent(new Event('loadedmetadata')); + + if (this.api) { + this.#muted = await this.api.getMuted(); + this.#volume = await this.api.getVolume(); + this.dispatchEvent(new Event('volumechange')); + + this.#duration = await this.api.getDuration(); + this.dispatchEvent(new Event('durationchange')); } - this.api = new VimeoPlayerAPI(iframe); + this.dispatchEvent(new Event('loadcomplete')); + this.loadComplete.resolve(); + }; + #setupApiListeners() { const textTracksVideo = document.createElement('video'); this.textTracks = textTracksVideo.textTracks; this.api.getTextTracks().then((vimeoTracks) => { @@ -247,7 +257,7 @@ class VimeoVideoElement extends MediaPlayedRangesMixin(globalThis.HTMLElement ?? const onceLoaded = () => { this.api.off('loaded', onceLoaded); - onLoaded(); + this.#onLoaded(); }; this.api.on('loaded', onceLoaded); @@ -331,8 +341,6 @@ class VimeoVideoElement extends MediaPlayedRangesMixin(globalThis.HTMLElement ?? this.#videoHeight = videoHeight; this.dispatchEvent(new Event('resize')); }); - - await this.loadComplete; } async attributeChangedCallback(attrName, oldValue, newValue) {