Skip to content

Commit

Permalink
Fix issue when the el.hasLoaded flag can be true but not all componen…
Browse files Browse the repository at this point in the history
…ts have initialized. A init method of a component might check the flag and assume all components have initialized
  • Loading branch information
dmarcos committed Nov 23, 2023
1 parent 61f6442 commit 924dc00
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 10 deletions.
4 changes: 2 additions & 2 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) { return; }
if (!this.hasLoaded && !this.sceneEl) { 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) { return; }
if (this.isPlaying || (!this.hasLoaded && !this.sceneEl)) { return; }
this.isPlaying = true;

// Wake up all components.
Expand Down
2 changes: 1 addition & 1 deletion src/core/a-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ class ANode extends HTMLElement {
}
});

self.hasLoaded = true;
self.setupMutationObserver();
if (cb) { cb(); }
self.hasLoaded = true;
self.emit('loaded', undefined, false);
});
}
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
if (!el.hasLoaded) {
if (!el.hasLoaded && !el.sceneEl) {
this.updateCachedAttrValue(attrValue);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/components/animation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ suite('animation', function () {
setup(function (done) {
this.done = false;
el = entityFactory();
el.setAttribute('animation', '');
el.addEventListener('componentinitialized', function handler (evt) {
if (evt.detail.name !== 'animation' || this.done) { return; }
component = el.components.animation;
this.done = true;
el.removeEventListener('componentinitialized', handler);
done();
});
el.setAttribute('animation', '');
});

suite('basic animation', () => {
Expand Down
5 changes: 2 additions & 3 deletions tests/components/line.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ suite('line', function () {
setup(function (done) {
var count = 0;
el = this.el = entityFactory();
el.setAttribute('line', '');
el.setAttribute('line__suffix', '');
if (el.hasLoaded) { done(); }
el.addEventListener('componentinitialized', function (evt) {
if (evt.detail.name !== 'line' &&
evt.detail.name !== 'line__suffix') { return; }
Expand All @@ -22,6 +19,8 @@ suite('line', function () {
done();
}
});
el.setAttribute('line', '');
el.setAttribute('line__suffix', '');
});

suite('init', function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/components/vive-controls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ suite('vive-controls', function () {
controlsSystem = el.sceneEl.systems['tracked-controls-webvr'];
done();
});
el.setAttribute('vive-controls', 'hand: right; model: false'); // To ensure index = 0.
el.setAttribute('vive-controls', 'hand: right; model: true'); // To ensure index = 0.
});

suite('checkIfControllerPresent', function () {
Expand Down
23 changes: 22 additions & 1 deletion tests/core/a-entity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ suite('a-entity', function () {
}
});

test('entity has not loaded until all components have loaded', function (done) {
const parentEl = el;
const el2 = document.createElement('a-entity');
registerComponent('test', {
schema: {array: {type: 'array'}},
init: function () {
assert.notOk(this.el.hasLoaded);
this.el.addEventListener('loaded', function () {
assert.ok(el2.components.geometry);
assert.ok(el2.components.material);
assert.ok(el2.components.test);
done();
});
}
});
el2.setAttribute('geometry', 'primitive:plane');
el2.setAttribute('test', '');
el2.setAttribute('material', 'color:blue');
parentEl.appendChild(el2);
});

suite('attachedCallback', function () {
test('initializes 3D object', function (done) {
elFactory().then(el => {
Expand Down Expand Up @@ -169,7 +190,7 @@ suite('a-entity', function () {
this.sinon.spy(AEntity.prototype, 'play');

el2.addEventListener('play', function () {
assert.ok(el2.hasLoaded);
assert.ok(!el2.hasLoaded && el2.sceneEl);
sinon.assert.called(AEntity.prototype.play);
done();
});
Expand Down

0 comments on commit 924dc00

Please sign in to comment.