Skip to content

Commit decd477

Browse files
authored
fix(module:watermark): cleanup event listeners once settled (#8862)
1 parent 40d466d commit decd477

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

components/water-mark/water-mark.component.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,10 @@ export class NzWaterMarkComponent implements AfterViewInit, OnInit, OnChanges, O
261261

262262
if (this.nzImage) {
263263
const img = new Image();
264-
img.onload = () => {
264+
265+
const onLoad = (): void => {
266+
cleanup();
267+
265268
ctx.drawImage(img, drawX, drawY, drawWidth, drawHeight);
266269

267270
/** Draw interleaved pictures after rotation */
@@ -270,7 +273,10 @@ export class NzWaterMarkComponent implements AfterViewInit, OnInit, OnChanges, O
270273
ctx.drawImage(img, alternateDrawX, alternateDrawY, drawWidth, drawHeight);
271274
this.appendWatermark(canvas.toDataURL(), markWidth);
272275
};
273-
img.onerror = () =>
276+
277+
const onError = (): void => {
278+
cleanup();
279+
274280
this.drawText(
275281
canvas,
276282
ctx,
@@ -284,6 +290,16 @@ export class NzWaterMarkComponent implements AfterViewInit, OnInit, OnChanges, O
284290
alternateDrawY,
285291
markWidth
286292
);
293+
};
294+
295+
const cleanup = (): void => {
296+
img.removeEventListener('load', onLoad);
297+
img.removeEventListener('error', onError);
298+
};
299+
300+
img.addEventListener('load', onLoad);
301+
img.addEventListener('error', onError);
302+
287303
img.crossOrigin = 'anonymous';
288304
img.referrerPolicy = 'no-referrer';
289305
img.src = this.nzImage;

0 commit comments

Comments
 (0)