/** * `lazyChainProxy` — turns an async "resolve the real object" function into * a synchronously-importable stand-in that supports the SAME call shape as * the real thing, including nested member access * (`editor.document.query(...)`), by recording the property-access PATH and only * resolving + walking it down at the point of an actual function CALL. * * This is what makes `index.ts`'s top-level `export const editor = ...` work * as a plain value a script can `import { editor } from '@volter/live'` and * call immediately — each call transparently awaits the memoized `connect()` * first. * * Limitation (by design, documented on `index.ts`'s exports too): only * FUNCTION-shaped access resolves through this proxy — `editor.document.query(s)` * works, but reading a plain data property would return another inert proxy, * not the real value, since there is no function CALL to trigger resolution. * Every documented `editor` member is a method, so this never bites the * documented surface. */ type AnyFn = (...args: unknown[]) => unknown; function isFunction(value: unknown): value is AnyFn { return typeof value === 'function'; } function walk(root: unknown, path: PropertyKey[]): { thisArg: unknown; fn: unknown } { if (path.length === 0) return { thisArg: undefined, fn: root }; let obj: Record = root as Record; for (let i = 0; i < path.length - 1; i++) { const key = path[i] as PropertyKey; obj = obj[key] as Record; } const lastKey = path[path.length - 1] as PropertyKey; return { thisArg: obj, fn: obj[lastKey] }; } /** * `resolveRoot` is called (and its result awaited) on every terminal * function call reached through the returned proxy — callers typically wire * it to a memoized `connect()` (see `index.ts`), so repeated calls across * many proxy invocations still resolve only once. * * `hint` names where a member the root does NOT have lives instead * (`member-hints.ts`): consulted for the FIRST step of the path only, because * that is the member the caller named on the façade — `editor.camera.set(…)` * fails at `camera`, not at `set`. */ export function lazyChainProxy( resolveRoot: () => Promise, path: PropertyKey[] = [], hint?: (member: PropertyKey) => string | undefined, ): T { const callableTarget = (() => {}) as unknown as object; return new Proxy(callableTarget, { get(_target, prop) { // Never look thenable — `await`ing a proxy (accidentally, or via a // generic helper checking `typeof x.then`) must not trigger resolution // or hang; there is no promise here, only a call-shaped stand-in. if (prop === 'then' || prop === 'catch' || prop === 'finally') return undefined; return lazyChainProxy(resolveRoot, [...path, prop], hint); }, apply(_target, _thisArg, args) { return resolveRoot().then((root) => { const first = path[0]; const pointer = first !== undefined && hint !== undefined && !(first in (root as object)) ? hint(first) : undefined; if (pointer !== undefined) throw new TypeError(`@volter/live: editor.${String(first)} does not exist. ${pointer}`); const { thisArg, fn } = walk(root, path); if (!isFunction(fn)) { const label = path.length > 0 ? path.map(String).join('.') : '(the connected value)'; throw new TypeError(`@volter/live: ${label} is not a function on the connected session.`); } return fn.apply(thisArg, args); }); }, }) as T; }