Skip to content

Commit f851977

Browse files
neuqzxyskie1997
authored andcommitted
fix: fix refresh memory issue caused by event listener
1 parent c0bab85 commit f851977

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@visactor/vrender-core",
5+
"comment": "fix: fix refresh memory issue caused by event listener",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@visactor/vrender-core"
10+
}

packages/vrender-core/src/plugins/builtin-plugin/auto-refresh-plugin.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class AutoRefreshPlugin implements IPlugin {
1010
key: string = this.name + this._uid;
1111
dpr: number;
1212
rafId: number;
13+
autoRefreshCbs?: (() => void)[];
1314

1415
handleChange = (graphic: IGraphic) => {
1516
if (graphic.glyphHost) {
@@ -51,8 +52,14 @@ export class AutoRefreshPlugin implements IPlugin {
5152
this.pluginService.stage.setDpr(this.dpr, true);
5253
}
5354
};
54-
55-
matchMedia(mqString).addEventListener('change', updatePixelRatio);
55+
const dom = matchMedia(mqString);
56+
dom && dom.addEventListener('change', updatePixelRatio);
57+
if (!this.autoRefreshCbs) {
58+
this.autoRefreshCbs = [];
59+
}
60+
this.autoRefreshCbs.push(() => {
61+
dom && dom.removeEventListener('change', updatePixelRatio);
62+
});
5663
} catch (err) {
5764
return false;
5865
}
@@ -61,5 +68,9 @@ export class AutoRefreshPlugin implements IPlugin {
6168
deactivate(context: IPluginService): void {
6269
const craf = application.global.getCancelAnimationFrame();
6370
craf && this.rafId && craf(this.rafId);
71+
this.autoRefreshCbs?.forEach(cb => {
72+
cb();
73+
});
74+
this.autoRefreshCbs = undefined;
6475
}
6576
}

0 commit comments

Comments
 (0)