-
Notifications
You must be signed in to change notification settings - Fork 46
fix: <vimeo-video> element race condition
#220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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?.() | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stale
|
||
|
|
||
| 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) { | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reordered
loadCompleterefresh enables nullapidereferenceMedium Severity
Moving
if (this.#hasLoaded) this.loadComplete = new PublicPromise()from before the 1-tickawaitto after it introduces a timing regression on second+ loads. Whensrcandloopchange in the same synchronous block (common in React/Next.js), theloophandler'sawait this.loadCompleteresolves immediately on the stale promise. Its continuation then runs afterload()setsthis.api = null, causingthis.api.setLoop(...)to throw aTypeError. Previously,loadCompletewas refreshed before theawait, so theloophandler would correctly suspend until the new load completed.Additional Locations (1)
packages/vimeo-video-element/vimeo-video-element.js#L358-L365