Skip to content

Commit adbfb9d

Browse files
authored
scalar: fix tiny chart (#2605)
This change updates `active` prop to a template of a tf-category-paginated-view when the category pane is collapsed/expanded. It also updates the `active` when a component is no longer mounted onto the DOM. The change does not contain any redraw logic but it magically works because the DataLoaderBehavior, when active changes to active, fetches data point for ones that are not in cache and then triggers rerender. This flow of logic is indirect and thus can be made to be more direct but that can cause too many "redraw" call which can be quite expensive (scales poorly with # of runs). Related to #2595.
1 parent 90f4bf9 commit adbfb9d

File tree

5 files changed

+69
-6
lines changed

5 files changed

+69
-6
lines changed

tensorboard/components/tf_paginated_view/test/categoryPaginatedViewTests.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,33 @@ namespace tf_paginated_view {
156156
expect(_getPageCount()).to.equal(5);
157157
});
158158

159+
it('sets all items to active=true when opened is true', () => {
160+
expect(getItem('id0').hasAttribute('active')).to.be.true;
161+
expect(getItem('id1').hasAttribute('active')).to.be.true;
162+
});
163+
164+
it('sets all items to active=false when opened is false', async () => {
165+
querySelector('button').click();
166+
await flushAllP();
167+
168+
expect(getItem('id0').hasAttribute('active')).to.be.false;
169+
expect(getItem('id1').hasAttribute('active')).to.be.false;
170+
});
171+
172+
it('sets item to inactive when it is out of view', async () => {
173+
// The DOM will be removed from document but it will be updated. Hold
174+
// references to them here.
175+
const item0 = getItem('id0');
176+
const item1 = getItem('id1');
177+
178+
await goNext();
179+
180+
expect(item0.hasAttribute('active')).to.be.false;
181+
expect(item1.hasAttribute('active')).to.be.false;
182+
expect(getItem('id2').hasAttribute('active')).to.be.true;
183+
expect(getItem('id3').hasAttribute('active')).to.be.true;
184+
});
185+
159186
function _getPageCount(): number {
160187
return view.$.view._pageCount;
161188
}

tensorboard/components/tf_paginated_view/test/tests.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
category="[[category]]"
3333
>
3434
<template>
35-
<span id$="[[item.id]]" number$="[[randomNumber]]">
35+
<span
36+
id$="[[item.id]]"
37+
number$="[[randomNumber]]"
38+
active$="[[active]]"
39+
>
3640
[[item.content]]
3741
</span>
3842
</template>

tensorboard/components/tf_paginated_view/tf-category-paginated-view.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@
268268
readOnly: true,
269269
},
270270

271+
_contentActive: {
272+
type: Boolean,
273+
computed: '_computeContentActive(opened)',
274+
},
275+
271276
disablePagination: {
272277
type: Boolean,
273278
value: false,
@@ -393,6 +398,10 @@
393398
this._setOpened(!this.opened);
394399
},
395400

401+
_computeContentActive() {
402+
return this.opened;
403+
},
404+
396405
_onPaneRenderedChanged(newRendered, oldRendered) {
397406
if (newRendered && newRendered !== oldRendered) {
398407
// Force dom-if render without waiting for one rAF.

tensorboard/components/tf_paginated_view/tf-dom-repeat.html

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@
4848
value: 'item',
4949
},
5050

51+
/**
52+
* Whether all stamped items are active or not.
53+
* @protected
54+
*/
55+
_contentActive: {
56+
type: Boolean,
57+
value: true,
58+
},
59+
5160
_domBootstrapped: {
5261
type: Boolean,
5362
value: false,
@@ -97,6 +106,7 @@
97106
observers: [
98107
'_bootstrapDom(_itemsRendered, isAttached)',
99108
'_updateDom(_renderedItems.*, _domBootstrapped)',
109+
'_updateActive(_contentActive)',
100110
'_trimCache(_cacheSize)',
101111
],
102112

@@ -135,7 +145,7 @@
135145
parentModel: true,
136146
instanceProps: {
137147
[this.as]: true,
138-
active: true,
148+
active: this._contentActive,
139149
},
140150
forwardHostProp: function(prop, value) {
141151
this._renderedTemplateInst.forEach((inst) => {
@@ -166,6 +176,14 @@
166176
this._domBootstrapped = true;
167177
},
168178

179+
_updateActive() {
180+
if (!this._domBootstrapped) return;
181+
182+
Array.from(this._renderedTemplateInst.values()).forEach((inst) => {
183+
inst.notifyPath('active', this._contentActive);
184+
});
185+
},
186+
169187
_updateDom(event) {
170188
if (!this._domBootstrapped) return;
171189
// These are uninteresting.
@@ -193,7 +211,7 @@
193211
if (this._renderedTemplateInst.has(key)) {
194212
this._renderedTemplateInst
195213
.get(key)
196-
.notifyPath(this.as, event.value, true /* fromAbove */);
214+
.notifyPath(this.as, event.value);
197215
} else {
198216
console.warn(
199217
`Expected '${key}' to exist in the DOM but ` +
@@ -213,8 +231,11 @@
213231
if (this._lruCachedItems.has(key)) {
214232
fragOrEl = this._lruCachedItems.get(key);
215233
this._lruCachedItems.delete(key);
234+
this._renderedTemplateInst
235+
.get(key)
236+
.notifyPath('active', this._contentActive);
216237
} else {
217-
const prop = {[this.as]: item, active: true};
238+
const prop = {[this.as]: item, active: this._contentActive};
218239
const inst = new this._ctor(prop);
219240
fragOrEl = inst.root;
220241
this._renderedTemplateInst.set(key, inst);
@@ -234,7 +255,9 @@
234255

235256
_removeItem(item, node) {
236257
Polymer.dom(node.parentNode).removeChild(node);
237-
this._lruCachedItems.set(this._getItemKey(item), node);
258+
const key = this._getItemKey(item);
259+
this._lruCachedItems.set(key, node);
260+
this._renderedTemplateInst.get(key).notifyPath('active', false);
238261
},
239262

240263
_trimCache() {

tensorboard/plugins/scalar/tf_scalar_dashboard/tf-scalar-dashboard.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ <h3>No scalar data was found.</h3>
156156
>
157157
<template>
158158
<tf-scalar-card
159-
active="[[active]"
159+
active="[[active]]"
160160
data-to-load="[[item.series]]"
161161
ignore-y-outliers="[[_ignoreYOutliers]]"
162162
multi-experiments="[[_getMultiExperiments(dataSelection)]]"

0 commit comments

Comments
 (0)