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

Make sure that hasLoaded flag is only true when the loaded event has fired. Alternative implementation of commit 924dc00 (fix #5425) #5430

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 7 additions & 6 deletions src/core/a-entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ class AEntity extends ANode {
var name;
var componentsToUpdate = this.componentsToUpdate;

if (!this.hasLoaded && !this.sceneEl) { return; }
if (!this.hasLoaded && !this.isLoading) { return; }

// Gather mixin-defined components.
for (i = 0; i < this.mixinEls.length; i++) {
Expand Down Expand Up @@ -511,7 +511,7 @@ class AEntity extends ANode {
var key;

// Already playing.
if (this.isPlaying || (!this.hasLoaded && !this.sceneEl)) { return; }
if (this.isPlaying || (!this.hasLoaded && !this.isLoading)) { return; }
this.isPlaying = true;

// Wake up all components.
Expand Down Expand Up @@ -568,7 +568,7 @@ class AEntity extends ANode {
/**
* When mixins updated, trigger init or optimized-update of relevant components.
*/
mixinUpdate (newMixins, oldMixins) {
mixinUpdate (newMixins, oldMixins, deferred) {
var componentsUpdated = AEntity.componentsUpdated;

var component;
Expand All @@ -577,14 +577,15 @@ class AEntity extends ANode {
var i;
var self = this;

if (!deferred) { oldMixins = oldMixins || this.getAttribute('mixin'); }

if (!this.hasLoaded) {
this.addEventListener('loaded', function () {
self.mixinUpdate(newMixins, oldMixins);
this.addEventListener('loaded-private', function () {
self.mixinUpdate(newMixins, oldMixins, true);
}, ONCE);
return;
}

oldMixins = oldMixins || this.getAttribute('mixin');
mixinIds = this.updateMixins(newMixins, oldMixins);

// Loop over current mixins.
Expand Down
9 changes: 9 additions & 0 deletions src/core/a-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,14 @@ class ANode extends HTMLElement {
}
});

self.isLoading = true;
self.setupMutationObserver();
if (cb) { cb(); }
self.isLoading = false;
self.hasLoaded = true;
// loaded-private is an event analog to loaded that gives A-Frame an opportunity to manage internal
// affairs before the publicly loaded event fires and corresponding handlers executed.
self.emit('loaded-private', undefined, false);
self.emit('loaded', undefined, false);
});
}
Expand Down Expand Up @@ -225,6 +230,10 @@ class ANode extends HTMLElement {
this.computedMixinStr);
}

if (newMixinIds.length === 0) {
window.HTMLElement.prototype.removeAttribute.call(this, 'mixin');
}

return mixinIds;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ Component.prototype = {

// Just cache the attribute if the entity has not loaded
// Components are not initialized until the entity has loaded
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps update this comment to something like "... until the entity starts loading"

if (!el.hasLoaded && !el.sceneEl) {
if (!el.hasLoaded && !el.isLoading) {
this.updateCachedAttrValue(attrValue);
return;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/core/a-entity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ suite('a-entity', function () {
parentEl.appendChild(el2);
});

test('update method is called only once', function (done) {
var triggerEl = document.createElement('a-entity');

registerComponent('trigger', {
init: function () {
var childEl = document.createElement('a-entity');
childEl.setAttributeNS(null, 'visible', 'false');
childEl.addEventListener('loaded', function () {
sinon.assert.calledOnce(updateSpy);
done();
});
this.el.sceneEl.appendChild(childEl);
childEl.setAttribute('visible', true);
}
});
var updateSpy = sinon.spy(components.visible.Component.prototype, 'update');
triggerEl.setAttribute('trigger', '');
el.sceneEl.appendChild(triggerEl);
});

suite('attachedCallback', function () {
test('initializes 3D object', function (done) {
elFactory().then(el => {
Expand Down
24 changes: 24 additions & 0 deletions tests/core/a-mixin.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global assert, setup, suite, test */
var helpers = require('../helpers');
var registerComponent = require('index').registerComponent;

suite('a-mixin', function () {
var assetsEl;
Expand Down Expand Up @@ -79,6 +80,29 @@ suite('a-mixin', function () {
});
});

test('allows mixin to be removed during component init', function (done) {
var mixinEl = document.createElement('a-mixin');
var testEl = document.createElement('a-entity');
mixinEl.setAttribute('id', 'red');
mixinEl.setAttribute('material', 'color: red');
assetsEl.appendChild(mixinEl);

registerComponent('delete-mixin', {
init: function () {
this.el.removeAttribute('mixin');
}
});

testEl.setAttribute('mixin', 'red');
testEl.setAttribute('delete-mixin', '');
testEl.addEventListener('loaded', function () {
assert.equal(testEl.getAttribute('mixin'), null);
assert.equal(testEl.getAttribute('material'), '');
done();
});
el.sceneEl.appendChild(testEl);
});

test('allows mixin to define mixin pre-attach', done => {
var mixinEl1;
var mixinEl2;
Expand Down
26 changes: 26 additions & 0 deletions tests/extras/primitives/primitives.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global AFRAME, assert, suite, test, THREE */
var helpers = require('../../helpers');
var registerPrimitive = require('extras/primitives/primitives').registerPrimitive;
var registerComponent = require('index').registerComponent;
var primitives = require('extras/primitives/primitives').primitives;

var primitiveId = 0;
Expand Down Expand Up @@ -339,6 +340,31 @@ suite('registerPrimitive (using innerHTML)', function () {
});
});

test('handles primitive created and updated in init method', function (done) {
var el = helpers.entityFactory();
var tagName = 'a-test-' + primitiveId++;
registerPrimitive(tagName, {
defaultComponents: {
scale: {x: 0.2, y: 0.2, z: 0.2}
}
});

registerComponent('create-and-update-primitive', {
init: function () {
var primitiveEl = document.createElement(tagName);
this.el.appendChild(primitiveEl);
primitiveEl.setAttribute('scale', {y: 0.2});
primitiveEl.addEventListener('loaded', function () {
assert.equal(primitiveEl.object3D.scale.x, 0.2);
done();
});
}
});

el.setAttribute('create-and-update-primitive', '');
el.sceneEl.appendChild(el);
});

test('resolves mapping collisions', function (done) {
primitiveFactory({
defaultComponents: {
Expand Down
Loading