import { CODE_APP_TOOLS } from "./bridgeTools"; export const CODE_APP_CALLS = [ "databases", "describe", "rows", "totals", "addRecord", "editRecord", "editRecords", "deleteRecord", ...CODE_APP_TOOLS, ] as const; export type BridgeCall = typeof CODE_APP_CALLS[number]; export type BridgeError = { code: string; message: string }; export type BridgeRequest = { id: string | number; call: BridgeCall; args: unknown[] }; export type BridgeAnswer = { id: string | number } & ( | { ok: true; result: unknown } | { ok: false; error: BridgeError } ); export type Database = { key: string; label: string }; export type BridgeField = { key: string; label?: string; type?: string }; export type DatabaseDescription = { key: string; fields: BridgeField[]; identity: { pid: string }; recordsMutable: boolean; }; export type RowOptions = { filters?: unknown[]; sorts?: unknown[]; fields?: string[]; limit?: number; cursor?: string | null; }; export type RowPage = { fields: BridgeField[]; rows: Record[]; total: number; nextCursor: string | null; limits: unknown[]; }; export type Aggregate = ({ colId: string; agg: string } | { field: string; fn: string }) & { p?: number }; export type TotalOptions = { aggregates?: Aggregate[]; groupBy?: string | string[]; filters?: unknown[] }; export type Totals = { total_rows: number; aggregates: Record; groups: unknown[] | null; total_groups?: number; limits: unknown[]; }; export type AddedRecord = { rid: string; pid: number }; export type EditedRecord = { pid: number; updates: Record }; export interface Loopable { databases(): Promise; describe(database: string): Promise; rows(database: string, options?: RowOptions): Promise; totals(database: string, options?: TotalOptions): Promise; addRecord(database: string, values: Record): Promise; editRecord(database: string, id: string | number, values: Record): Promise; editRecords(database: string, records: { id: string | number; values: Record }[]): Promise; deleteRecord(database: string, id: string | number): Promise<{ ok: boolean }>; tools: { listBrainFiles(): Promise; readBrainFile(path: string): Promise; sendRequest(request: { to: string; title: string; body: string }): Promise; createSchedule(schedule: { database: string; payload: unknown; when: string }): Promise; }; } // This function is serialized into the child document and must have no module dependencies. function installLoopable() { const host = window.parent.parent; const pending = new Map void; reject: (error: Error) => void }>(); let sequence = 0; const invoke = (call: string, args: unknown[]): Promise => new Promise((resolve, reject) => { const id = `loopable:${++sequence}`; pending.set(id, { resolve, reject }); try { host.postMessage({ id, call, args }, "*"); } catch { pending.delete(id); reject(Object.assign(new Error("This request could not be sent."), { code: "bridge_unavailable" })); } }); window.addEventListener("message", (event) => { if (event.source !== host) return; const answer = event.data; if (!answer || typeof answer !== "object" || typeof answer.id !== "string" || typeof answer.ok !== "boolean") return; const request = pending.get(answer.id); if (!request) return; if (answer.ok && Object.hasOwn(answer, "result")) { pending.delete(answer.id); request.resolve(answer.result); } else if (!answer.ok && typeof answer.error?.code === "string" && typeof answer.error?.message === "string") { pending.delete(answer.id); request.reject(Object.assign(new Error(answer.error.message), { code: answer.error.code })); } }); window.addEventListener("pagehide", () => { for (const request of pending.values()) { request.reject(Object.assign(new Error("This App was closed."), { code: "bridge_closed" })); } pending.clear(); }); const loopable = { databases: () => invoke("databases", []), describe: (database: string) => invoke("describe", [database]), rows: (database: string, options: RowOptions = {}) => invoke("rows", [database, options]), totals: (database: string, options: TotalOptions = {}) => invoke("totals", [database, options]), addRecord: (database: string, values: unknown) => invoke("addRecord", [database, values]), editRecord: (database: string, id: unknown, values: unknown) => invoke("editRecord", [database, id, values]), editRecords: (database: string, records: unknown) => invoke("editRecords", [database, records]), deleteRecord: (database: string, id: unknown) => invoke("deleteRecord", [database, id]), tools: Object.freeze({ listBrainFiles: () => invoke("tools.listBrainFiles", []), readBrainFile: (path: string) => invoke("tools.readBrainFile", [path]), sendRequest: (request: unknown) => invoke("tools.sendRequest", [request]), createSchedule: (schedule: unknown) => invoke("tools.createSchedule", [schedule]), }), }; Object.defineProperty(window, "loopable", { value: Object.freeze(loopable), writable: false, configurable: false }); } export function bridgeScript(): string { return `(${installLoopable.toString()})();`.replace(/