Skip to content

Commit 3a2d8c7

Browse files
authored
feat: show warning when engaging turbo mode in development mode (#3705)
1 parent 529f971 commit 3a2d8c7

2 files changed

Lines changed: 217 additions & 0 deletions

File tree

‎packages/charts/src/vaadin-chart.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,17 @@ class Chart extends ResizeMixin(ElementMixin(ThemableMixin(PolymerElement))) {
535535
this._jsonConfigurationBuffer = null;
536536
this.__initChart(options);
537537
this.__addChildObserver();
538+
this.__checkTurboMode();
538539
});
539540
}
540541

542+
/** @protected */
543+
ready() {
544+
super.ready();
545+
546+
this.addEventListener('chart-redraw', this.__onRedraw.bind(this));
547+
}
548+
541549
/**
542550
* @return {!Options}
543551
*/
@@ -1781,6 +1789,34 @@ class Chart extends ResizeMixin(ElementMixin(ThemableMixin(PolymerElement))) {
17811789
__showWarn(propertyName, acceptedValues) {
17821790
console.warn('<vaadin-chart> Acceptable values for "' + propertyName + '" are ' + acceptedValues);
17831791
}
1792+
1793+
/** @private */
1794+
__onRedraw() {
1795+
this.__checkTurboMode();
1796+
}
1797+
1798+
/** @private */
1799+
__checkTurboMode() {
1800+
const isDevelopmentMode = !!window.Vaadin.developmentMode;
1801+
1802+
if (!this.configuration || !isDevelopmentMode || this.__turboModeWarningAlreadyLogged) {
1803+
return;
1804+
}
1805+
1806+
const exceedsTurboThreshold = this.configuration.series.some((series) => {
1807+
const threshold = (series.options && series.options.turboThreshold) || 0;
1808+
const dataLength = series.data.length;
1809+
1810+
return threshold > 0 && dataLength > threshold;
1811+
});
1812+
1813+
if (exceedsTurboThreshold) {
1814+
this.__turboModeWarningAlreadyLogged = true;
1815+
console.warn(
1816+
'<vaadin-chart> Turbo mode has been enabled for one or more series, because the number of data items exceeds the configured threshold. Turbo mode improves the performance of charts with lots of data, but is not compatible with every type of series. Please consult the documentation on compatibility, or how to disable turbo mode.'
1817+
);
1818+
}
1819+
}
17841820
}
17851821

17861822
customElements.define(Chart.is, Chart);
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import { expect } from '@esm-bundle/chai';
2+
import { fixtureSync, nextRender, oneEvent } from '@vaadin/testing-helpers';
3+
import sinon from 'sinon';
4+
import '../vaadin-chart.js';
5+
6+
describe('turbo-mode', () => {
7+
let chart;
8+
9+
beforeEach(async () => {
10+
sinon.stub(console, 'warn');
11+
});
12+
13+
afterEach(() => {
14+
console.warn.restore();
15+
});
16+
17+
function addSeries(numDataItems) {
18+
const data = Array.from(Array(numDataItems).keys());
19+
const series = fixtureSync(`<vaadin-chart-series values="[${data.join(',')}]"></vaadin-chart-series>`);
20+
chart.appendChild(series);
21+
}
22+
23+
function addSeriesWithJS(numDataItems) {
24+
const data = Array.from(Array(numDataItems).keys());
25+
chart.updateConfiguration({
26+
series: [
27+
{
28+
type: 'column',
29+
data
30+
}
31+
]
32+
});
33+
}
34+
35+
describe('when initializing chart', () => {
36+
beforeEach(async () => {
37+
chart = document.createElement('vaadin-chart');
38+
chart.updateConfiguration({
39+
plotOptions: {
40+
series: {
41+
turboThreshold: 10
42+
}
43+
}
44+
});
45+
await nextRender();
46+
});
47+
48+
describe('add series', () => {
49+
it('should not log warning when no series exceeds the turbo threshold', async () => {
50+
addSeries(9);
51+
document.body.appendChild(chart);
52+
await nextRender();
53+
expect(console.warn.called).to.be.false;
54+
});
55+
it('should log warning when initializing with a series that exceeds the turbo threshold', async () => {
56+
addSeries(11);
57+
document.body.appendChild(chart);
58+
await nextRender();
59+
expect(console.warn.calledOnce).to.be.true;
60+
expect(console.warn.args[0][0]).to.include('<vaadin-chart> Turbo mode has been enabled for one or more series');
61+
});
62+
});
63+
64+
describe('add series with JS', () => {
65+
it('should not log warning when no series exceeds the turbo threshold', async () => {
66+
addSeriesWithJS(9);
67+
document.body.appendChild(chart);
68+
await nextRender();
69+
expect(console.warn.called).to.be.false;
70+
});
71+
it('should log warning when initializing with a series that exceeds the turbo threshold', async () => {
72+
addSeriesWithJS(11);
73+
document.body.appendChild(chart);
74+
await nextRender();
75+
expect(console.warn.calledOnce).to.be.true;
76+
expect(console.warn.args[0][0]).to.include('<vaadin-chart> Turbo mode has been enabled for one or more series');
77+
});
78+
});
79+
});
80+
81+
describe('with existing chart', () => {
82+
beforeEach(async () => {
83+
chart = fixtureSync(`<vaadin-chart></vaadin-chart>`);
84+
chart.updateConfiguration({
85+
plotOptions: {
86+
series: {
87+
turboThreshold: 10
88+
}
89+
}
90+
});
91+
await nextRender();
92+
});
93+
94+
describe('adding series', () => {
95+
it('should not log warning when no series exceeds the turbo threshold', async () => {
96+
addSeries(9);
97+
await oneEvent(chart, 'chart-redraw');
98+
expect(console.warn.called).to.be.false;
99+
});
100+
101+
it('should log warning when a series exceeds the turbo threshold', async () => {
102+
addSeries(11);
103+
await oneEvent(chart, 'chart-redraw');
104+
expect(console.warn.calledOnce).to.be.true;
105+
expect(console.warn.args[0][0]).to.include('<vaadin-chart> Turbo mode has been enabled for one or more series');
106+
});
107+
});
108+
109+
describe('adding series with JS', () => {
110+
it('should not log warning when no series exceeds the turbo threshold', async () => {
111+
addSeriesWithJS(9);
112+
await oneEvent(chart, 'chart-redraw');
113+
expect(console.warn.called).to.be.false;
114+
});
115+
116+
it('should log warning when a series exceeds the turbo threshold', async () => {
117+
addSeriesWithJS(11);
118+
await oneEvent(chart, 'chart-redraw');
119+
expect(console.warn.calledOnce).to.be.true;
120+
expect(console.warn.args[0][0]).to.include('<vaadin-chart> Turbo mode has been enabled for one or more series');
121+
});
122+
});
123+
124+
describe('adding points', () => {
125+
function addPoint(seriesIndex, data) {
126+
chart.configuration.series[seriesIndex].addPoint(data);
127+
}
128+
129+
it('should not log warning when total number of items does not exceed the turbo threshold', async () => {
130+
addSeriesWithJS(4);
131+
await oneEvent(chart, 'chart-redraw');
132+
addPoint(0, 5);
133+
addPoint(0, 6);
134+
addPoint(0, 7);
135+
chart.configuration.redraw();
136+
expect(console.warn.called).to.be.false;
137+
});
138+
139+
it('should log warning when total number of items exceeds the turbo threshold', async () => {
140+
addSeriesWithJS(10);
141+
await oneEvent(chart, 'chart-redraw');
142+
expect(console.warn.called).to.be.false;
143+
144+
addPoint(0, 11);
145+
chart.configuration.redraw();
146+
expect(console.warn.calledOnce).to.be.true;
147+
expect(console.warn.args[0][0]).to.include('<vaadin-chart> Turbo mode has been enabled for one or more series');
148+
});
149+
});
150+
151+
describe('single log entry', () => {
152+
it('should only log the warning once', async () => {
153+
addSeriesWithJS(13);
154+
await oneEvent(chart, 'chart-redraw');
155+
expect(console.warn.calledOnce).to.be.true;
156+
expect(console.warn.args[0][0]).to.include('<vaadin-chart> Turbo mode has been enabled for one or more series');
157+
158+
console.warn.resetHistory();
159+
addSeriesWithJS(16);
160+
await oneEvent(chart, 'chart-redraw');
161+
expect(console.warn.called).to.be.false;
162+
});
163+
});
164+
165+
describe('production mode', () => {
166+
const originalDevelopmentMode = window.Vaadin.developmentMode;
167+
before(() => {
168+
window.Vaadin.developmentMode = false;
169+
});
170+
after(() => {
171+
window.Vaadin.developmentMode = originalDevelopmentMode;
172+
});
173+
174+
it('should not log warning in production', async () => {
175+
addSeriesWithJS(13);
176+
await oneEvent(chart, 'chart-redraw');
177+
expect(console.warn.called).to.be.false;
178+
});
179+
});
180+
});
181+
});

0 commit comments

Comments
 (0)