Skip to content

Commit

Permalink
sync rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
sdegueldre committed Sep 29, 2023
1 parent 9c40ee6 commit d9af7ae
Show file tree
Hide file tree
Showing 19 changed files with 288 additions and 214 deletions.
25 changes: 15 additions & 10 deletions src/runtime/component_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { OwlError } from "../common/owl_error";
import { Fiber, makeChildFiber, makeRootFiber, MountFiber, MountOptions } from "./fibers";
import { clearReactivesForCallback, getSubscriptions, reactive, targets } from "./reactivity";
import { STATUS } from "./status";
import { batched, Callback } from "./utils";
import { batched, Callback, possiblySync } from "./utils";

let currentNode: ComponentNode | null = null;

Expand Down Expand Up @@ -128,21 +128,26 @@ export class ComponentNode<P extends Props = any, E = any> implements VNode<Comp
this.initiateRender(fiber);
}

async initiateRender(fiber: Fiber | MountFiber) {
initiateRender(fiber: Fiber | MountFiber) {
this.fiber = fiber;
if (this.mounted.length) {
fiber.root!.mounted.push(fiber);
}
const component = this.component;
try {
await Promise.all(this.willStart.map((f) => f.call(component)));
} catch (e) {
this.app.handleError({ node: this, error: e });
const onSuccess = () => {
if (this.status === STATUS.NEW && this.fiber === fiber) {
fiber.render();
}
};
return possiblySync(() => {
const willStartResults = this.willStart.map((f) => f.call(component));
if (willStartResults.some(r => typeof r?.then === "function")) {
return Promise.all(willStartResults);
}
return;
}
if (this.status === STATUS.NEW && this.fiber === fiber) {
fiber.render();
}
}, onSuccess, (e: any) => {
this.app.handleError({ node: this, error: e })
});
}

async render(deep: boolean) {
Expand Down
17 changes: 17 additions & 0 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,20 @@ export class Markup extends String {}
export function markup(value: any) {
return new Markup(value);
}

export function possiblySync(computation: Function, onSuccess: Function, onError: Function) {
try {
let result;
try {
result = computation();
} catch (e) {
return onError(e);
}
if (typeof result?.then === "function") {
return result.then(onSuccess, onError);
}
return onSuccess(result);
} catch (e) {
return Promise.reject(e);
}
}
8 changes: 4 additions & 4 deletions tests/components/__snapshots__/concurrency.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1861,10 +1861,10 @@ exports[`renderings, destruction, patch, stuff, ... yet another variation 3`] =
) {
let { text, createBlock, list, multi, html, toggler, comment } = bdom;

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

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

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

return function template(ctx, node, key = \\"\\") {
const b2 = text(\`C\`);
const b2 = text(\`D\`);
let hdlr1 = [ctx['increment'], ctx];
let txt1 = ctx['state'].val;
const b3 = block3([hdlr1, txt1]);
Expand Down
Loading

0 comments on commit d9af7ae

Please sign in to comment.