mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] components: avoid leaks when children are outdated/destroyed
Every use case involving some sort of key set on a component would give birth to a leak in an async context: - If a key of a component changed, the outdated one was never destroyed. - destroyed component were never removed from their parent's reference map. This commit solves both issues, that are tightly linked anyway.
This commit is contained in:
committed by
Aaron Bohy
parent
38f39b6755
commit
c221721d7f
@@ -1,5 +1,5 @@
|
||||
import { App, Component, mount, status, useState, xml } from "../../src";
|
||||
import { elem, makeTestFixture, nextTick, snapshotEverything } from "../helpers";
|
||||
import { elem, makeTestFixture, nextTick, snapshotEverything, useLogLifecycle } from "../helpers";
|
||||
import { markup } from "../../src/utils";
|
||||
|
||||
let fixture: HTMLElement;
|
||||
@@ -900,4 +900,73 @@ describe("t-out in components", () => {
|
||||
"<div><b>one</b><b>one</b><b>two</b><b>two</b><b>tree</b><b>tree</b></div>"
|
||||
);
|
||||
});
|
||||
|
||||
test("component children doesn't leak (if case)", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`<div />`;
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
}
|
||||
}
|
||||
class Parent extends Component {
|
||||
static components = { Child };
|
||||
static template = xml`<Child t-if="ifVar" />`;
|
||||
ifVar = true;
|
||||
}
|
||||
|
||||
const parent = await mount(Parent, fixture);
|
||||
expect(Object.keys(parent.__owl__.children).length).toStrictEqual(1);
|
||||
expect([
|
||||
"Child:setup",
|
||||
"Child:willStart",
|
||||
"Child:willRender",
|
||||
"Child:rendered",
|
||||
"Child:mounted",
|
||||
]).toBeLogged();
|
||||
|
||||
parent.ifVar = false;
|
||||
parent.render();
|
||||
await nextTick();
|
||||
expect(Object.keys(parent.__owl__.children).length).toStrictEqual(0);
|
||||
expect(["Child:willUnmount", "Child:willDestroy"]).toBeLogged();
|
||||
});
|
||||
|
||||
test("component children doesn't leak (t-key case)", async () => {
|
||||
// This test should encompass the t-foreach and t-call cases too (because they also use a flavor of some key)
|
||||
class Child extends Component {
|
||||
static template = xml`<div />`;
|
||||
setup() {
|
||||
useLogLifecycle();
|
||||
}
|
||||
}
|
||||
class Parent extends Component {
|
||||
static components = { Child };
|
||||
static template = xml`<Child t-key="keyVar" />`;
|
||||
keyVar = 1;
|
||||
}
|
||||
|
||||
const parent = await mount(Parent, fixture);
|
||||
expect(Object.keys(parent.__owl__.children).length).toStrictEqual(1);
|
||||
expect([
|
||||
"Child:setup",
|
||||
"Child:willStart",
|
||||
"Child:willRender",
|
||||
"Child:rendered",
|
||||
"Child:mounted",
|
||||
]).toBeLogged();
|
||||
|
||||
parent.keyVar = 2;
|
||||
parent.render();
|
||||
await nextTick();
|
||||
expect(Object.keys(parent.__owl__.children).length).toStrictEqual(1);
|
||||
expect([
|
||||
"Child:setup",
|
||||
"Child:willStart",
|
||||
"Child:willRender",
|
||||
"Child:rendered",
|
||||
"Child:willUnmount",
|
||||
"Child:willDestroy",
|
||||
"Child:mounted",
|
||||
]).toBeLogged();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user