From 7bfc0d6cb68c2339993899e113f0b56e167a704e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sat, 26 Jan 2019 18:20:49 +0100 Subject: [PATCH] call destroyed hook only once --- web/static/src/ts/core/Widget.ts | 11 +++++++++-- web/static/tests/widget.test.ts | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/web/static/src/ts/core/Widget.ts b/web/static/src/ts/core/Widget.ts index 4c90164b..fc9c7fca 100644 --- a/web/static/src/ts/core/Widget.ts +++ b/web/static/src/ts/core/Widget.ts @@ -21,6 +21,7 @@ interface Meta { vnode: VNode | null; isStarted: boolean; isMounted: boolean; + isDestroyed: boolean; parent: Widget | null; children: Widget[]; } @@ -57,6 +58,7 @@ export class Widget { vnode: null, isStarted: false, isMounted: false, + isDestroyed: false, parent: p, children: [] }; @@ -91,8 +93,13 @@ export class Widget { } destroy() { - if (this.el) { - this.el.remove(); + if (!this._.isDestroyed) { + if (this.el) { + this.el.remove(); + delete this._.vnode; + } + this._.isDestroyed = true; + this.destroyed(); } } diff --git a/web/static/tests/widget.test.ts b/web/static/tests/widget.test.ts index 74e92376..f77e874b 100644 --- a/web/static/tests/widget.test.ts +++ b/web/static/tests/widget.test.ts @@ -249,6 +249,21 @@ describe("destroy method", () => { widget.destroy(); expect(document.contains(widget.el)).toBe(false); }); + + test("destroying a widget twice only call destroyed once", async () => { + let count = 0; + class TestWidget extends Widget { + destroyed() { + count++; + } + } + const widget = new TestWidget(env); + await widget.mount(fixture); + widget.destroy(); + expect(count).toBe(1); + widget.destroy(); + expect(count).toBe(1); + }); }); describe("composition", () => {