mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] runtime, compiler: fix refs getting set or unset incorrectly
Previously, refs could get set to null incorrectly, or could stay set when they shouldn't. This was caused by the fact that singleRefSetter and multiRefSetters are created on every render, meaning that any render that did not affect the status of the ref (mounted or not), would overwrite the previous ref setter, including its closure, causing the captured `_el` or `count` to be lost. This means that on a subsequent render that did affect the status of the ref, the singleRefSetter would consider that it did not set the ref, and so shouldn't unset it, causing it to incorrectly stay keep the element, while in multiRefSetter, the opposite problem occured: the count would be 0 on removal, causing it to believe that it was the first call in the corresponding patch that affected the ref, when in fact, the closure is already stale, because it was created from the previous render, and the fresh multiRefSetter from the latest render may already have been called by an element with that ref that came earlier in the template. This commit changes the ref setting strategy to work around the issue of stale closures: we define a setRef method on ComponentNode that is always called, this method ignores calls with `null`, meaning that the refs object on the ComponentNode never reverts to a null value for any key. Instead, the useRef hook will check whether the element that the ref points to is still mounted, and return null if not.
This commit is contained in:
committed by
Géry Debongnie
parent
7ac81ed5fe
commit
975ed32f0b
@@ -90,6 +90,28 @@ describe("refs", () => {
|
||||
expect(test.ref.el!.tagName).toBe("DIV");
|
||||
});
|
||||
|
||||
test("ref is unset when t-if goes to false after unrelated render", async () => {
|
||||
class Comp extends Component {
|
||||
static template = xml`<div t-if="state.show" t-att-class="state.class" t-ref="coucou"/>`;
|
||||
state = useState({ show: true, class: "test" });
|
||||
ref = useRef("coucou");
|
||||
}
|
||||
|
||||
const comp = await mount(Comp, fixture);
|
||||
expect(comp.ref.el).not.toBeNull();
|
||||
|
||||
comp.state.class = "test2";
|
||||
await nextTick();
|
||||
|
||||
comp.state.show = false;
|
||||
await nextTick();
|
||||
expect(comp!.ref.el).toBeNull();
|
||||
|
||||
comp.state.show = true;
|
||||
await nextTick();
|
||||
expect(comp!.ref.el).not.toBeNull();
|
||||
});
|
||||
|
||||
test("throws if there are 2 same refs at the same time", async () => {
|
||||
const consoleWarn = console.warn;
|
||||
console.warn = jest.fn();
|
||||
@@ -104,10 +126,10 @@ describe("refs", () => {
|
||||
|
||||
const app = new App(Test, { test: true });
|
||||
const mountProm = expect(app.mount(fixture)).rejects.toThrowError(
|
||||
"Cannot have 2 elements with same ref name at the same time"
|
||||
'Cannot set the same ref more than once in the same component, ref "coucou" was set multiple times in Test'
|
||||
);
|
||||
await expect(nextAppError(app)).resolves.toThrow(
|
||||
"Cannot have 2 elements with same ref name at the same time"
|
||||
'Cannot set the same ref more than once in the same component, ref "coucou" was set multiple times in Test'
|
||||
);
|
||||
await mountProm;
|
||||
expect(console.warn).toBeCalledTimes(1);
|
||||
|
||||
Reference in New Issue
Block a user