Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 45 additions & 37 deletions packages/vimeo-video-element/vimeo-video-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,13 @@ 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;

// Wait 1 tick to allow other attributes to be set.
await (this.#loadRequested = Promise.resolve());
this.#loadRequested = null;

const isFirstLoad = !this.#hasLoaded;
if (this.#hasLoaded) this.loadComplete = new PublicPromise();
this.#hasLoaded = true;

this.#currentTime = 0;
this.#duration = NaN;
Expand Down Expand Up @@ -185,50 +183,62 @@ 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;

let iframe = this.shadowRoot?.querySelector('iframe');

this.#isInit = true;
if (isFirstLoad && iframe) {
this.#config = JSON.parse(iframe.getAttribute('data-config') || '{}');
}

let iframe = this.shadowRoot?.querySelector('iframe');
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;
this.loadComplete = new PublicPromise();
super.disconnectedCallback?.()
}
Comment thread
cursor[bot] marked this conversation as resolved.

#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'));

if (!this.shadowRoot) {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = getTemplateHTML(namedNodeMapToObject(this.attributes), this);
iframe = this.shadowRoot.querySelector('iframe');
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) => {
Expand All @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
Loading