call destroyed hook only once

This commit is contained in:
Géry Debongnie
2019-01-26 18:20:49 +01:00
parent c3fa3042dc
commit 7bfc0d6cb6
2 changed files with 24 additions and 2 deletions
+9 -2
View File
@@ -21,6 +21,7 @@ interface Meta<T extends WEnv> {
vnode: VNode | null;
isStarted: boolean;
isMounted: boolean;
isDestroyed: boolean;
parent: Widget<T> | null;
children: Widget<T>[];
}
@@ -57,6 +58,7 @@ export class Widget<T extends WEnv> {
vnode: null,
isStarted: false,
isMounted: false,
isDestroyed: false,
parent: p,
children: []
};
@@ -91,8 +93,13 @@ export class Widget<T extends WEnv> {
}
destroy() {
if (this.el) {
this.el.remove();
if (!this._.isDestroyed) {
if (this.el) {
this.el.remove();
delete this._.vnode;
}
this._.isDestroyed = true;
this.destroyed();
}
}
+15
View File
@@ -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<WEnv> {
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", () => {