Skip to content

Commit 9ae6222

Browse files
committed
continue rebase, fix tests
1 parent 5df981b commit 9ae6222

File tree

6 files changed

+233
-260
lines changed

6 files changed

+233
-260
lines changed

‎src/core/component.js‎

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,7 @@ Component.prototype = {
258258
var key;
259259
var initialOldData;
260260
var skipTypeChecking;
261-
<<<<<<< HEAD
262-
var oldData = this.oldData;
263261
var hasComponentChanged;
264-
=======
265-
>>>>>>> object pooling in component core, optimize style parser to reuse object
266262

267263
// Just cache the attribute if the entity has not loaded
268264
// Components are not initialized until the entity has loaded
@@ -292,11 +288,8 @@ Component.prototype = {
292288
// Cache previously passed attribute to decide if we skip type checking.
293289
this.previousAttrValue = attrValue;
294290

295-
<<<<<<< HEAD
296-
// Cache current attrValue for future updates. Updates `this.attrValue`.
297-
attrValue = this.parseAttrValueForCache(attrValue);
298-
=======
299291
// Parse the attribute value.
292+
// Cache current attrValue for future updates. Updates `this.attrValue`.
300293
attrValue = this.parseAttrValueForCache(attrValue);
301294

302295
// Update previous attribute value to later decide if we skip type checking.
@@ -306,7 +299,6 @@ Component.prototype = {
306299
this.data = this.buildData(attrValue, clobber, false, skipTypeChecking);
307300

308301
// Cache current attrValue for future updates.
309-
>>>>>>> object pooling in component core, optimize style parser to reuse object
310302
this.updateCachedAttrValue(attrValue, clobber);
311303

312304
if (this.updateSchema) {
@@ -337,22 +329,6 @@ Component.prototype = {
337329
if (el.isPlaying) { this.play(); }
338330
el.emit('componentinitialized', this.evtDetail, false);
339331
} else {
340-
<<<<<<< HEAD
341-
hasComponentChanged = !utils.deepEqual(this.oldData, this.data);
342-
// Don't update if properties haven't changed.
343-
// Always update rotation, position, scale.
344-
if (!this.isPositionRotationScale && !hasComponentChanged) { return; }
345-
// Store current data as previous data for future updates.
346-
this.oldData = extendProperties({}, this.data, isSinglePropSchema);
347-
// Update component.
348-
this.update(oldData);
349-
// In the case of position, rotation, scale always update the component
350-
// but don't emit componentchanged event if component has not changed.
351-
if (!hasComponentChanged) { return; }
352-
=======
353-
// Don't update if properties haven't changed
354-
if (utils.deepEqual(this.oldData, this.data)) { return; }
355-
356332
// Store the previous old data before we calculate the new oldData.
357333
if (this.previousOldData instanceof Object) {
358334
utils.objectPool.clearObject(this.previousOldData);
@@ -363,15 +339,20 @@ Component.prototype = {
363339
this.previousOldData = this.oldData;
364340
}
365341

342+
hasComponentChanged = !utils.deepEqual(this.oldData, this.data);
343+
344+
// Don't update if properties haven't changed.
345+
// Always update rotation, position, scale.
346+
if (!this.isPositionRotationScale && !hasComponentChanged) { return; }
347+
366348
// Store current data as previous data for future updates.
367349
// Reuse `this.oldData` object to try not to allocate another one.
368350
if (this.oldData instanceof Object) { utils.objectPool.clearObject(this.oldData); }
369351
this.oldData = extendProperties(this.oldData, this.data, this.isObjectBased);
370352

371-
// Update component.
353+
// Update component with the previous old data.
372354
this.update(this.previousOldData);
373355

374-
>>>>>>> object pooling in component core, optimize style parser to reuse object
375356
this.throttledEmitComponentChanged();
376357
}
377358
},
@@ -630,28 +611,20 @@ function copyData (dest, sourceData) {
630611
* @param {boolean} isObjectBased - Whether values are objects.
631612
* @returns Overridden object or value.
632613
*/
633-
<<<<<<< HEAD
634-
function extendProperties (dest, source, isSinglePropSchema) {
635-
if (isSinglePropSchema && (source === null || typeof source !== 'object')) { return source; }
636-
var copy = utils.extend(dest, source);
637-
var key;
638-
for (key in copy) {
639-
if (!copy[key] || copy[key].constructor !== Object) { continue; }
640-
copy[key] = utils.clone(copy[key]);
641-
}
642-
return copy;
643-
=======
644614
function extendProperties (dest, source, isObjectBased) {
645615
var key;
646-
if (isObjectBased && source instanceof Object) {
616+
if (isObjectBased && source.constructor === Object) {
647617
for (key in source) {
648618
if (source[key] === undefined) { continue; }
649-
dest[key] = source[key];
619+
if (source[key] && source[key].constructor === Object) {
620+
dest[key] = utils.clone(source[key]);
621+
} else {
622+
dest[key] = source[key];
623+
}
650624
}
651625
return dest;
652626
}
653627
return source;
654-
>>>>>>> object pooling in component core, optimize style parser to reuse object
655628
}
656629

657630
/**

‎src/utils/styleParser.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Some code adapted from `style-attr` (https://github.com/joshwnj/style-attr)
44
* by Josh Johnston (MIT License).
55
*/
6+
var DASH_REGEX = /-([a-z])/g;
67

78
/**
89
* Deserialize style-like string into an object of properties.
@@ -38,7 +39,7 @@ module.exports.stringify = function (data) {
3839
* @return {string} CamelCased string.
3940
*/
4041
function toCamelCase (str) {
41-
return str.replace(/-([a-z])/g, upperCase);
42+
return str.replace(DASH_REGEX, upperCase);
4243
}
4344
module.exports.toCamelCase = toCamelCase;
4445

‎tests/core/a-entity.test.js‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,6 @@ suite('a-entity', function () {
466466

467467
suite('flushToDOM', function () {
468468
test('updates DOM attributes', function () {
469-
var el = this.el;
470469
var material;
471470
var materialStr = 'color: #F0F; metalness: 0.75';
472471
el.setAttribute('material', materialStr);
@@ -478,7 +477,6 @@ suite('a-entity', function () {
478477
});
479478

480479
test('updates DOM attributes of a multiple component', function () {
481-
var el = this.el;
482480
var soundAttrValue;
483481
var soundStr = 'src: url(mysoundfile.mp3); autoplay: true';
484482
el.setAttribute('sound__1', {'src': 'url(mysoundfile.mp3)', autoplay: true});
@@ -907,16 +905,7 @@ suite('a-entity', function () {
907905
assert.equal(el.getAttribute('material').color, '#FFF');
908906
});
909907

910-
test('does not remove default component', function () {
911-
var el = this.el;
912-
assert.ok('position' in el.components);
913-
el.removeAttribute('position');
914-
assert.shallowDeepEqual(el.getDOMAttribute('position'), {});
915-
assert.ok('position' in el.components);
916-
});
917-
918908
test('does not remove mixed-in component', function () {
919-
var el = this.el;
920909
mixinFactory('foo', {position: '1 2 3'});
921910
mixinFactory('bar', {scale: '1 2 3'});
922911
el.setAttribute('mixin', 'foo bar');
@@ -934,8 +923,6 @@ suite('a-entity', function () {
934923

935924
suite('initComponent', function () {
936925
test('initializes component', function () {
937-
var el = this.el;
938-
el.initComponent('material', false, 'color: #F0F; transparent: true');
939926
el.initComponent('material', 'color: #F0F; transparent: true', false);
940927
assert.ok(el.components.material);
941928
});
@@ -949,7 +936,6 @@ suite('a-entity', function () {
949936
});
950937

951938
test('initializes dependency component and can set attribute', function () {
952-
var el = this.el;
953939
el.initComponent('material', '', true);
954940
assert.shallowDeepEqual(el.getAttribute('material'), {});
955941
});

‎tests/core/component.test.js‎

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -289,25 +289,28 @@ suite('Component', function () {
289289
});
290290

291291
test('does not emit componentchanged for multi-prop if not changed', function (done) {
292-
el.addEventListener('componentinitialized', function (evt) {
292+
function componentChanged (evt) {
293293
if (evt.detail.name !== 'material') { return; }
294+
el.removeEventListener('componentchanged', componentChanged);
295+
// Should not reach here.
296+
assert.equal(true, false, 'Component should not have emitted changed.');
297+
}
294298

295-
el.addEventListener('componentchanged', function (evt) {
296-
if (evt.detail.name !== 'material') { return; }
297-
// Should not reach here.
298-
assert.equal(true, false, 'Component should not have emitted changed.');
299-
});
300-
301-
// Update.
299+
function componentInitialized (evt) {
300+
if (evt.detail.name !== 'material') { return; }
301+
el.addEventListener('componentchanged', componentChanged);
302302
el.setAttribute('material', 'color', 'red');
303+
}
303304

304-
// Have `done()` race with the failing assertion in the event handler.
305-
setTimeout(() => {
306-
done();
307-
}, 100);
308-
});
309-
// Initialization.
305+
el.addEventListener('componentinitialized', componentInitialized);
310306
el.setAttribute('material', 'color', 'red');
307+
308+
// Have `done()` race with the failing assertion in the event handler.
309+
setTimeout(() => {
310+
el.removeEventListener('componentchanged', componentChanged);
311+
el.removeEventListener('componentinitialized', componentInitialized);
312+
done();
313+
}, 100);
311314
});
312315

313316
test('does not update for multi-prop if not changed', function (done) {
@@ -358,25 +361,27 @@ suite('Component', function () {
358361
});
359362

360363
test('does not emit componentchanged for single-prop if not changed', function (done) {
361-
el.addEventListener('componentinitialized', function (evt) {
362-
if (evt.detail.name !== 'position') { return; }
364+
function componentInitialized (evt) {
365+
if (evt.detail.name !== 'visible') { return; }
366+
el.addEventListener('componentchanged', componentChanged);
367+
el.setAttribute('visible', false);
368+
}
363369

364-
el.addEventListener('componentchanged', function (evt) {
365-
if (evt.detail.name !== 'position') { return; }
366-
// Should not reach here.
367-
assert.equal(true, false, 'Component should not have emitted changed.');
368-
});
370+
function componentChanged (evt) {
371+
if (evt.detail.name !== 'visible') { return; }
372+
// Should not reach here.
373+
assert.equal(true, false, 'Component should not have emitted changed.');
374+
}
369375

370-
// Update.
371-
el.setAttribute('position', {x: 1, y: 2, z: 3});
376+
el.addEventListener('componentinitialized', componentInitialized);
377+
el.setAttribute('visible', false);
372378

373-
// Have `done()` race with the failing assertion in the event handler.
374-
setTimeout(() => {
375-
done();
376-
}, 100);
377-
});
378-
// Initialization.
379-
el.setAttribute('position', {x: 1, y: 2, z: 3});
379+
// Have `done()` race with the failing assertion in the event handler.
380+
setTimeout(() => {
381+
el.removeEventListener('componentinitialized', componentInitialized);
382+
el.removeEventListener('componentchanged', componentChanged);
383+
done();
384+
}, 100);
380385
});
381386

382387
test('does not emit componentchanged for value if not changed', function (done) {
@@ -437,8 +442,6 @@ suite('Component', function () {
437442
});
438443

439444
test('does not clone properties from attrValue into data that are not plain objects', function () {
440-
var attrValue;
441-
var data;
442445
var el;
443446
registerComponent('dummy', {
444447
schema: {
@@ -458,6 +461,19 @@ suite('Component', function () {
458461
var el;
459462
var data;
460463

464+
registerComponent('dummy', {
465+
schema: {
466+
color: {type: 'string'},
467+
direction: {type: 'vec3'},
468+
el: {type: 'selector', default: 'body'}
469+
}
470+
});
471+
472+
el = document.createElement('a-entity');
473+
el.hasLoaded = true;
474+
el.setAttribute('dummy', '');
475+
assert.notOk(el.components.dummy.attrValue.el);
476+
461477
// Direction property preserved across updateProperties calls but cloned into a different
462478
// object.
463479
el.components.dummy.updateProperties({
@@ -702,8 +718,8 @@ suite('Component', function () {
702718

703719
suite('updateProperties', function () {
704720
setup(function (done) {
705-
components.dummy = undefined;
706721
var el = this.el = entityFactory();
722+
components.dummy = undefined;
707723
if (el.hasLoaded) { done(); }
708724
el.addEventListener('loaded', function () { done(); });
709725
});
@@ -749,7 +765,7 @@ suite('Component', function () {
749765
el.addEventListener('loaded', function () { done(); });
750766
});
751767

752-
test('init is only called once if the init routine sets the component', function () {
768+
test('init is called once if the init routine sets the component', function () {
753769
var initCanaryStub = sinon.stub();
754770
var el = this.el;
755771
registerComponent('dummy', {
@@ -1016,15 +1032,19 @@ suite('Component', function () {
10161032
test('applies default array property types with no defined value', function (done) {
10171033
var el;
10181034
registerComponent('test', {
1019-
schema: {default: ['foo']},
1035+
schema: {
1036+
arr: {default: ['foo']}
1037+
},
10201038

10211039
update: function () {
1022-
assert.equal(this.data[0], 'foo');
1040+
assert.equal(this.data.arr[0], 'foo');
10231041
done();
10241042
}
10251043
});
10261044
el = entityFactory();
1027-
el.setAttribute('test', '');
1045+
el.addEventListener('loaded', () => {
1046+
el.setAttribute('test', '');
1047+
});
10281048
});
10291049
});
10301050

‎tests/core/controls.test.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global AFRAME, Event, assert, process, setup, suite, test */
1+
/* global Event, assert, process, setup, suite, test */
22
var helpers = require('../helpers');
33
var PI = Math.PI;
44

0 commit comments

Comments
 (0)