mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] component: tricky concurrency issue
This is a really interesting problem: there was a situation where a component would not have an event handler properly bound. The reason was that we were in a situation where the scheduler task queue was flushed at an unfortunate timing: - parent component template is rendered - subcomponent is prepared: - sub component is willStarted - it is rendered (so, fiber counter is set to 0) - scheduler task queue is flushed => we patch the DOM - the code registered in the __prepare.then(handler) is executed, which add the createHook to the vnode (but after the dom is patched) Usually, the last two steps happen in a different order, but in an environment with connected components, we sometimes flush the task queue at various moments, so some code could be executed between the end of __prepare and the registered handler. The fix is interesting: we give a callback to the __prepare function instead of registering a .then handler to the deferred. This callback is then guaranteed to be called at exactly the proper timing. Thanks seb for the hard work of finding the root cause of this issue closes #520
This commit is contained in:
@@ -1849,6 +1849,38 @@ describe("other directives with t-component", () => {
|
||||
expect(widget.n).toBe(1);
|
||||
});
|
||||
|
||||
test("t-on works, even if flush is called many times", async () => {
|
||||
let flag = false;
|
||||
let mounted = false;
|
||||
class Child extends Component<any, any> {
|
||||
static template = xml`<span>child</span>`;
|
||||
mounted() {
|
||||
mounted = true;
|
||||
}
|
||||
}
|
||||
|
||||
class Parent extends Component<any, any> {
|
||||
static template = xml`<div><Child t-on-click="doSomething"/></div>`;
|
||||
static components = { Child };
|
||||
|
||||
doSomething() {
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
|
||||
const parent = new Parent();
|
||||
parent.mount(fixture);
|
||||
while (!mounted) {
|
||||
await nextMicroTick();
|
||||
Component.scheduler.flush();
|
||||
}
|
||||
|
||||
expect(fixture.innerHTML).toBe("<div><span>child</span></div>");
|
||||
expect(flag).toBe(false);
|
||||
fixture.querySelector("span")!.click();
|
||||
expect(flag).toBe(true);
|
||||
});
|
||||
|
||||
test("t-on with handler bound to argument", async () => {
|
||||
expect.assertions(3);
|
||||
env.qweb.addTemplates(`
|
||||
|
||||
Reference in New Issue
Block a user