refactor: split tree shell page runtime
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
// MNote debug /tree shell page-mode runtime helpers.
|
||||
|
||||
export 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();
|
||||
}
|
||||
|
||||
export 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;
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user