refactor: narrow graph editor api

Amp-Thread-ID: https://ampcode.com/threads/T-019f9d91-77c1-7206-a60f-ed6554ce92ab

Co-authored-by: Heaust Azure <heaust.azure@gmail.com>
This commit is contained in:
Amp
2026-07-29 02:26:44 +00:00
co-authored by heaust
parent c1a95e4b27
commit 4ff33e4079
2 changed files with 11 additions and 29 deletions
+2 -3
View File
@@ -6,7 +6,7 @@ import {
socketTypes, socketTypes,
} from "./catalog.js"; } from "./catalog.js";
export class AuthoringGraphError extends Error { class AuthoringGraphError extends Error {
constructor(code, details = {}) { constructor(code, details = {}) {
super(code); super(code);
this.name = "AuthoringGraphError"; this.name = "AuthoringGraphError";
@@ -53,7 +53,6 @@ const deepFreeze = (value) => {
return value; return value;
}; };
const sourceMaps = new WeakMap(); const sourceMaps = new WeakMap();
export const getSourceMap = (ir) => sourceMaps.get(ir);
export const mapAuthoringDiagnostic = (ir, diagnostic) => { export const mapAuthoringDiagnostic = (ir, diagnostic) => {
const details = diagnostic?.details; const details = diagnostic?.details;
const path = [ const path = [
@@ -62,7 +61,7 @@ export const mapAuthoringDiagnostic = (ir, diagnostic) => {
details?.field, details?.field,
diagnostic?.field, diagnostic?.field,
].find((value) => typeof value === "string"); ].find((value) => typeof value === "string");
const map = getSourceMap(ir); const map = sourceMaps.get(ir);
let match; let match;
if (path && map) if (path && map)
for (const key of Object.keys(map)) for (const key of Object.keys(map))
+9 -26
View File
@@ -135,34 +135,17 @@ export async function createRenderGraphEditor(canvas) {
menu = createAddNodeMenu(canvas.ownerDocument); menu = createAddNodeMenu(canvas.ownerDocument);
host.attach(root, view); host.attach(root, view);
await view.whenRendered(); await view.whenRendered();
return createEditorFacade(root, view, { requestAddNode, destroy }); const editorRoot = root,
editorView = view;
return {
getState: () => editorRoot.getState(),
onSnapshots: (fn) =>
editorRoot.onSnapshots((event) => fn(event.snapshot, event.version)),
whenRendered: () => editorView.whenRendered(),
destroy,
};
} catch (e) { } catch (e) {
await destroy().catch(() => {}); await destroy().catch(() => {});
throw e; throw e;
} }
} }
/** Creates the small application-facing wrapper around an fxnode root and view. */
export function createEditorFacade(
root,
view,
{ requestAddNode = () => null, destroy = async () => {} } = {},
) {
return {
getState: () => root.getState(),
getSaveData: () => root.getSaveData(),
load: async (data) => {
await root.load(data);
await view.whenRendered();
},
loadComposition: async (data) => {
await root.loadComposition(data);
await view.whenRendered();
},
onSnapshots: (fn) =>
root.onSnapshots((event) => fn(event.snapshot, event.version)),
requestAddNode,
whenRendered: () => view.whenRendered(),
destroy,
};
}