Skip to content

Commit 1c924b6

Browse files
ngokevindmarcos
authored andcommitted
don't use object diff if not necessary (#1388)
1 parent 3f03424 commit 1c924b6

File tree

4 files changed

+19
-43
lines changed

4 files changed

+19
-43
lines changed

‎src/components/light.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports.Component = registerComponent('light', {
3737
*/
3838
update: function (oldData) {
3939
var data = this.data;
40-
var diffData = diff(data, oldData || {});
40+
var diffData = diff(data, oldData);
4141
var light = this.light;
4242

4343
// Existing light.

‎src/components/material.js‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
/* global Promise */
2-
var debug = require('../utils/debug');
3-
var utils = require('../utils');
2+
var utils = require('../utils/');
43
var component = require('../core/component');
54
var THREE = require('../lib/three');
65
var shader = require('../core/shader');
76

8-
var error = debug('components:material:error');
9-
var diff = utils.diff;
7+
var error = utils.debug('components:material:error');
108
var registerComponent = component.registerComponent;
119
var shaders = shader.shaders;
1210
var shaderNames = shader.shaderNames;
@@ -38,9 +36,7 @@ module.exports.Component = registerComponent('material', {
3836
*/
3937
update: function (oldData) {
4038
var data = this.data;
41-
var dataDiff = oldData ? diff(oldData, data) : data;
42-
43-
if (!this.shader || dataDiff.shader) {
39+
if (!this.shader || data.shader !== oldData.shader) {
4440
this.updateShader(data.shader);
4541
}
4642
this.shader.update(this.data);

‎src/components/sound.js‎

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var debug = require('../utils/debug');
2-
var diff = require('../utils').diff;
32
var registerComponent = require('../core/component').registerComponent;
43
var THREE = require('../lib/three');
54

@@ -24,44 +23,30 @@ module.exports.Component = registerComponent('sound', {
2423

2524
update: function (oldData) {
2625
var data = this.data;
27-
var diffData = diff(oldData || {}, data);
2826
var el = this.el;
2927
var sound = this.sound;
30-
var src = data.src;
31-
var srcChanged = 'src' in diffData;
28+
var srcChanged = data.src !== oldData.src;
3229

3330
// Create new sound if not yet created or changing `src`.
3431
if (srcChanged) {
35-
if (!src) {
32+
if (!data.src) {
3633
warn('Audio source was not specified with `src`');
3734
return;
3835
}
3936
sound = this.setupSound();
4037
}
4138

42-
if (srcChanged || 'autoplay' in diffData) {
43-
sound.autoplay = data.autoplay;
44-
}
45-
46-
if (srcChanged || 'loop' in diffData) {
47-
sound.setLoop(data.loop);
48-
}
49-
50-
if (srcChanged || 'volume' in diffData) {
51-
sound.setVolume(data.volume);
52-
}
39+
sound.autoplay = data.autoplay;
40+
sound.setLoop(data.loop);
41+
sound.setVolume(data.volume);
5342

54-
if ('on' in diffData) {
55-
if (oldData && oldData.on) {
56-
el.removeEventListener(oldData.on);
57-
}
43+
if (data.on !== oldData.on) {
44+
if (oldData.on) { el.removeEventListener(oldData.on); }
5845
el.addEventListener(data.on, this.play.bind(this));
5946
}
6047

61-
// All sound values set. Load in `src.
62-
if (srcChanged) {
63-
sound.load(src);
64-
}
48+
// All sound values set. Load in `src`.
49+
if (srcChanged) { sound.load(data.src); }
6550
},
6651

6752
remove: function () {

‎src/core/component.js‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,26 +189,27 @@ Component.prototype = {
189189
updateProperties: function (value) {
190190
var el = this.el;
191191
var isSinglePropSchema = isSingleProp(this.schema);
192-
var previousData = extendProperties({}, this.data, isSinglePropSchema);
192+
var oldData = extendProperties({}, this.data, isSinglePropSchema);
193+
193194
this.updateCachedAttrValue(value);
194195
if (this.updateSchema) {
195196
this.updateSchema(buildData(el, this.name, this.schema, this.attrValue, true));
196197
}
197198
this.data = buildData(el, this.name, this.schema, this.attrValue);
198199

199200
// Don't update if properties haven't changed
200-
if (!isSinglePropSchema && utils.deepEqual(previousData, this.data)) { return; }
201+
if (!isSinglePropSchema && utils.deepEqual(oldData, this.data)) { return; }
201202

202203
if (!this.initialized) {
203204
this.init();
204205
this.initialized = true;
205206
}
206-
this.update(previousData);
207+
this.update(oldData);
207208

208209
el.emit('componentchanged', {
209210
name: this.name,
210211
newData: this.getData(),
211-
oldData: previousData
212+
oldData: oldData
212213
});
213214
},
214215

@@ -342,12 +343,6 @@ module.exports.buildData = buildData;
342343
* @returns Overridden object or value.
343344
*/
344345
function extendProperties (dest, source, isSinglePropSchema) {
345-
if (isSinglePropSchema) {
346-
if (source === undefined || source === null ||
347-
(typeof source === 'object' && Object.keys(source).length === 0)) {
348-
return dest;
349-
}
350-
return source;
351-
}
346+
if (isSinglePropSchema) { return source; }
352347
return utils.extend(dest, source);
353348
}

0 commit comments

Comments
 (0)