Skip to content

Commit 0b60f4b

Browse files
committed
split up concept of loading and rendering, to fix initial loading state
1 parent 6bf2c4c commit 0b60f4b

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/plugin.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616

1717
class ChartDatasourcePrometheusPluginInternals {
1818
loading: boolean = false;
19+
rendering: boolean = false;
1920
updateInterval: any | null = null;
2021
error: string | null = null;
2122
}
@@ -49,7 +50,9 @@ export class ChartDatasourcePrometheusPlugin {
4950
}
5051

5152
public beforeUpdate(chart: Chart, args: any, _options: any) {
52-
if (!!chart['datasource-prometheus'] && chart['datasource-prometheus'].loading == true)
53+
if (!!chart['datasource-prometheus']
54+
&& (chart['datasource-prometheus'].loading === true
55+
|| chart['datasource-prometheus'].rendering === true))
5356
return;
5457

5558
const options = Object.assign(new ChartDatasourcePrometheusPluginOptions(), _options);
@@ -84,15 +87,7 @@ export class ChartDatasourcePrometheusPlugin {
8487
// loop over queries
8588
// when we get all query results, we mix series into a single `datasets` array
8689
chart['datasource-prometheus'].loading = true;
87-
88-
if (options.loadingMsg) {
89-
this.writeText(chart, options.loadingMsg.message, (ctx) => {
90-
ctx.direction = options.loadingMsg.direction;
91-
ctx.textAlign = options.loadingMsg.textAlign;
92-
ctx.textBaseline = options.loadingMsg.textBaseline;
93-
ctx.font = options.loadingMsg.font;
94-
});
95-
}
90+
this.updateMessage(chart, _options);
9691

9792
Promise.all(reqs)
9893
.then((results) => {
@@ -149,6 +144,10 @@ export class ChartDatasourcePrometheusPlugin {
149144
}
150145

151146
public afterDraw(chart: Chart, args: any, _options: any) {
147+
this.updateMessage(chart, _options);
148+
}
149+
150+
public updateMessage(chart: Chart, _options: any) {
152151
const options = Object.assign(new ChartDatasourcePrometheusPluginOptions(), _options);
153152

154153
if (chart['datasource-prometheus'].error != null) {
@@ -158,7 +157,16 @@ export class ChartDatasourcePrometheusPlugin {
158157
ctx.textBaseline = options.errorMsg.textBaseline;
159158
ctx.font = "16px normal 'Helvetica Nueue'";
160159
});
161-
} else if (chart.data.datasets.length == 0 && chart['datasource-prometheus'].loading !== true) {
160+
} else if (chart['datasource-prometheus'].loading == true) {
161+
if (options.loadingMsg) {
162+
this.writeText(chart, options.loadingMsg.message, (ctx) => {
163+
ctx.direction = options.loadingMsg.direction;
164+
ctx.textAlign = options.loadingMsg.textAlign;
165+
ctx.textBaseline = options.loadingMsg.textBaseline;
166+
ctx.font = options.loadingMsg.font;
167+
});
168+
}
169+
} else if (chart.data.datasets.length == 0) {
162170
this.writeText(chart, options.noDataMsg.message, (ctx) => {
163171
ctx.direction = options.noDataMsg.direction;
164172
ctx.textAlign = options.noDataMsg.textAlign;
@@ -190,8 +198,9 @@ export class ChartDatasourcePrometheusPlugin {
190198
}
191199

192200
private resumeRendering(chart: Chart) {
193-
chart['datasource-prometheus'].loading = true;
194-
chart.update();
195201
chart['datasource-prometheus'].loading = false;
202+
chart['datasource-prometheus'].rendering = true;
203+
chart.update();
204+
chart['datasource-prometheus'].rendering = false;
196205
}
197206
};

0 commit comments

Comments
 (0)