Skip to content

Commit

Permalink
fix(): handle data changes during postAsyncRender
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Oct 30, 2024
1 parent ef9af92 commit 4e8953f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/runtime/src/internal/Renderer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3447,7 +3447,7 @@ describe("renderBrick for tpl", () => {
{}
)
).rejects.toMatchInlineSnapshot(
`[Error: Can not have proxied slot ref when the parent has a slot element child, check your template "my.tpl-m" and ref "main"]`
`[Error: Can not have proxied slot ref when the ref target has a slot element child, check your template "my.tpl-m" and ref "main"]`
);
});
});
Expand Down
58 changes: 41 additions & 17 deletions packages/runtime/src/internal/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,26 @@ async function legacyRenderBrick(
}
};

const renderControlNode = async (
runtimeContext: RuntimeContext,
type: "initial" | "rerender"
) => {
type RenderControlNodeOptions =
| {
type: "initial";
runtimeContext: RuntimeContext;
tplStateStoreScope?: undefined;
formStateStoreScope?: undefined;
}
| {
type: "rerender";
runtimeContext: RuntimeContext;
tplStateStoreScope: DataStore<"STATE">[];
formStateStoreScope: DataStore<"FORM_STATE">[];
};

const renderControlNode = async ({
type,
runtimeContext,
tplStateStoreScope,
formStateStoreScope,
}: RenderControlNodeOptions) => {
let changedAfterInitial = false;
const tracker: InitialTracker = {
disposes: [],
Expand All @@ -531,6 +547,16 @@ async function legacyRenderBrick(
tracker,
uninitialized && type === "initial"
);

// Changes may happen during `postAsyncRender`.
if (!changedAfterInitial && type === "rerender") {
const scopedStores = [...tplStateStoreScope, ...formStateStoreScope];
await postAsyncRender(rawOutput, runtimeContext, [
runtimeContext.ctxStore,
...scopedStores,
]);
}

tracker.disposes.forEach((dispose) => dispose());
tracker.disposes.length = 0;
uninitialized = false;
Expand All @@ -549,7 +575,10 @@ async function legacyRenderBrick(
return rawOutput!;
};

const controlledOutput = await renderControlNode(runtimeContext, "initial");
const controlledOutput = await renderControlNode({
type: "initial",
runtimeContext,
});

const { onMount, onUnmount } = brickConf.lifeCycle ?? {};

Expand All @@ -561,17 +590,12 @@ async function legacyRenderBrick(
const [scopedRuntimeContext, tplStateStoreScope, formStateStoreScope] =
createScopedRuntimeContext(runtimeContext);

const reControlledOutput = await renderControlNode(
scopedRuntimeContext,
"rerender"
);

const scopedStores = [...tplStateStoreScope, ...formStateStoreScope];
await postAsyncRender(
reControlledOutput,
scopedRuntimeContext,
scopedStores
);
const reControlledOutput = await renderControlNode({
type: "rerender",
runtimeContext: scopedRuntimeContext,
tplStateStoreScope,
formStateStoreScope,
});

// Ignore stale renders
if (renderId === currentRenderId) {
Expand All @@ -596,7 +620,7 @@ async function legacyRenderBrick(
)(new CustomEvent("mount", { detail: { rerender: true } }));
}

for (const store of scopedStores) {
for (const store of [...tplStateStoreScope, ...formStateStoreScope]) {
store.mountAsyncData();
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime/src/internal/secret_internals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ describe("useBrick", () => {
});

test("tpl", async () => {
mockInternalApiGetRuntimeContext.mockReturnValue({
ctxStore: new DataStore("CTX"),
} as RuntimeContext);
consoleInfo.mockReturnValue();
customTemplates.define("my.tpl-a", {
state: [
Expand Down Expand Up @@ -472,6 +475,7 @@ describe("useBrick", () => {
unmountUseBrick(renderResult, mountResult);

consoleInfo.mockReset();
mockInternalApiGetRuntimeContext.mockReturnValue(undefined);
});

test("root as portal", async () => {
Expand Down

0 comments on commit 4e8953f

Please sign in to comment.