Skip to content

Commit 068096d

Browse files
committed
fix stale elements bug
1 parent 519a8da commit 068096d

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

src/internal/utils/renderer-utils.ts

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ export class SVGRendererSingle {
55
public element: null | Highcharts.SVGElement = null;
66

77
public hide() {
8-
this.element?.attr({ class: undefined }).hide();
8+
if (this.element && document.contains(this.element.element)) {
9+
this.element.attr({ class: undefined }).hide();
10+
} else {
11+
this.destroy();
12+
}
913
}
1014

1115
public destroy() {
@@ -36,55 +40,60 @@ export class SVGRendererSingle {
3640
}
3741

3842
export class SVGRendererPool {
39-
private circles: Highcharts.SVGElement[] = [];
40-
private circleIndex = 0;
41-
private rects: Highcharts.SVGElement[] = [];
42-
private rectIndex = 0;
43-
private paths: Highcharts.SVGElement[] = [];
44-
private pathIndex = 0;
43+
private elements = {
44+
circle: [] as Highcharts.SVGElement[],
45+
rect: [] as Highcharts.SVGElement[],
46+
path: [] as Highcharts.SVGElement[],
47+
};
48+
private indices = { circle: 0, rect: 0, path: 0 };
4549

4650
public hideAll() {
47-
this.circles.forEach((c) => c.attr({ class: undefined }).hide());
48-
this.circleIndex = 0;
49-
this.rects.forEach((c) => c.attr({ class: undefined }).hide());
50-
this.rectIndex = 0;
51-
this.paths.forEach((c) => c.attr({ class: undefined }).hide());
52-
this.pathIndex = 0;
51+
for (const key of ["circle", "rect", "path"] as const) {
52+
this.elements[key] = this.elements[key].filter((element) => {
53+
if (!document.contains(element.element)) {
54+
element.destroy();
55+
return false;
56+
} else {
57+
element.attr({ class: undefined }).hide();
58+
return true;
59+
}
60+
});
61+
this.indices[key] = 0;
62+
}
5363
}
5464

5565
public destroyAll() {
56-
this.circles.forEach((c) => c.destroy());
57-
this.circles = [];
58-
this.rects.forEach((c) => c.destroy());
59-
this.rects = [];
60-
this.paths.forEach((c) => c.destroy());
61-
this.paths = [];
66+
for (const key of ["circle", "rect", "path"] as const) {
67+
this.elements[key].forEach((element) => element.destroy());
68+
this.elements[key] = [];
69+
this.indices[key] = 0;
70+
}
6271
}
6372

6473
public circle(rr: Highcharts.SVGRenderer, attr: Highcharts.SVGAttributes): Highcharts.SVGElement {
65-
if (this.circles[this.circleIndex]) {
66-
return this.circles[this.circleIndex++].attr(attr).show();
74+
if (this.elements.circle[this.indices.circle]) {
75+
return this.elements.circle[this.indices.circle++].attr(attr).show();
6776
}
6877
const newCircle = rr.circle().add();
69-
this.circles.push(newCircle);
78+
this.elements.circle.push(newCircle);
7079
return this.circle(rr, attr);
7180
}
7281

7382
public rect(rr: Highcharts.SVGRenderer, attr: Highcharts.SVGAttributes): Highcharts.SVGElement {
74-
if (this.rects[this.rectIndex]) {
75-
return this.rects[this.rectIndex++].attr(attr).show();
83+
if (this.elements.rect[this.indices.rect]) {
84+
return this.elements.rect[this.indices.rect++].attr(attr).show();
7685
}
7786
const newRect = rr.rect().add();
78-
this.rects.push(newRect);
87+
this.elements.rect.push(newRect);
7988
return this.rect(rr, attr);
8089
}
8190

8291
public path(rr: Highcharts.SVGRenderer, attr: Highcharts.SVGAttributes): Highcharts.SVGElement {
83-
if (this.paths[this.pathIndex]) {
84-
return this.paths[this.pathIndex++].attr(attr).show();
92+
if (this.elements.path[this.indices.path]) {
93+
return this.elements.path[this.indices.path++].attr(attr).show();
8594
}
8695
const newPath = rr.path().add();
87-
this.paths.push(newPath);
96+
this.elements.path.push(newPath);
8897
return this.path(rr, attr);
8998
}
9099
}

0 commit comments

Comments
 (0)