refactor: split tree shell page runtime

This commit is contained in:
lix-2026
2026-05-26 02:42:07 +08:00
parent c5e0396eba
commit d47a7d80ee
5 changed files with 80 additions and 55 deletions
@@ -1,6 +1,11 @@
// MNote debug /tree shell 浏览器运行时外置模块。
// 该文件由 tree.rs 原 inline runtime 同构迁出,语义仍由 Rust tree_shell reducer 主导。
import {
reconcilePageRuntimeResult,
reducePageActionWithRuntime,
} from "./tree-shell-page-runtime.js";
function buildFileTreeRuntimeEnvironment(runtimeContext) {
const fileTreeRowById = runtimeContext.fileTreeRowById || new Map();
return {
@@ -17,60 +22,6 @@ function buildFileTreeRuntimeEnvironment(runtimeContext) {
};
}
async function reducePageActionWithRuntime(runtimeContext, action, item) {
if (runtimeContext.mode !== "page" || !runtimeContext.runtimeReduceEndpoint) {
return null;
}
const runtimeAction = runtimeContext.buildPageRuntimeAction(action, item);
if (!runtimeAction) return null;
const response = await fetch(runtimeContext.runtimeReduceEndpoint, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
mode: "page",
requestId: `page-runtime-${Date.now()}`,
environment: runtimeContext.buildPageRuntimeEnvironment(),
state: runtimeContext.readPageRuntimeState(action, item),
action: runtimeAction,
}),
});
if (!response.ok) {
throw new Error(await runtimeContext.readErrorMessage(response));
}
return response.json();
}
function reconcilePageRuntimeResult(runtimeContext, runtimeResult, item) {
const result = runtimeContext.normalizePageRuntimeResult(runtimeResult);
if (!result) return false;
const previousExpanded = new Set(runtimeContext.expanded);
let shouldPatchTree = false;
if (Array.isArray(result.expandedIds)) {
shouldPatchTree =
runtimeContext.commitPageExpandedIds(result.expandedIds) || shouldPatchTree;
}
const currentFocusedNodeId = runtimeContext.getFocusedNodeId();
if (result.focusedId && result.focusedId !== currentFocusedNodeId) {
runtimeContext.setFocusedNodeId(result.focusedId);
runtimeContext.postPageFocusChange(result.focusedId);
shouldPatchTree = true;
}
const itemNodeId = runtimeContext.normalizeText(item?.nodeId);
if (
itemNodeId &&
previousExpanded.has(itemNodeId) !== runtimeContext.expanded.has(itemNodeId)
) {
runtimeContext.postPageExpandChange(itemNodeId, runtimeContext.expanded.has(itemNodeId));
}
if (shouldPatchTree) {
runtimeContext.patchPageTreeAfterRuntimeState(
Array.isArray(result.expandedIds) ? [itemNodeId, ...result.expandedIds] : [itemNodeId],
result.focusedId || runtimeContext.getFocusedNodeId(),
);
}
return shouldPatchTree;
}
function startTreeShellRuntime() {
const stateElement = document.getElementById("tree-shell-state");
const appElement = document.getElementById("tree-shell-app");