The Coline App SDK: apps, tools, file types and surfaces that run inside Coline workspaces, and public websites with Coline Sites. TypeScript, with an in-memory test workspace. MIT.
| 6 | } from "./types"; |
| 7 | import { DEFAULT_RECORD_ACCESS, type ColineRecordAccess } from "./record-access"; |
| 8 | import { compareRecords, matchesRecordWhere, normalizeRecordSort } from "./record-query"; |
| 9 |
| 10 | export interface LiveCollectionSnapshot<T> { |
| 11 | records: readonly ColineRecordEnvelope<T>[]; |
| 12 | nextCursor: string | null; |
| 13 | status: "loading" | "ready" | "syncing" | "error"; |
| 14 | pending: number; |
| 15 | error: Error | null; |
| 16 | } |
| 17 | export interface LiveCollectionOptions { |
| 18 | /** Fallback for dropped events and reconnects; minimum 250 ms, default 2000 ms. */ |
| 19 | pollIntervalMs?: number; |
| 20 | /** Identity for local optimistic envelopes; the server always supplies authoritative identity. */ |
| 21 | actorUserId?: string; |
| 22 | installId?: string; |
| 23 | } |
| 24 | interface Pending<T> { |
| 25 | id: string; |
| 26 | overlay: (rows: Map<string, ColineRecordEnvelope<T>>) => void; |
| 27 | execute: () => Promise<ColineRecordEnvelope<T> | null>; |
| 28 | resolve: (value: ColineRecordEnvelope<T> | null) => void; |
| 29 | reject: (error: unknown) => void; |
| 30 | } |
| 31 | const authorizationCodes = new Set([ |
| 32 | "permission_not_granted", |
| 33 | "permission_not_declared", |
| 34 | "app_install_not_found", |
| 35 | "install_not_active", |
| 36 | "entitlement_required", |
| 37 | "app_invocation_token_invalid", |
| 38 | "app_invocation_token_expired", |
| 39 | "app_invocation_install_mismatch", |
| 40 | "app_operation_denied", |
| 41 | "workspace_access_denied", |
| 42 | "actor_not_member", |
| 43 | "forbidden", |
| 44 | "unauthorized", |
| 45 | "record_forbidden", |
| 46 | ]); |
| 47 | const asError = (cause: unknown) => (cause instanceof Error ? cause : new Error(String(cause))); |
| 48 |
| 49 | /** A bounded live query: immediate optimistic changes, CAS writes, authoritative replacement on sync. */ |
| 50 | export function createLiveCollection<T extends Record<string, unknown>>( |
| 51 | coline: Pick<ColineCapabilities, "storage" | "realtime">, |
| 52 | name: string, |
| 53 | query: ColineRecordQuery<T> = {}, |
| 54 | options: LiveCollectionOptions = {}, |
| 55 | ) { |
| 56 | const collection: ColineCollection<T> = coline.storage.collection<T>(name); |
| 57 | const input = structuredClone(query); |
| 58 | const listeners = new Set<() => void>(); |
| 59 | let base = new Map<string, ColineRecordEnvelope<T>>(); |
| 60 | let pending: Pending<T>[] = []; |
| 61 | let revision: number | undefined; |
| 62 | let connectionEpoch = 0, |
| 63 | accessEpoch = 0, |
| 64 | snapshotEpoch = 0; |
| 65 | let generation = 0, |
| 66 | running = false, |
| 67 | disposed = false, |
| 68 | started = false; |
| 69 | let syncing: Promise<void> | null = null, |
| 70 | syncAgain = false; |
| 71 | let timer: ReturnType<typeof setInterval> | null = null; |
| 72 | let subscription: { unsubscribe: () => void } | null = null; |
| 73 | let snapshot: LiveCollectionSnapshot<T> = { |
| 74 | records: [], |
| 75 | nextCursor: null, |
| 76 | status: "loading", |
| 77 | pending: 0, |
| 78 | error: null, |
| 79 | }; |
| 80 | function emit(update: Partial<LiveCollectionSnapshot<T>> = {}) { |
| 81 | if (disposed) return; |
| 82 | const rows = new Map(base); |
| 83 | for (const mutation of pending) mutation.overlay(rows); |
| 84 | const records = [...rows.values()].filter((row) => matchesRecordWhere(row.data, input.where)); |
| 85 | const sorts = normalizeRecordSort(input); |
| 86 | records.sort((a, b) => compareRecords(a, b, sorts)); |
| 87 | snapshot = { |
| 88 | ...snapshot, |
| 89 | ...update, |
| 90 | records: records.slice(0, input.limit ?? 50), |
| 91 | pending: pending.length, |
| 92 | }; |
| 93 | for (const listener of listeners) listener(); |
| 94 | } |
| 95 | function cancel(error: unknown, id?: string) { |
| 96 | const removed = pending.filter((item) => !id || item.id === id); |
| 97 | pending = pending.filter((item) => id && item.id !== id); |
| 98 | for (const item of removed) item.reject(error); |
| 99 | } |
| 100 | async function refresh(): Promise<void> { |
| 101 | if (disposed) return; |
| 102 | if (syncing) { |
| 103 | syncAgain = true; |
| 104 | return syncing; |
| 105 | } |
| 106 | syncing = (async () => { |
| 107 | do { |
| 108 | syncAgain = false; |
| 109 | const before = generation; |
| 110 | try { |
| 111 | const result = await collection.sync(input, revision); |
| 112 | if (disposed) return; |
| 113 | if (before !== generation) { |
| 114 | revision = undefined; |
| 115 | syncAgain = true; |
| 116 | continue; |
| 117 | } |
| 118 | if (result.records) { |
| 119 | snapshotEpoch++; |
| 120 | base = new Map(result.records.map((row) => [row.id, row])); |
| 121 | emit({ nextCursor: result.nextCursor }); |
| 122 | } |
| 123 | revision = result.revision; |
| 124 | emit({ status: "ready", error: null }); |
| 125 | } catch (cause) { |
| 126 | if (disposed) return; |
| 127 | const code = (cause as { code?: string })?.code; |
| 128 | // HTTP routes prefix guard denials; the local bridge uses their raw codes. |
| 129 | const denial = |
| 130 | typeof code === "string" ? code.replace(/^APP_CAPABILITY_/i, "").toLowerCase() : null; |
| 131 | if (denial && authorizationCodes.has(denial)) { |
| 132 | accessEpoch++; |
| 133 | base.clear(); |
| 134 | cancel(cause); |
| 135 | revision = undefined; |
| 136 | } |
| 137 | emit({ status: "error", error: asError(cause) }); |
| 138 | } |
| 139 | } while (syncAgain && !disposed); |
| 140 | })(); |
| 141 | try { |
| 142 | await syncing; |
| 143 | } finally { |
| 144 | syncing = null; |
| 145 | } |
| 146 | } |
| 147 | async function drain() { |
| 148 | if (running || disposed) return; |
| 149 | running = true; |
| 150 | try { |
| 151 | while (pending.length && !disposed) { |
| 152 | const item = pending[0]!; |
| 153 | try { |
| 154 | const accessAtStart = accessEpoch, |
| 155 | snapshotAtStart = snapshotEpoch; |
| 156 | const result = await item.execute(); |
| 157 | if (disposed) return; |
| 158 | if (accessAtStart !== accessEpoch) continue; |
| 159 | generation++; |
| 160 | if (result) { |
| 161 | // A newer authorized snapshot may have removed this record (ACL or filter). |
| 162 | if (snapshotAtStart === snapshotEpoch || base.has(item.id)) base.set(item.id, result); |
| 163 | } else base.delete(item.id); |
| 164 | pending = pending.filter((entry) => entry !== item); |
| 165 | item.resolve(result); |
| 166 | emit({ error: null }); |
| 167 | } catch (cause) { |
| 168 | generation++; |
| 169 | cancel(cause, item.id); |
| 170 | revision = undefined; |
| 171 | emit({ error: asError(cause) }); |
| 172 | await refresh(); |
| 173 | } |
| 174 | } |
| 175 | } finally { |
| 176 | running = false; |
| 177 | if (!disposed && started) { |
| 178 | revision = undefined; |
| 179 | void refresh(); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | function enqueue(id: string, overlay: Pending<T>["overlay"], execute: Pending<T>["execute"]) { |
| 184 | if (disposed) return Promise.reject(new Error("Live collection is disposed.")); |
| 185 | const promise = new Promise<ColineRecordEnvelope<T> | null>((resolve, reject) => |
| 186 | pending.push({ id, overlay, execute, resolve, reject }), |
| 187 | ); |
| 188 | emit({ error: null }); // Synchronous: rendering does not wait for the network. |
| 189 | void drain(); |
| 190 | return promise; |
| 191 | } |
| 192 | return { |
| 193 | getSnapshot: () => snapshot, |
| 194 | subscribe(listener: () => void) { |
| 195 | listeners.add(listener); |
| 196 | return () => { |
| 197 | listeners.delete(listener); |
| 198 | }; |
| 199 | }, |
| 200 | async start() { |
| 201 | if (disposed || started) return; |
| 202 | started = true; |
| 203 | const epoch = ++connectionEpoch; |
| 204 | timer = setInterval(() => void refresh(), Math.max(250, options.pollIntervalMs ?? 2000)); |
| 205 | try { |
| 206 | void coline.realtime |
| 207 | .subscribe("coline.records.changed", () => { |
| 208 | void refresh(); |
| 209 | }) |
| 210 | .then((value) => { |
| 211 | if (disposed || !started || epoch !== connectionEpoch) value.unsubscribe(); |
| 212 | else subscription = value; |
| 213 | }) |
| 214 | .catch(() => { |
| 215 | /* Polling remains authoritative when realtime consent is absent. */ |
| 216 | }); |
| 217 | } catch { |
| 218 | /* Transports may reject client-only subscriptions synchronously; keep polling. */ |
| 219 | } |
| 220 | await refresh(); |
| 221 | }, |
| 222 | stop() { |
| 223 | started = false; |
| 224 | connectionEpoch++; |
| 225 | if (timer) clearInterval(timer); |
| 226 | timer = null; |
| 227 | subscription?.unsubscribe(); |
| 228 | subscription = null; |
| 229 | }, |
| 230 | refresh, |
| 231 | insert(data: T, access: ColineRecordAccess = DEFAULT_RECORD_ACCESS) { |
| 232 | const id = `rec_${crypto.randomUUID()}`; |
| 233 | const now = new Date().toISOString(); |
| 234 | const row: ColineRecordEnvelope<T> = { |
| 235 | id, |
| 236 | appId: "", |
| 237 | installId: options.installId ?? "", |
| 238 | collection: name, |
| 239 | version: 1, |
| 240 | access: structuredClone(access), |
| 241 | createdBy: options.actorUserId |
| 242 | ? { kind: "user", userId: options.actorUserId } |
| 243 | : { kind: "app" }, |
| 244 | createdAt: now, |
| 245 | updatedAt: now, |
| 246 | data: structuredClone(data), |
| 247 | }; |
| 248 | return enqueue( |
| 249 | id, |
| 250 | (rows) => { |
| 251 | rows.set(id, row); |
| 252 | }, |
| 253 | () => collection.insert(data, { id, access }), |
| 254 | ); |
| 255 | }, |
| 256 | update(id: string, patch: Partial<T>) { |
| 257 | const copy = structuredClone(patch); |
| 258 | const expectedVersion = snapshot.records.find((row) => row.id === id)?.version; |
| 259 | if (expectedVersion === undefined) |
| 260 | return Promise.reject(new Error("Record is not in this view.")); |
| 261 | return enqueue( |
| 262 | id, |
| 263 | (rows) => { |
| 264 | const row = rows.get(id); |
| 265 | if (row) |
| 266 | rows.set(id, { ...row, version: row.version + 1, data: { ...row.data, ...copy } }); |
| 267 | }, |
| 268 | () => collection.update(id, copy, { expectedVersion }), |
| 269 | ); |
| 270 | }, |
| 271 | delete(id: string) { |
| 272 | const expectedVersion = snapshot.records.find((row) => row.id === id)?.version; |
| 273 | if (expectedVersion === undefined) |
| 274 | return Promise.reject(new Error("Record is not in this view.")); |
| 275 | return enqueue( |
| 276 | id, |
| 277 | (rows) => { |
| 278 | rows.delete(id); |
| 279 | }, |
| 280 | async () => { |
| 281 | await collection.delete(id, { expectedVersion }); |
| 282 | return null; |
| 283 | }, |
| 284 | ); |
| 285 | }, |
| 286 | setAccess(id: string, access: ColineRecordAccess) { |
| 287 | const expectedVersion = snapshot.records.find((row) => row.id === id)?.version; |
| 288 | if (expectedVersion === undefined) |
| 289 | return Promise.reject(new Error("Record is not in this view.")); |
| 290 | return enqueue( |
| 291 | id, |
| 292 | (rows) => { |
| 293 | const row = rows.get(id); |
| 294 | if (row) rows.set(id, { ...row, version: row.version + 1 }); |
| 295 | }, |
| 296 | () => collection.setAccess(id, access, { expectedVersion }), |
| 297 | ); |
| 298 | }, |
| 299 | dispose() { |
| 300 | if (disposed) return; |
| 301 | disposed = true; |
| 302 | if (timer) clearInterval(timer); |
| 303 | subscription?.unsubscribe(); |
| 304 | cancel(new Error("Live collection disposed. A sent write may still finish on the server.")); |
| 305 | listeners.clear(); |
| 306 | base.clear(); |
| 307 | }, |
| 308 | }; |
| 309 | } |
| 310 |