Skip to content

Commit

Permalink
Merge pull request #4538 from easyops-cn/steve/v3-fix-rerender
Browse files Browse the repository at this point in the history
Steve/v3-fix-rerender
  • Loading branch information
qiaofengxi authored Oct 30, 2024
2 parents 4d4bc39 + 4e8953f commit 6e6758f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 27 deletions.
10 changes: 2 additions & 8 deletions packages/monaco-contributions/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ export const conf = {
{ open: '"', close: '"' },
{ open: "'", close: "'" },
],
surroundingPairs: [
{ open: "{", close: "}" },
{ open: "[", close: "]" },
{ open: "(", close: ")" },
{ open: '"', close: '"' },
{ open: "'", close: "'" },
],
folding: {
offSide: true,
},
Expand Down Expand Up @@ -312,9 +305,10 @@ export const language = {
[
/(<%[~=]?)/,
{
token: "white",
token: "delimiter",
next: "@expressionEmbedded",
nextEmbedded: "text/javascript",
bracket: "@open",
},
],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function setupTemplateProxy(

if (slotProxies && slotted) {
throw new Error(
`Can not have proxied slot ref when the parent has a slot element child, check your template "${hostBrick.type}" and ref "${ref}"`
`Can not have proxied slot ref when the ref target has a slot element child, check your template "${hostBrick.type}" and ref "${ref}"`
);
}

Expand Down
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 6e6758f

Please sign in to comment.