mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
call destroyed hook only once
This commit is contained in:
@@ -21,6 +21,7 @@ interface Meta<T extends WEnv> {
|
|||||||
vnode: VNode | null;
|
vnode: VNode | null;
|
||||||
isStarted: boolean;
|
isStarted: boolean;
|
||||||
isMounted: boolean;
|
isMounted: boolean;
|
||||||
|
isDestroyed: boolean;
|
||||||
parent: Widget<T> | null;
|
parent: Widget<T> | null;
|
||||||
children: Widget<T>[];
|
children: Widget<T>[];
|
||||||
}
|
}
|
||||||
@@ -57,6 +58,7 @@ export class Widget<T extends WEnv> {
|
|||||||
vnode: null,
|
vnode: null,
|
||||||
isStarted: false,
|
isStarted: false,
|
||||||
isMounted: false,
|
isMounted: false,
|
||||||
|
isDestroyed: false,
|
||||||
parent: p,
|
parent: p,
|
||||||
children: []
|
children: []
|
||||||
};
|
};
|
||||||
@@ -91,8 +93,13 @@ export class Widget<T extends WEnv> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
if (this.el) {
|
if (!this._.isDestroyed) {
|
||||||
this.el.remove();
|
if (this.el) {
|
||||||
|
this.el.remove();
|
||||||
|
delete this._.vnode;
|
||||||
|
}
|
||||||
|
this._.isDestroyed = true;
|
||||||
|
this.destroyed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -249,6 +249,21 @@ describe("destroy method", () => {
|
|||||||
widget.destroy();
|
widget.destroy();
|
||||||
expect(document.contains(widget.el)).toBe(false);
|
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", () => {
|
describe("composition", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user