/** * NAMES AN AGENT REACHES FOR THAT THE FAÇADE DOES NOT HAVE, and where the * thing it wanted actually lives. * * A call to `editor.setCamera(...)` would otherwise fail with the engine's bare * "editor.setCamera is not a function", though the camera is reachable through * `editor.present`'s `viewport.camera`. The refusal is the one moment the * caller is certainly reading, so that is where the pointer goes. * * TARGETED, NOT A CATCH-ALL. Only the names below are intercepted, each with * the door it was looking for. Every other missing member keeps JavaScript's * own behaviour (`undefined`, then the engine's TypeError), so this table can * never turn a typo into a wrong suggestion — it only ever adds what is known. */ const CAMERA_HINT = 'The viewport camera is posed through a presented view: ' + 'await editor.present({ version: 1, viewport: { camera: { position: { x, y, z }, target: { x, y, z }, fov } } }) ' + '(stage space: metres, Y up), ' + 'and read back with (await editor.currentView()).viewport.camera. ' + 'editor.frame() and editor.orbit() frame or swing around the subject instead.'; /** Missing `editor` member → what to use instead. */ const EDITOR_MEMBER_HINTS: Readonly> = { camera: CAMERA_HINT, setCamera: CAMERA_HINT, getCamera: CAMERA_HINT, cameraPose: CAMERA_HINT, setCameraPose: CAMERA_HINT, moveCamera: CAMERA_HINT, poseCamera: CAMERA_HINT, lookAt: CAMERA_HINT, }; /** The pointer for a member `editor` does not have, or undefined when there is none to give. */ export function editorMemberHint(member: PropertyKey): string | undefined { return typeof member === 'string' && Object.hasOwn(EDITOR_MEMBER_HINTS, member) ? EDITOR_MEMBER_HINTS[member] : undefined; } /** * `editor` as `eval` binds it: the same object, except that reading one of the * hinted names it does NOT have throws the hint, naming `editor.member`. * Thrown on READ, not on call, so `editor.camera.position` is caught as well as * `editor.setCamera(...)`. * * Methods come back bound to the real object: `LiveEditor` keeps its client in * a `#`-private field, which a call whose `this` is the proxy cannot read. */ export function withEditorMemberHints(target: T): T { return new Proxy(target, { get(object, key) { if (!(key in object)) { const hint = editorMemberHint(key); if (hint !== undefined) throw new TypeError(`editor.${String(key)} does not exist. ${hint}`); } const value: unknown = Reflect.get(object, key, object); return typeof value === 'function' ? (value as (...args: unknown[]) => unknown).bind(object) : value; }, }); }