Skip to content

Commit 40d466d

Browse files
authored
fix(module:qrcode): remove event listeners once settled (#8861)
1 parent 472bb34 commit 40d466d

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

components/qr-code/qrcode.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ export function drawCanvas(
6161
iconImg.crossOrigin = 'anonymous';
6262
iconImg.width = iconSize * (canvas.width / size);
6363
iconImg.height = iconSize * (canvas.width / size);
64-
iconImg.onload = () => {
64+
65+
const onLoad = (): void => {
66+
cleanup();
6567
drawCanvasBackground(ctx, canvas.width, canvas.height, scale, backgroundColor);
66-
drawCanvasColor(ctx, value, scale, formattedPadding, backgroundColor, color);
68+
drawCanvasColor(ctx, value!, scale, formattedPadding, backgroundColor, color);
6769
const iconCoordinate = canvas.width / 2 - (iconSize * (canvas.width / size)) / 2;
6870

6971
ctx.fillRect(iconCoordinate, iconCoordinate, iconSize * (canvas.width / size), iconSize * (canvas.width / size));
@@ -75,10 +77,20 @@ export function drawCanvas(
7577
iconSize * (canvas.width / size)
7678
);
7779
};
78-
iconImg.onerror = () => {
80+
81+
const onError = (): void => {
82+
cleanup();
7983
drawCanvasBackground(ctx, canvas.width, canvas.height, scale, backgroundColor);
8084
drawCanvasColor(ctx, value, scale, formattedPadding, backgroundColor, color);
8185
};
86+
87+
const cleanup = (): void => {
88+
iconImg.removeEventListener('load', onLoad);
89+
iconImg.removeEventListener('error', onError);
90+
};
91+
92+
iconImg.addEventListener('load', onLoad);
93+
iconImg.addEventListener('error', onError);
8294
}
8395
}
8496

0 commit comments

Comments
 (0)