Skip to content
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

Close a tiny gap in A-Frame loading order #5248

Merged

Conversation

wmurphyrd
Copy link
Collaborator

@wmurphyrd wmurphyrd commented Feb 19, 2023

Description:

There is a small period between document.readyState === 'interactive' and DOMContentLoaded, notably this is when deferred scripts (including modules) run, so the existing checks leave a gap where attaching an entity would cause it to initialze early and fail. fixes #5228

Changes proposed:

  • Track whether DOMContentLoaded has fired with a static property on ANode
  • Use that static property instead of readyState in connected callbacks to check if DOM is ready

@@ -292,6 +292,8 @@ ANode.evtData = {};
ANode.newMixinIdArray = [];
ANode.oldMixinIdArray = [];
ANode.mixinIds = {};
ANode.DOMContentHasLoaded = false;
Copy link
Collaborator Author

@wmurphyrd wmurphyrd Feb 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally thought of adding this to window.AFRAME, but I see that's not a current pattern in the codebase to reference the global AFRAME from src files, so added it to the ANode static props instead.

@@ -292,6 +292,8 @@ ANode.evtData = {};
ANode.newMixinIdArray = [];
ANode.oldMixinIdArray = [];
ANode.mixinIds = {};
ANode.DOMContentHasLoaded = false;
document.addEventListener('DOMContentLoaded', function () { ANode.DOMContentHasLoaded = true; }, { once: true });
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels a little weird having this listener as a side-effect of the module. Another option would be to put ANode.DOMContentHasLoaded = true; inside doConnectedCallback, but that would entail repeating the assignment unnecessarily for each entity in the scene

@@ -1,5 +1,6 @@
/* global customElements, HTMLElement */
var debug = require('../utils/debug');
var ANode = require('./a-node').ANode;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not thrilled about adding an import here - but it seemed better to keep the logic consistent

@vincentfretin
Copy link
Contributor

Thanks @wmurphyrd, the changes looks simple and sound so I'm not afraid of regression here. If this fixes the edge case, that's great. @dmarcos What do you think?

@dmarcos
Copy link
Member

dmarcos commented Feb 21, 2023

I would like to understand better the sequence of events so we make sure we are addressing the root of the issue and not jus the symptoms. Why is there a gap between document.readyState === 'interactive' and DOMContentLoaded? Is it specified in the standard, an implementation quirk in some browsers? What's the interplay with deferred scripts?

@wmurphyrd
Copy link
Collaborator Author

I would like to understand better the sequence of events so we make sure we are addressing the root of the issue and not jus the symptoms. Why is there a gap between document.readyState === 'interactive' and DOMContentLoaded? Is it specified in the standard, an implementation quirk in some browsers? What's the interplay with deferred scripts?

Sure thing. This is specified in the standard:

The DOMContentLoaded event fires after the transition to "interactive" but before the transition to "complete", at the point where all subresources apart from async script elements have loaded.

https://html.spec.whatwg.org/multipage/dom.html#reporting-document-loading-status

So the (abbreviated) sequence of events is:

  1. readyState: 'loading'
  2. readyState: 'interactive'
  3. Execute deferred scripts (including modules)
  4. DOMContentLoaded
  5. readyState: 'complete'

During step 3, the existing check in ANode.connectedCallback would tell the entity to try to initialize immediately even though DOMContentLoaded has not yet fired an thus the systems have not yet initialized, creating the error seen in the reprex in #5228

@@ -20,7 +20,7 @@ class AAssets extends ANode {

connectedCallback () {
// Defer if DOM is not ready.
if (document.readyState === 'loading') {
Copy link
Member

@dmarcos dmarcos Feb 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about if we check for document.readyState !== 'complete' instead? I think that would wait until deferred scripts have already run

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would need to call this.doConnectedCallback in the DOMContentLoaded event handler

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively we can listen to readystatechange and wait for document.readyState === 'complete' instead of listening to DOMContentLoaded

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about if we check for document.readyState !== 'complete' instead? I think that would wait until deferred scripts have already run

If we checked against readyState complete but still listened for DOMContentLoaded, that would open another gap in the loading order:

  1. readyState: 'loading'
  2. readyState: 'interactive'
  3. Execute deferred scripts (including modules)
  4. DOMContentLoaded
  5. Execute DOMContentLoaded listeners
  6. readyState: 'complete'

During step 5, the readyState would still be interactive, so any entities attached from inside a DOMContentLoaded listener would never initialize as they would exit early from their connectedCallback and stay forever waiting for the DOMContentLoaded event that has already passed

would need to call this.doConnectedCallback in the DOMContentLoaded event handler

It does call through to doConnectedCallback as-is because the listener for the ANode static property is registered first and thus updates ANode.DOMContentHasLoaded before this comes back in via DOMContentLoaded listener. However, if you're open to updating all of these listeners to call doConnectedCallback I would love to make that change because it makes it much easier to subclass these when they call into doConnectedCallback via this rather than prototype.doConnectedCallback like how a-entity does currently

alternatively we can listen to readystatechange and wait for document.readyState === 'complete' instead of listening to DOMContentLoaded

Yes I proposed this as one of the two possible solutions when opening #5228. So that would look like:

if (readyState !== 'complete') {
  // vital that the callback here is connectedCallback and not doConnectedCallback
  // to make sure we don't initialize early on the loading->interactive transition
  document.addEventListener('readystatechange', this.connectedCallback.bind(this));
  return;
}

Note this would conflict with the previous suggesting about changing the callback to doConnectedCallback. If we still wanted to do that as well, we'd need to add an event handler function that checked the current readyState to make sure it was complete before calling doConnectedCallback

Copy link
Collaborator Author

@wmurphyrd wmurphyrd Feb 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops on second thought the readystatechange solution proposed above would not work; it could stack listeners and initialize twice. Would have to go with the creating a readystatechange handler

onReadyStateChange () {
  if (document.readyState === 'complete') {
    this.doConnectedCallback()
  }
}

connectedCallback () {
  // Defer if DOM is not ready.
  if (document.readyState !== 'complete') {
    document.addEventListener('readystatechange', this.onReadyStateChange.bind(this));
    return;
  }
  this.doConnectedCallback()
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readystatechange

Thanks for all the info and patience. I think this solution looks simpler. Can we incorporate in the PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good

Copy link
Collaborator Author

@wmurphyrd wmurphyrd Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmarcos I did the readystatechange version, but it occurred to me during the work that this could be a breaking change. Anyone using DOMContentLoaded listeners of their own with A-Frame probably expects the scene and entities to be initialized (although I guess that would depend on whether they registered their listerens before or after A-Frame), but this would change it so scene and entities are never initialized during DOMContentLoaded listener callbacks.

The unit tests may also be bearing this out, as the readystatechange version has one failure whereas this current version had none:

SUMMARY:
✔ 2302 tests completed
ℹ 16 tests skipped
✖ 1 test failed

FAILED TESTS:
    playSound
      ✖ plays sound if sound already playing when changing src
        Firefox 110.0 (Ubuntu 0.0.0)
      Timeout of 3000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

So just wanted to check in if you still want to go this direction with readystatechange. If so, I'll dig into the unit test failure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In A-Frame the 'loaded' event should be used. DOMContentLoaded is not very useful since won't guarantee the scene, assets and entities are ready to go.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readystatechange version is up - that test failure I noted above - cannot get it to happen again. Tried several times locally and it passes in CI as well. Must have been a fluke.

This version still resolves my issues with deferred scripts & SVAWC

@wmurphyrd wmurphyrd force-pushed the 5228-track-dom-content-loaded-state branch from 3c08e6e to 2186d62 Compare February 24, 2023 15:00
@dmarcos
Copy link
Member

dmarcos commented Feb 26, 2023

Thanks so much! Let's merge and see how it goes. Unfortunate that document readystate, DOMContentLoaded and interplay with different kind of scripts (modules, deferred, async) has gotten so complicated. Hopefully it doesn't bite us again.

@dmarcos dmarcos merged commit 0d97218 into aframevr:master Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Edge case in load order with <script defer> - TypeError: system is undefined
3 participants