import type { GuestWrite, Kernel } from "@nibling/kernel"; import type { EphemeralStore } from "loro-crdt"; import type { Doc, Unsubscribe, Version } from "./doc.ts"; import { hideCursor, showCursor, type Actor } from "./presence.ts"; export interface BridgeOptions { /** Shows each writing process's cursor over what it changed, to editors sharing this store. */ readonly presence?: EphemeralStore; /** How a writing process appears to editors. */ readonly actor?: (write: GuestWrite) => Actor; /** How long a process's cursor stays after its last write. */ readonly cursorMs?: number; } /** How many kernel generations to remember. A process that read an older one replaces the text. */ const MAX_VERSIONS = 1000; const defaultActor = ({ pid, argv }: GuestWrite): Actor => { const script = argv.find((arg, i) => i > 0 && !arg.startsWith("-"))?.split("/").pop(); return { id: `pid${pid}`, user: { name: script ? `${script} ยท ${pid}` : `pid ${pid}`, colorClassName: `nib-user-${2 + (pid % 4)}` } }; }; /** * Keeps the kernel file at `path` and `doc` in step, with `doc` as the source of truth. * * The doc's text is written into the kernel whenever it changes, and each write's kernel * generation is remembered with the doc version it holds. A guest's finished write is merged * into the doc against the version of the generation that process last read (D16), so edits * it never saw survive; the merged text then goes back into the kernel, where guest * `fs.watch` listeners see it. */ export function bridge(kernel: Pick, path: string, doc: Doc, options: BridgeOptions = {}): Unsubscribe { const { presence, actor = defaultActor, cursorMs = 6000 } = options; const decoder = new TextDecoder(); const versions = new Map(); const cursorTimers = new Map>(); /** The text the kernel holds, as far as the bridge knows. */ let kernelText: string | null = null; let pushing = false; let again = false; let stopped = false; const remember = (gen: number, version: Version) => { versions.set(gen, version); if (versions.size > MAX_VERSIONS) versions.delete(versions.keys().next().value!); }; // One push in flight at a time; changes made meanwhile are pushed once it lands. async function push(): Promise { if (pushing) { again = true; return; } pushing = true; try { do { again = false; const { text, version } = doc.read(); if (text === kernelText) continue; kernelText = text; remember(await kernel.putFile(path, text), version); } while (again && !stopped); } finally { pushing = false; } } function onGuestWrite(write: GuestWrite): void { const text = decoder.decode(write.data); kernelText = text; const base = write.readGen === null ? undefined : versions.get(write.readGen); const result = doc.write(text, base ? { base } : {}); remember(write.gen, result.version); if (presence && result.changed) { const who = actor(write); showCursor(doc, presence, who, result.changed.from, result.changed.to); clearTimeout(cursorTimers.get(who.id)); cursorTimers.set(who.id, setTimeout(() => hideCursor(presence, who), cursorMs)); } void push(); } const stopWrites = kernel.observeWrites([path], (write) => { if (write.path === path) onGuestWrite(write); }); const stopChanges = doc.onChange(() => void push()); void push(); return () => { stopped = true; stopWrites(); stopChanges(); for (const timer of cursorTimers.values()) clearTimeout(timer); if (presence) for (const id of cursorTimers.keys()) hideCursor(presence, { id }); }; }