Skip to content

Commit 3d53926

Browse files
committed
sync rendering
1 parent 9c40ee6 commit 3d53926

19 files changed

+292
-217
lines changed

src/runtime/component_node.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { OwlError } from "../common/owl_error";
66
import { Fiber, makeChildFiber, makeRootFiber, MountFiber, MountOptions } from "./fibers";
77
import { clearReactivesForCallback, getSubscriptions, reactive, targets } from "./reactivity";
88
import { STATUS } from "./status";
9-
import { batched, Callback } from "./utils";
9+
import { batched, Callback, possiblySync } from "./utils";
1010

1111
let currentNode: ComponentNode | null = null;
1212

@@ -128,21 +128,29 @@ export class ComponentNode<P extends Props = any, E = any> implements VNode<Comp
128128
this.initiateRender(fiber);
129129
}
130130

131-
async initiateRender(fiber: Fiber | MountFiber) {
131+
initiateRender(fiber: Fiber | MountFiber) {
132132
this.fiber = fiber;
133133
if (this.mounted.length) {
134134
fiber.root!.mounted.push(fiber);
135135
}
136136
const component = this.component;
137-
try {
138-
await Promise.all(this.willStart.map((f) => f.call(component)));
139-
} catch (e) {
140-
this.app.handleError({ node: this, error: e });
141-
return;
142-
}
143-
if (this.status === STATUS.NEW && this.fiber === fiber) {
144-
fiber.render();
145-
}
137+
return possiblySync(
138+
() => {
139+
const willStartResults = this.willStart.map((f) => f.call(component));
140+
if (willStartResults.some((r) => typeof r?.then === "function")) {
141+
return Promise.all(willStartResults);
142+
}
143+
return;
144+
},
145+
() => {
146+
if (this.status === STATUS.NEW && this.fiber === fiber) {
147+
fiber.render();
148+
}
149+
},
150+
(e: any) => {
151+
this.app.handleError({ node: this, error: e });
152+
}
153+
);
146154
}
147155

148156
async render(deep: boolean) {

src/runtime/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,20 @@ export class Markup extends String {}
8888
export function markup(value: any) {
8989
return new Markup(value);
9090
}
91+
92+
export function possiblySync(computation: Function, onSuccess: Function, onError: Function) {
93+
try {
94+
let result;
95+
try {
96+
result = computation();
97+
} catch (e) {
98+
return onError(e);
99+
}
100+
if (typeof result?.then === "function") {
101+
return result.then(onSuccess, onError);
102+
}
103+
return onSuccess(result);
104+
} catch (e) {
105+
return Promise.reject(e);
106+
}
107+
}

tests/components/__snapshots__/concurrency.test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,10 +1861,10 @@ exports[`renderings, destruction, patch, stuff, ... yet another variation 3`] =
18611861
) {
18621862
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
18631863

1864-
let block3 = createBlock(\`<p block-handler-0=\\"click\\"><block-text-1/></p>\`);
1864+
let block3 = createBlock(\`<span block-handler-0=\\"click\\"><block-text-1/></span>\`);
18651865

18661866
return function template(ctx, node, key = \\"\\") {
1867-
const b2 = text(\`D\`);
1867+
const b2 = text(\`C\`);
18681868
let hdlr1 = [ctx['increment'], ctx];
18691869
let txt1 = ctx['state'].val;
18701870
const b3 = block3([hdlr1, txt1]);
@@ -1878,10 +1878,10 @@ exports[`renderings, destruction, patch, stuff, ... yet another variation 4`] =
18781878
) {
18791879
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
18801880

1881-
let block3 = createBlock(\`<span block-handler-0=\\"click\\"><block-text-1/></span>\`);
1881+
let block3 = createBlock(\`<p block-handler-0=\\"click\\"><block-text-1/></p>\`);
18821882

18831883
return function template(ctx, node, key = \\"\\") {
1884-
const b2 = text(\`C\`);
1884+
const b2 = text(\`D\`);
18851885
let hdlr1 = [ctx['increment'], ctx];
18861886
let txt1 = ctx['state'].val;
18871887
const b3 = block3([hdlr1, txt1]);

0 commit comments

Comments
 (0)