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 { 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 | "workspace_access_denied", |
| 37 | "actor_not_member", |
| 38 | "FORBIDDEN", |
| 39 | "UNAUTHORIZED", |
| 40 | "record_forbidden", |
| 41 | ]); |
| 42 | const asError = (cause: unknown) => (cause instanceof Error ? cause : new Error(String(cause))); |
| 43 |
| 44 | /** A bounded live query: immediate optimistic changes, CAS writes, authoritative replacement on sync. */ |
| 45 | export function createLiveCollection<T extends Record<string, unknown>>( |
| 46 | coline: Pick<ColineCapabilities, "storage" | "realtime">, |
| 47 | name: string, |
| 48 | query: ColineRecordQuery<T> = {}, |
| 49 | options: LiveCollectionOptions = {}, |
| 50 | ) { |
| 51 | const collection: ColineCollection<T> = coline.storage.collection<T>(name); |
| 52 | const input = structuredClone(query); |
| 53 | const listeners = new Set<() => void>(); |
| 54 | let base = new Map<string, ColineRecordEnvelope<T>>(); |
| 55 | let pending: Pending<T>[] = []; |
| 56 | let revision: number | undefined; |
| 57 | let connectionEpoch = 0, |
| 58 | accessEpoch = 0, |
| 59 | snapshotEpoch = 0; |
| 60 | let generation = 0, |
| 61 | running = false, |
| 62 | disposed = false, |
| 63 | started = false; |
| 64 | let syncing: Promise<void> | null = null, |
| 65 | syncAgain = false; |
| 66 | let timer: ReturnType<typeof setInterval> | null = null; |
| 67 | let subscription: { unsubscribe: () => void } | null = null; |
| 68 | let snapshot: LiveCollectionSnapshot<T> = { |
| 69 | records: [], |
| 70 | nextCursor: null, |
| 71 | status: "loading", |
| 72 | pending: 0, |
| 73 | error: null, |
| 74 | }; |
| 75 | function emit(update: Partial<LiveCollectionSnapshot<T>> = {}) { |
| 76 | if (disposed) return; |
| 77 | const rows = new Map(base); |
| 78 | for (const mutation of pending) mutation.overlay(rows); |
| 79 | const records = [...rows.values()].filter((row) => matchesRecordWhere(row.data, input.where)); |
| 80 | records.sort((a, b) => { |
| 81 | const av = input.orderBy ? a.data[input.orderBy] : a.updatedAt; |
| 82 | const bv = input.orderBy ? b.data[input.orderBy] : b.updatedAt; |
| 83 | const comparison = |
| 84 | String(av ?? "").localeCompare(String(bv ?? "")) || a.id.localeCompare(b.id); |
| 85 | return input.order === "asc" ? comparison : -comparison; |
| 86 | }); |
| 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 | if (code && authorizationCodes.has(code)) { |
| 129 | accessEpoch++; |
| 130 | base.clear(); |
| 131 | cancel(cause); |
| 132 | revision = undefined; |
| 133 | } |
| 134 | emit({ status: "error", error: asError(cause) }); |
| 135 | } |
| 136 | } while (syncAgain && !disposed); |
| 137 | })(); |
| 138 | try { |
| 139 | await syncing; |
| 140 | } finally { |
| 141 | syncing = null; |
| 142 | } |
| 143 | } |
| 144 | async function drain() { |
| 145 | if (running || disposed) return; |
| 146 | running = true; |
| 147 | try { |
| 148 | while (pending.length && !disposed) { |
| 149 | const item = pending[0]!; |
| 150 | try { |
| 151 | const accessAtStart = accessEpoch, |
| 152 | snapshotAtStart = snapshotEpoch; |
| 153 | const result = await item.execute(); |
| 154 | if (disposed) return; |
| 155 | if (accessAtStart !== accessEpoch) continue; |
| 156 | generation++; |
| 157 | if (result) { |
| 158 | // A newer authorized snapshot may have removed this record (ACL or filter). |
| 159 | if (snapshotAtStart === snapshotEpoch || base.has(item.id)) base.set(item.id, result); |
| 160 | } else base.delete(item.id); |
| 161 | pending = pending.filter((entry) => entry !== item); |
| 162 | item.resolve(result); |
| 163 | emit({ error: null }); |
| 164 | } catch (cause) { |
| 165 | generation++; |
| 166 | cancel(cause, item.id); |
| 167 | revision = undefined; |
| 168 | emit({ error: asError(cause) }); |
| 169 | await refresh(); |
| 170 | } |
| 171 | } |
| 172 | } finally { |
| 173 | running = false; |
| 174 | if (!disposed && started) { |
| 175 | revision = undefined; |
| 176 | void refresh(); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | function enqueue(id: string, overlay: Pending<T>["overlay"], execute: Pending<T>["execute"]) { |
| 181 | if (disposed) return Promise.reject(new Error("Live collection is disposed.")); |
| 182 | const promise = new Promise<ColineRecordEnvelope<T> | null>((resolve, reject) => |
| 183 | pending.push({ id, overlay, execute, resolve, reject }), |
| 184 | ); |
| 185 | emit({ error: null }); // Synchronous: rendering does not wait for the network. |
| 186 | void drain(); |
| 187 | return promise; |
| 188 | } |
| 189 | return { |
| 190 | getSnapshot: () => snapshot, |
| 191 | subscribe(listener: () => void) { |
| 192 | listeners.add(listener); |
| 193 | return () => { |
| 194 | listeners.delete(listener); |
| 195 | }; |
| 196 | }, |
| 197 | async start() { |
| 198 | if (disposed || started) return; |
| 199 | started = true; |
| 200 | const epoch = ++connectionEpoch; |
| 201 | timer = setInterval(() => void refresh(), Math.max(250, options.pollIntervalMs ?? 2000)); |
| 202 | void coline.realtime |
| 203 | .subscribe("coline.records.changed", () => { |
| 204 | void refresh(); |
| 205 | }) |
| 206 | .then((value) => { |
| 207 | if (disposed || !started || epoch !== connectionEpoch) value.unsubscribe(); |
| 208 | else subscription = value; |
| 209 | }) |
| 210 | .catch(() => { |
| 211 | /* Polling remains authoritative when realtime consent is absent. */ |
| 212 | }); |
| 213 | await refresh(); |
| 214 | }, |
| 215 | stop() { |
| 216 | started = false; |
| 217 | connectionEpoch++; |
| 218 | if (timer) clearInterval(timer); |
| 219 | timer = null; |
| 220 | subscription?.unsubscribe(); |
| 221 | subscription = null; |
| 222 | }, |
| 223 | refresh, |
| 224 | insert(data: T, access: ColineRecordAccess = DEFAULT_RECORD_ACCESS) { |
| 225 | const id = `rec_${crypto.randomUUID()}`; |
| 226 | const now = new Date().toISOString(); |
| 227 | const row: ColineRecordEnvelope<T> = { |
| 228 | id, |
| 229 | appId: "", |
| 230 | installId: options.installId ?? "", |
| 231 | collection: name, |
| 232 | version: 1, |
| 233 | access: structuredClone(access), |
| 234 | createdBy: options.actorUserId |
| 235 | ? { kind: "user", userId: options.actorUserId } |
| 236 | : { kind: "app" }, |
| 237 | createdAt: now, |
| 238 | updatedAt: now, |
| 239 | data: structuredClone(data), |
| 240 | }; |
| 241 | return enqueue( |
| 242 | id, |
| 243 | (rows) => { |
| 244 | rows.set(id, row); |
| 245 | }, |
| 246 | () => collection.insert(data, { id, access }), |
| 247 | ); |
| 248 | }, |
| 249 | update(id: string, patch: Partial<T>) { |
| 250 | const copy = structuredClone(patch); |
| 251 | const expectedVersion = snapshot.records.find((row) => row.id === id)?.version; |
| 252 | if (expectedVersion === undefined) |
| 253 | return Promise.reject(new Error("Record is not in this view.")); |
| 254 | return enqueue( |
| 255 | id, |
| 256 | (rows) => { |
| 257 | const row = rows.get(id); |
| 258 | if (row) |
| 259 | rows.set(id, { ...row, version: row.version + 1, data: { ...row.data, ...copy } }); |
| 260 | }, |
| 261 | () => collection.update(id, copy, { expectedVersion }), |
| 262 | ); |
| 263 | }, |
| 264 | delete(id: string) { |
| 265 | const expectedVersion = snapshot.records.find((row) => row.id === id)?.version; |
| 266 | if (expectedVersion === undefined) |
| 267 | return Promise.reject(new Error("Record is not in this view.")); |
| 268 | return enqueue( |
| 269 | id, |
| 270 | (rows) => { |
| 271 | rows.delete(id); |
| 272 | }, |
| 273 | async () => { |
| 274 | await collection.delete(id, { expectedVersion }); |
| 275 | return null; |
| 276 | }, |
| 277 | ); |
| 278 | }, |
| 279 | setAccess(id: string, access: ColineRecordAccess) { |
| 280 | const expectedVersion = snapshot.records.find((row) => row.id === id)?.version; |
| 281 | if (expectedVersion === undefined) |
| 282 | return Promise.reject(new Error("Record is not in this view.")); |
| 283 | return enqueue( |
| 284 | id, |
| 285 | (rows) => { |
| 286 | const row = rows.get(id); |
| 287 | if (row) rows.set(id, { ...row, version: row.version + 1 }); |
| 288 | }, |
| 289 | () => collection.setAccess(id, access, { expectedVersion }), |
| 290 | ); |
| 291 | }, |
| 292 | dispose() { |
| 293 | if (disposed) return; |
| 294 | disposed = true; |
| 295 | if (timer) clearInterval(timer); |
| 296 | subscription?.unsubscribe(); |
| 297 | cancel(new Error("Live collection disposed. A sent write may still finish on the server.")); |
| 298 | listeners.clear(); |
| 299 | base.clear(); |
| 300 | }, |
| 301 | }; |
| 302 | } |
| 303 |