[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:
Lucas Perais (lpe)
2022-01-11 17:30:56 +01:00
committed by Aaron Bohy
parent 38f39b6755
commit c221721d7f
7 changed files with 411 additions and 1 deletions
+70 -1
View File
@@ -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>&lt;b&gt;one&lt;/b&gt;<b>one</b>&lt;b&gt;two&lt;/b&gt;<b>two</b>&lt;b&gt;tree&lt;/b&gt;<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();
});
});