Skip to content

Commit 4050d0d

Browse files
committed
optimize component change/initialize events (fixes #2950)
1 parent feb8d1a commit 4050d0d

File tree

2 files changed

+13
-34
lines changed

2 files changed

+13
-34
lines changed

src/core/component.js

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,17 @@ var Component = module.exports.Component = function (el, attrValue, id) {
3535
this.el = el;
3636
this.id = id;
3737
this.attrName = this.name + (id ? '__' + id : '');
38+
this.evtDetail = {id: this.id, name: this.name};
3839
this.initialized = false;
3940
this.el.components[this.attrName] = this;
41+
4042
// Store component data from previous update call.
4143
this.oldData = undefined;
44+
4245
// Last value passed to updateProperties.
4346
this.previousAttrValue = undefined;
44-
this.throttledEmitComponentChanged = utils.throttle(function emitComponentChange (oldData) {
45-
el.emit('componentchanged', {
46-
id: self.id,
47-
name: self.name,
48-
newData: self.data,
49-
oldData: oldData
50-
}, false);
47+
this.throttledEmitComponentChanged = utils.throttle(function emitChange () {
48+
el.emit('componentchanged', self.evtDetail, false);
5149
}, 200);
5250
this.updateProperties(attrValue);
5351
};
@@ -145,17 +143,6 @@ Component.prototype = {
145143
return styleParser.stringify(data);
146144
},
147145

148-
/**
149-
* Returns a copy of data such that we don't expose the private this.data.
150-
*
151-
* @returns {object} data
152-
*/
153-
getData: function () {
154-
var data = this.data;
155-
if (typeof data !== 'object') { return data; }
156-
return utils.extend({}, data);
157-
},
158-
159146
/**
160147
* Update the cache of the pre-parsed attribute value.
161148
*
@@ -272,20 +259,15 @@ Component.prototype = {
272259
this.update(oldData);
273260
// Play the component if the entity is playing.
274261
if (el.isPlaying) { this.play(); }
275-
el.emit('componentinitialized', {
276-
id: this.id,
277-
name: this.name,
278-
data: this.getData()
279-
}, false);
262+
el.emit('componentinitialized', this.evtDetail, false);
280263
} else {
281264
// Don't update if properties haven't changed
282265
if (utils.deepEqual(this.oldData, this.data)) { return; }
283266
// Store current data as previous data for future updates.
284267
this.oldData = extendProperties({}, this.data, isSinglePropSchema);
285268
// Update component.
286269
this.update(oldData);
287-
// Limit event to fire once every 200ms.
288-
this.throttledEmitComponentChanged(oldData);
270+
this.throttledEmitComponentChanged();
289271
}
290272
},
291273

tests/core/component.test.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,8 @@ suite('Component', function () {
198198
el.setAttribute('material', 'color: red');
199199
el.addEventListener('componentchanged', function (evt) {
200200
if (evt.detail.name !== 'material') { return; }
201-
assert.equal(evt.detail.oldData.color, 'red');
202-
assert.equal(evt.detail.newData.color, 'blue');
203201
assert.equal(evt.detail.name, 'material');
202+
assert.equal(el.getAttribute('material').color, 'blue');
204203
assert.ok('id' in evt.detail);
205204
done();
206205
});
@@ -213,8 +212,7 @@ suite('Component', function () {
213212
el.setAttribute('position', {x: 0, y: 0, z: 0});
214213
el.addEventListener('componentchanged', function (evt) {
215214
if (evt.detail.name !== 'position') { return; }
216-
assert.shallowDeepEqual(evt.detail.oldData, {x: 0, y: 0, z: 0});
217-
assert.shallowDeepEqual(evt.detail.newData, {x: 1, y: 2, z: 3});
215+
assert.shallowDeepEqual(el.getAttribute('position'), {x: 1, y: 2, z: 3});
218216
assert.equal(evt.detail.name, 'position');
219217
assert.ok('id' in evt.detail);
220218
done();
@@ -227,8 +225,7 @@ suite('Component', function () {
227225
test('emits componentchanged for value', function (done) {
228226
el.addEventListener('componentchanged', function (evt) {
229227
if (evt.detail.name !== 'visible') { return; }
230-
assert.shallowDeepEqual(evt.detail.oldData, true);
231-
assert.shallowDeepEqual(evt.detail.newData, false);
228+
assert.equal(el.getAttribute('visible'), false);
232229
assert.equal(evt.detail.name, 'visible');
233230
done();
234231
});
@@ -353,7 +350,6 @@ suite('Component', function () {
353350
test('emits componentinitialized', function (done) {
354351
el.addEventListener('componentinitialized', function (evt) {
355352
if (evt.detail.name !== 'material') { return; }
356-
assert.ok(evt.detail.data);
357353
assert.ok('id' in evt.detail);
358354
assert.equal(evt.detail.name, 'material');
359355
done();
@@ -652,8 +648,9 @@ suite('Component', function () {
652648
schema: {color: {default: 'red'}},
653649
update: function () { this.el.setAttribute('dummy', 'color', 'blue'); }
654650
});
655-
this.el.addEventListener('componentchanged', function (evt) {
656-
assert.equal('blue', evt.detail.newData.color);
651+
this.el.addEventListener('componentchanged', evt => {
652+
assert.equal(evt.detail.name, 'dummy');
653+
assert.equal(this.el.getAttribute('dummy').color, 'blue');
657654
done();
658655
});
659656
var component = new TestComponent(this.el);

0 commit comments

Comments
 (0)