import handleShapeUpdate from "src/ng-mock/wasm-land/handleShapeUpdate"; import type { Connection, Diff, Scope, Shape } from "../types"; import handleShapeRequest from "src/ng-mock/wasm-land/handleShapeRequest"; import { applyDiff } from "./applyDiff"; export async function createSignalObjectForShape(shape: Shape, scope?: Scope) { const ret: { state?: any; connectionId?: Connection["id"]; update: (diff: Diff) => Promise; } = { async update(diff) { if (!ret.connectionId) throw new Error("Connection not established yet for shape" + shape); await handleShapeUpdate(ret.connectionId, diff); }, }; const onDbUpdate = (diff: Diff) => { ret.state = applyDiff(ret.state || {}, diff); }; await handleShapeRequest(shape, onDbUpdate).then( ({ connectionId, shapeObject }) => { ret.state = shapeObject; ret.connectionId = connectionId; } ); return ret; }