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 { compareRecordValues, matchesRecordWhere } 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 | records.sort((a, b) => { |
| 86 | const av = input.orderBy ? a.data[input.orderBy] : a.updatedAt; |
| 87 | const bv = input.orderBy ? b.data[input.orderBy] : b.updatedAt; |
| 88 | const comparison = compareRecordValues(av, bv) || a.id.localeCompare(b.id); |
| 89 | return input.order === "asc" ? comparison : -comparison; |
| 90 | }); |
| 91 | snapshot = { |
| 92 | ...snapshot, |
| 93 | ...update, |
| 94 | records: records.slice(0, input.limit ?? 50), |
| 95 | pending: pending.length, |
| 96 | }; |
| 97 | for (const listener of listeners) listener(); |
| 98 | } |
| 99 | function cancel(error: unknown, id?: string) { |
| 100 | const removed = pending.filter((item) => !id || item.id === id); |
| 101 | pending = pending.filter((item) => id && item.id !== id); |
| 102 | for (const item of removed) item.reject(error); |
| 103 | } |
| 104 | async function refresh(): Promise<void> { |
| 105 | if (disposed) return; |
| 106 | if (syncing) { |
| 107 | syncAgain = true; |
| 108 | return syncing; |
| 109 | } |
| 110 | syncing = (async () => { |
| 111 | do { |
| 112 | syncAgain = false; |
| 113 | const before = generation; |
| 114 | try { |
| 115 | const result = await collection.sync(input, revision); |
| 116 | if (disposed) return; |
| 117 | if (before !== generation) { |
| 118 | revision = undefined; |
| 119 | syncAgain = true; |
| 120 | continue; |
| 121 | } |
| 122 | if (result.records) { |
| 123 | snapshotEpoch++; |
| 124 | base = new Map(result.records.map((row) => [row.id, row])); |
| 125 | emit({ nextCursor: result.nextCursor }); |
| 126 | } |
| 127 | revision = result.revision; |
| 128 | emit({ status: "ready", error: null }); |
| 129 | } catch (cause) { |
| 130 | if (disposed) return; |
| 131 | const code = (cause as { code?: string })?.code; |
| 132 | // HTTP routes prefix guard denials; the local bridge uses their raw codes. |
| 133 | const denial = |
| 134 | typeof code === "string" ? code.replace(/^APP_CAPABILITY_/i, "").toLowerCase() : null; |
| 135 | if (denial && authorizationCodes.has(denial)) { |
| 136 | accessEpoch++; |
| 137 | base.clear(); |
| 138 | cancel(cause); |
| 139 | revision = undefined; |
| 140 | } |
| 141 | emit({ status: "error", error: asError(cause) }); |
| 142 | } |
| 143 | } while (syncAgain && !disposed); |
| 144 | })(); |
| 145 | try { |
| 146 | await syncing; |
| 147 | } finally { |
| 148 | syncing = null; |
| 149 | } |
| 150 | } |
| 151 | async function drain() { |
| 152 | if (running || disposed) return; |
| 153 | running = true; |
| 154 | try { |
| 155 | while (pending.length && !disposed) { |
| 156 | const item = pending[0]!; |
| 157 | try { |
| 158 | const accessAtStart = accessEpoch, |
| 159 | snapshotAtStart = snapshotEpoch; |
| 160 | const result = await item.execute(); |
| 161 | if (disposed) return; |
| 162 | if (accessAtStart !== accessEpoch) continue; |
| 163 | generation++; |
| 164 | if (result) { |
| 165 | // A newer authorized snapshot may have removed this record (ACL or filter). |
| 166 | if (snapshotAtStart === snapshotEpoch || base.has(item.id)) base.set(item.id, result); |
| 167 | } else base.delete(item.id); |
| 168 | pending = pending.filter((entry) => entry !== item); |
| 169 | item.resolve(result); |
| 170 | emit({ error: null }); |
| 171 | } catch (cause) { |
| 172 | generation++; |
| 173 | cancel(cause, item.id); |
| 174 | revision = undefined; |
| 175 | emit({ error: asError(cause) }); |
| 176 | await refresh(); |
| 177 | } |
| 178 | } |
| 179 | } finally { |
| 180 | running = false; |
| 181 | if (!disposed && started) { |
| 182 | revision = undefined; |
| 183 | void refresh(); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | function enqueue(id: string, overlay: Pending<T>["overlay"], execute: Pending<T>["execute"]) { |
| 188 | if (disposed) return Promise.reject(new Error("Live collection is disposed.")); |
| 189 | const promise = new Promise<ColineRecordEnvelope<T> | null>((resolve, reject) => |
| 190 | pending.push({ id, overlay, execute, resolve, reject }), |
| 191 | ); |
| 192 | emit({ error: null }); // Synchronous: rendering does not wait for the network. |
| 193 | void drain(); |
| 194 | return promise; |
| 195 | } |
| 196 | return { |
| 197 | getSnapshot: () => snapshot, |
| 198 | subscribe(listener: () => void) { |
| 199 | listeners.add(listener); |
| 200 | return () => { |
| 201 | listeners.delete(listener); |
| 202 | }; |
| 203 | }, |
| 204 | async start() { |
| 205 | if (disposed || started) return; |
| 206 | started = true; |
| 207 | const epoch = ++connectionEpoch; |
| 208 | timer = setInterval(() => void refresh(), Math.max(250, options.pollIntervalMs ?? 2000)); |
| 209 | try { |
| 210 | void coline.realtime |
| 211 | .subscribe("coline.records.changed", () => { |
| 212 | void refresh(); |
| 213 | }) |
| 214 | .then((value) => { |
| 215 | if (disposed || !started || epoch !== connectionEpoch) value.unsubscribe(); |
| 216 | else subscription = value; |
| 217 | }) |
| 218 | .catch(() => { |
| 219 | /* Polling remains authoritative when realtime consent is absent. */ |
| 220 | }); |
| 221 | } catch { |
| 222 | /* Transports may reject client-only subscriptions synchronously; keep polling. */ |
| 223 | } |
| 224 | await refresh(); |
| 225 | }, |
| 226 | stop() { |
| 227 | started = false; |
| 228 | connectionEpoch++; |
| 229 | if (timer) clearInterval(timer); |
| 230 | timer = null; |
| 231 | subscription?.unsubscribe(); |
| 232 | subscription = null; |
| 233 | }, |
| 234 | refresh, |
| 235 | insert(data: T, access: ColineRecordAccess = DEFAULT_RECORD_ACCESS) { |
| 236 | const id = `rec_${crypto.randomUUID()}`; |
| 237 | const now = new Date().toISOString(); |
| 238 | const row: ColineRecordEnvelope<T> = { |
| 239 | id, |
| 240 | appId: "", |
| 241 | installId: options.installId ?? "", |
| 242 | collection: name, |
| 243 | version: 1, |
| 244 | access: structuredClone(access), |
| 245 | createdBy: options.actorUserId |
| 246 | ? { kind: "user", userId: options.actorUserId } |
| 247 | : { kind: "app" }, |
| 248 | createdAt: now, |
| 249 | updatedAt: now, |
| 250 | data: structuredClone(data), |
| 251 | }; |
| 252 | return enqueue( |
| 253 | id, |
| 254 | (rows) => { |
| 255 | rows.set(id, row); |
| 256 | }, |
| 257 | () => collection.insert(data, { id, access }), |
| 258 | ); |
| 259 | }, |
| 260 | update(id: string, patch: Partial<T>) { |
| 261 | const copy = structuredClone(patch); |
| 262 | const expectedVersion = snapshot.records.find((row) => row.id === id)?.version; |
| 263 | if (expectedVersion === undefined) |
| 264 | return Promise.reject(new Error("Record is not in this view.")); |
| 265 | return enqueue( |
| 266 | id, |
| 267 | (rows) => { |
| 268 | const row = rows.get(id); |
| 269 | if (row) |
| 270 | rows.set(id, { ...row, version: row.version + 1, data: { ...row.data, ...copy } }); |
| 271 | }, |
| 272 | () => collection.update(id, copy, { expectedVersion }), |
| 273 | ); |
| 274 | }, |
| 275 | delete(id: string) { |
| 276 | const expectedVersion = snapshot.records.find((row) => row.id === id)?.version; |
| 277 | if (expectedVersion === undefined) |
| 278 | return Promise.reject(new Error("Record is not in this view.")); |
| 279 | return enqueue( |
| 280 | id, |
| 281 | (rows) => { |
| 282 | rows.delete(id); |
| 283 | }, |
| 284 | async () => { |
| 285 | await collection.delete(id, { expectedVersion }); |
| 286 | return null; |
| 287 | }, |
| 288 | ); |
| 289 | }, |
| 290 | setAccess(id: string, access: ColineRecordAccess) { |
| 291 | const expectedVersion = snapshot.records.find((row) => row.id === id)?.version; |
| 292 | if (expectedVersion === undefined) |
| 293 | return Promise.reject(new Error("Record is not in this view.")); |
| 294 | return enqueue( |
| 295 | id, |
| 296 | (rows) => { |
| 297 | const row = rows.get(id); |
| 298 | if (row) rows.set(id, { ...row, version: row.version + 1 }); |
| 299 | }, |
| 300 | () => collection.setAccess(id, access, { expectedVersion }), |
| 301 | ); |
| 302 | }, |
| 303 | dispose() { |
| 304 | if (disposed) return; |
| 305 | disposed = true; |
| 306 | if (timer) clearInterval(timer); |
| 307 | subscription?.unsubscribe(); |
| 308 | cancel(new Error("Live collection disposed. A sent write may still finish on the server.")); |
| 309 | listeners.clear(); |
| 310 | base.clear(); |
| 311 | }, |
| 312 | }; |
| 313 | } |
| 314 |