Skip to content

Commit

Permalink
fix: fix issue that cause memory leak (#5587)
Browse files Browse the repository at this point in the history
* fix: fix issue that may cause memories lack

* test: adjust test case to destroy graph
  • Loading branch information
Aarebecca authored Mar 29, 2024
1 parent 72c5e74 commit bcd0804
Show file tree
Hide file tree
Showing 29 changed files with 143 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('behavior combo expand collapse', () => {
graph = await createDemoGraph(comboExpandCollapse, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('default status', async () => {
await expect(graph).toMatchSnapshot(__filename);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe('behavior drag canvas', () => {
graph = await createDemoGraph(behaviorDragCanvas, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('default status', () => {
expect(graph.getBehaviors()).toEqual([
'drag-canvas',
Expand Down Expand Up @@ -78,8 +82,4 @@ describe('behavior drag canvas', () => {

await expect(graph).toMatchSnapshot(__filename);
});

it('destroy', () => {
graph.destroy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('behavior drag combo', () => {
graph = await createDemoGraph(comboExpandCollapse, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('default status', async () => {
graph.setBehaviors([{ type: 'drag-element', dropEffect: 'link' }]);
graph.expand('combo-1');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ describe('behavior drag element', () => {
graph = await createDemoGraph(behaviorDragNode, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('default status', async () => {
await expect(graph).toMatchSnapshot(__filename);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe('behavior zoom canvas', () => {
graph = await createDemoGraph(behaviorZoomCanvas, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('default status', () => {
expect(graph.getZoom()).toBe(1);
expect(graph.getBehaviors()).toEqual([{ type: 'zoom-canvas' }]);
Expand Down Expand Up @@ -191,8 +195,4 @@ describe('behavior zoom canvas', () => {
container?.dispatchEvent(new Event(ContainerEvent.KEY_DOWN));
expect(keydownListener).toHaveBeenCalledTimes(1);
});

it('destroy', () => {
graph.destroy();
});
});
4 changes: 4 additions & 0 deletions packages/g6/__tests__/unit/elements/change-type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('element change type', () => {
graph = await createDemoGraph(elementChangeType, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('default status', async () => {
await expect(graph).toMatchSnapshot(__filename);
});
Expand Down
4 changes: 4 additions & 0 deletions packages/g6/__tests__/unit/elements/combo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('combo', () => {
graph = await createDemoGraph(combo, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('default status', async () => {
await expect(graph).toMatchSnapshot(__filename);
});
Expand Down
4 changes: 4 additions & 0 deletions packages/g6/__tests__/unit/elements/edge-polyline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ describe('edge polyline', () => {
graph = await createDemoGraph(edgePolyline, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('Control Points', async () => {
updateEdgeStyle(graph, 'edge-1', 'controlPoints', [[300, 190]]);

Expand Down
4 changes: 4 additions & 0 deletions packages/g6/__tests__/unit/elements/port.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ describe('element port', () => {
graph = await createDemoGraph(elementPort, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('default status', async () => {
await expect(graph).toMatchSnapshot(__filename, 'port_hidden');
});
Expand Down
4 changes: 4 additions & 0 deletions packages/g6/__tests__/unit/elements/position-combo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('element position combo', () => {
graph = await createDemoGraph(elementPositionCombo, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('default status', async () => {
await expect(graph).toMatchSnapshot(__filename);
});
Expand Down
4 changes: 4 additions & 0 deletions packages/g6/__tests__/unit/elements/position.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('element position', () => {
graph = await createDemoGraph(elementPosition, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('default status', async () => {
await expect(graph).toMatchSnapshot(__filename);
});
Expand Down
4 changes: 4 additions & 0 deletions packages/g6/__tests__/unit/elements/state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('element state', () => {
graph = await createDemoGraph(elementState, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('default status', async () => {
await expect(graph).toMatchSnapshot(__filename);
});
Expand Down
4 changes: 4 additions & 0 deletions packages/g6/__tests__/unit/elements/visibility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('element visibility', () => {
graph = await createDemoGraph(elementVisibility, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('default status', async () => {
await expect(graph).toMatchSnapshot(__filename);
});
Expand Down
4 changes: 4 additions & 0 deletions packages/g6/__tests__/unit/elements/z-index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('element zIndex', () => {
graph = await createDemoGraph(elementZIndex, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('default status', async () => {
await expect(graph).toMatchSnapshot(__filename);
});
Expand Down
4 changes: 4 additions & 0 deletions packages/g6/__tests__/unit/plugins/plugin-grid-line.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ describe('plugin grid line', () => {
.getElementsByClassName('g6-grid-line')! as HTMLCollectionOf<HTMLElement>;
});

afterAll(() => {
graph.destroy();
});

it('default status', () => {
expect(graph.getPlugins()).toEqual([{ type: 'grid-line', follow: false }]);
expect(gridLineElement.length).toBe(1);
Expand Down
5 changes: 5 additions & 0 deletions packages/g6/__tests__/unit/runtime/element.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import { omit } from '@antv/util';

describe('ElementController', () => {
let graph: Graph;

beforeAll(async () => {
graph = await createDemoGraph(graphElement);
});

afterAll(() => {
graph.destroy();
});

it('static', async () => {
await expect(graph).toMatchSnapshot(__filename);

Expand Down
2 changes: 1 addition & 1 deletion packages/g6/__tests__/unit/runtime/graph/graph.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ describe('Graph', () => {
it('destroy', () => {
graph.destroy();
// @ts-expect-error context is private.
expect(graph.context).toBeUndefined();
expect(graph.context).toEqual({});
expect(graph.destroyed).toBe(true);
});
});
35 changes: 23 additions & 12 deletions packages/g6/__tests__/unit/runtime/viewport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import { AABB } from '@antv/g';

describe('ViewportController', () => {
let graph: Graph;

beforeAll(async () => {
graph = await createDemoGraph(controllerViewport);
});

afterAll(() => {
graph.destroy();
});

it('viewport center', () => {
expect(graph.getViewportCenter()).toBeCloseTo([250, 250, 0]);
});
Expand Down Expand Up @@ -104,18 +109,19 @@ describe('ViewportController', () => {
// @ts-expect-error
expect(graph.context.viewport.getBBoxInViewport(bbox).halfExtents).toBeCloseTo([100, 100, 0]);
});

afterAll(() => {
graph.destroy();
});
});

describe('Viewport Fit without Animation', () => {
let graph: Graph;

beforeAll(async () => {
graph = await createDemoGraph(viewportFit);
});

afterAll(() => {
graph.destroy();
});

it('default', async () => {
await expect(graph).toMatchSnapshot(__filename, 'before-fit');
});
Expand Down Expand Up @@ -144,18 +150,19 @@ describe('Viewport Fit without Animation', () => {
await graph.fitCenter();
await expect(graph).toMatchSnapshot(__filename, 're-fitCenter');
});

afterAll(() => {
graph.destroy();
});
});

describe('Viewport Fit with Animation', () => {
let graph: Graph;

beforeAll(async () => {
graph = await createDemoGraph(viewportFit, { animation: true });
});

afterAll(() => {
graph.destroy();
});

it('default', async () => {
await expect(graph).toMatchSnapshot(__filename, 'before-fit-animation');
});
Expand Down Expand Up @@ -184,10 +191,6 @@ describe('Viewport Fit with Animation', () => {
await graph.fitCenter();
await expect(graph).toMatchSnapshot(__filename, 're-fitCenter-animation');
});

afterAll(() => {
graph.destroy();
});
});

describe('Viewport Fit with AutoFit and Padding without Animation', () => {
Expand All @@ -199,6 +202,10 @@ describe('Viewport Fit with AutoFit and Padding without Animation', () => {
});
});

afterAll(() => {
graph.destroy();
});

it('default', async () => {
await expect(graph).toMatchSnapshot(__filename, 'auto-fit-with-padding');
});
Expand All @@ -214,6 +221,10 @@ describe('Viewport Fit with AutoFit and Padding with Animation', () => {
});
});

afterAll(() => {
graph.destroy();
});

it('default', async () => {
await expect(graph).toMatchSnapshot(__filename, 'auto-fit-with-padding-animation');
});
Expand Down
4 changes: 4 additions & 0 deletions packages/g6/__tests__/unit/spec/theme.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe('spec theme', () => {
graph = await createDemoGraph(theme, { animation: false });
});

afterAll(() => {
graph.destroy();
});

it('theme', async () => {
const theme: ThemeOptions = 'light';

Expand Down
1 change: 1 addition & 0 deletions packages/g6/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"dev": "vite",
"fix": "eslint ./src ./__tests__ --fix && prettier ./src __tests__ --write ",
"jest": "node --expose-gc --max-old-space-size=4096 --unhandled-rejections=strict --experimental-vm-modules ../../node_modules/jest/bin/jest --coverage --logHeapUsage --detectOpenHandles",
"jest:inspect": "node --inspect --expose-gc --max-old-space-size=4096 --unhandled-rejections=strict --experimental-vm-modules ../../node_modules/jest/bin/jest --coverage --logHeapUsage --detectOpenHandles --runInBand",
"lint": "eslint ./src __tests__ --quiet && prettier ./src __tests__ --check",
"prepublishOnly": "npm run ci",
"size": "limit-size",
Expand Down
6 changes: 6 additions & 0 deletions packages/g6/src/elements/shapes/base-shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,10 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
const { visibility } = this.attributes;
setVisibility(this, visibility);
}

public destroy(): void {
this.shapeMap = {};
this.animateMap = {};
super.destroy();
}
}
6 changes: 3 additions & 3 deletions packages/g6/src/plugins/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
}

public update(options: Partial<TooltipOptions>) {
this.unbundEvents();
this.unbindEvents();
super.update(options);
if (this.tooltipElement) {
this.container?.removeChild(this.tooltipElement.HTMLTooltipElement);
Expand All @@ -83,7 +83,7 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
this.tooltipElement = this.initTooltip();
}

private unbundEvents() {
private unbindEvents() {
const { graph } = this.context;
/** The previous event binding needs to be removed when updating the trigger. */
const events = this.getEvents();
Expand Down Expand Up @@ -259,7 +259,7 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
};

public destroy(): void {
this.unbundEvents();
this.unbindEvents();
if (this.tooltipElement) {
this.container?.removeChild(this.tooltipElement.HTMLTooltipElement);
}
Expand Down
12 changes: 5 additions & 7 deletions packages/g6/src/registry/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ export abstract class ExtensionController<Extension extends BaseExtension<Loosel
public destroy() {
Object.values(this.extensionMap).forEach((extension) => extension.destroy());
// @ts-expect-error force delete
delete this.context;
// @ts-expect-error force delete
delete this.extensions;
// @ts-expect-error force delete
delete this.extensionMap;
this.context = {};
this.extensions = [];
this.extensionMap = {};
}
}

Expand Down Expand Up @@ -105,9 +103,9 @@ export class BaseExtension<T extends LooselyExtensionOption> {

public destroy() {
// @ts-expect-error force delete
delete this.context;
this.context = {};
// @ts-expect-error force delete
delete this.options;
this.options = {};

this.destroyed = true;
}
Expand Down
Loading

0 comments on commit bcd0804

Please sign in to comment.