Skip to content

Commit 7a7f632

Browse files
committed
perf(UI): Avoid unnecessary calls to controls configure (#8116)
1 parent cbd6764 commit 7a7f632

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

lib/util/config_utils.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ goog.provide('shaka.util.ConfigUtils');
88

99
goog.require('goog.asserts');
1010
goog.require('shaka.log');
11+
goog.require('shaka.util.ArrayUtils');
1112
goog.require('shaka.util.ObjectUtils');
1213

1314

@@ -189,13 +190,20 @@ shaka.util.ConfigUtils = class {
189190
// eslint-disable-next-line no-prototype-builtins
190191
if (!base.hasOwnProperty(key)) {
191192
acc[key] = value;
193+
} else if (value instanceof HTMLElement &&
194+
base[key] instanceof HTMLElement) {
195+
if (!value.isEqualNode(base[key])) {
196+
acc[key] = value;
197+
}
192198
} else if (isObject(value) && isObject(base[key])) {
193199
const diff = changes(value, base[key]);
194200
if (Object.keys(diff).length > 0 || !isObject(diff)) {
195201
acc[key] = diff;
196202
}
197-
} else if (isArrayEmpty(value) && isArrayEmpty(base[key])) {
198-
// Do nothing if both are empty arrays
203+
} else if (Array.isArray(value) && Array.isArray(base[key])) {
204+
if (!shaka.util.ArrayUtils.hasSameElements(value, base[key])) {
205+
acc[key] = value;
206+
}
199207
} else if (Number.isNaN(value) && Number.isNaN(base[key])) {
200208
// Do nothing if both are NaN
201209
} else if (value !== base[key]) {
@@ -209,7 +217,9 @@ shaka.util.ConfigUtils = class {
209217

210218
const removeEmpty = (obj) => {
211219
for (const key of Object.keys(obj)) {
212-
if (isObject(obj[key]) && Object.keys(obj[key]).length === 0) {
220+
if (obj[key] instanceof HTMLElement) {
221+
// Do nothing if it's a HTMLElement
222+
} else if (isObject(obj[key]) && Object.keys(obj[key]).length === 0) {
213223
delete obj[key];
214224
} else if (isArrayEmpty(obj[key])) {
215225
delete obj[key];

test/text/text_displayer_layout_unit.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ filterDescribe('Cue layout', shaka.test.TextLayoutTests.supported, () => {
4646
// Turn off every part of the UI that we can, so that the screenshot is
4747
// less likely to change because of something unrelated to text
4848
// rendering.
49-
ui.configure('controlPanelElements', []);
50-
ui.configure('addSeekBar', false);
51-
ui.configure('addBigPlayButton', false);
52-
ui.configure('enableFullscreenOnRotation', false);
49+
ui.configure({
50+
controlPanelElements: [],
51+
addSeekBar: false,
52+
addBigPlayButton: false,
53+
enableFullscreenOnRotation: false,
54+
});
5355

5456
// Recreate the text displayer so that the text container comes after
5557
// the controls (as it does in production). This is important for the

ui/ui.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ shaka.ui.Overlay = class {
8585
this.controls_ = new shaka.ui.Controls(
8686
player, videoContainer, video, vrCanvas, this.config_);
8787

88-
// Run the initial setup so that no configure() call is required for default
89-
// settings.
90-
this.configure({});
91-
9288
// If the browser's native controls are disabled, use UI TextDisplayer.
9389
if (!video.controls) {
9490
player.setVideoContainer(videoContainer);
@@ -166,18 +162,29 @@ shaka.ui.Overlay = class {
166162

167163
goog.asserts.assert(typeof(config) == 'object', 'Should be an object!');
168164

165+
const newConfig = /** @type {!shaka.extern.UIConfiguration} */(
166+
Object.assign({}, this.config_));
169167
shaka.util.ConfigUtils.mergeConfigObjects(
170-
this.config_, config, this.defaultConfig_(),
168+
newConfig, config, this.defaultConfig_(),
171169
/* overrides= */ {}, /* path= */ '');
172170

173171
// If a cast receiver app id has been given, add a cast button to the UI
174-
if (this.config_.castReceiverAppId &&
175-
!this.config_.overflowMenuButtons.includes('cast')) {
176-
this.config_.overflowMenuButtons.push('cast');
172+
if (newConfig.castReceiverAppId &&
173+
!newConfig.overflowMenuButtons.includes('cast')) {
174+
newConfig.overflowMenuButtons.push('cast');
177175
}
178176

179177
goog.asserts.assert(this.player_ != null, 'Should have a player!');
180178

179+
const diff = shaka.util.ConfigUtils.getDifferenceFromConfigObjects(
180+
newConfig, this.config_);
181+
if (!Object.keys(diff).length) {
182+
// No changes
183+
return;
184+
}
185+
186+
this.config_ = newConfig;
187+
181188
this.controls_.configure(this.config_);
182189

183190
this.controls_.dispatchEvent(new shaka.util.FakeEvent('uiupdated'));

0 commit comments

Comments
 (0)