Skip to content

Commit

Permalink
Merge pull request #3420 from easyops-cn/steve/v3-control-nodes-life-…
Browse files Browse the repository at this point in the history
…cycle

fix(): dispatch onUnmount for control nodes when root unmount
  • Loading branch information
qiaofengxi authored Aug 29, 2023
2 parents c67b211 + 81627a2 commit f6b1265
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/runtime/src/internal/Renderer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,24 @@ describe("renderBrick for control nodes", () => {

unmountTree(container);
unmountTree(portal);
rendererContext.dispatchOnUnmount();
rendererContext.dispose();

expect(consoleInfo).toBeCalledTimes(12);
expect(consoleInfo).toHaveBeenNthCalledWith(
10,
"onUnmount",
"unmount",
"a"
);
expect(consoleInfo).toHaveBeenNthCalledWith(
11,
"onUnmount",
"unmount",
"c"
);
expect(consoleInfo).toHaveBeenNthCalledWith(12, ":forEach unmount");

consoleInfo.mockReset();
});

Expand Down
11 changes: 10 additions & 1 deletion packages/runtime/src/internal/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ export async function renderBrick(
listenerFactory(
onUnmount,
runtimeContext
)(new CustomEvent("unmount"));
)(new CustomEvent("unmount", { detail: { rerender: true } }));
}

rendererContext.reRender(
Expand Down Expand Up @@ -489,6 +489,15 @@ export async function renderBrick(
});
}

if (onUnmount) {
rendererContext.registerArbitraryLifeCycle("onUnmount", () => {
listenerFactory(
onUnmount,
runtimeContext
)(new CustomEvent("unmount", { detail: { rerender: false } }));
});
}

return controlledOutput;
}

Expand Down

0 comments on commit f6b1265

Please sign in to comment.