sendHistory
Send what the remote lacks of the history up to head: its causals, the namespace nodes they point at (a
node the remote has, it has everything under), and every definition those name. Returns definitions sent.
import type { Codebase } from "#codebase/tcmts/cli/codebase"; import { chunks, oldestFirst, sendClosure } from "#codebase/tcmts/cli/sync"; import type { Remote } from "#codebase/tcmts/cli/sync"; import type { NamespaceBody } from "#codebase/tcmts/store"; export async function sendHistory(cb: Codebase, remote: Remote, head: string): Promise<number> { const lacking: string[] = []; for (const c of chunks(cb.store.ancestors(head))) lacking.push(...(await remote.missingCausals(c))); if (!lacking.length) return 0; const causals = oldestFirst(cb.store.getCausals(lacking)); const nodes: string[] = []; const named = new Set<string>(); const seen = new Set<string>(); for (let level = [...new Set(causals.map((c) => c.namespace))]; level.length;) { level = level.filter((h) => !seen.has(h) && (seen.add(h), true)); const missing: string[] = []; for (const c of chunks(level)) missing.push(...(await remote.missingNamespaces(c))); nodes.push(...missing); for (const n of missing) for (const d of cb.store.namespaceDefinitions(n)) named.add(d); level = missing.flatMap((n) => cb.store.namespaceChildren(n)); } const sent = await sendClosure(cb, remote, [...named]); // and the text versions those nodes' names show (comments, layout, doc), for code the remote may have already const shown = new Map<string, [string, string]>(); for (const n of cb.store.getNamespaces(nodes)) for (const [, d, t] of (JSON.parse(n.body) as NamespaceBody)[3] ?? []) shown.set(`${d} ${t}`, [d, t]); const texts = [...shown.values()].flatMap(([d, t]) => cb.store.textVersion(d, t) ? [cb.store.defWithText(d, t)!] : [], ); for (const c of chunks(texts)) await remote.put(c); // children before the nodes that point at them for (const c of chunks(nodes.reverse())) await remote.putNamespaces(cb.store.getNamespaces(c)); for (const c of chunks(causals)) await remote.putCausals(c); return sent; }
uses
- tcmts.cli.codebase.Codebase
- tcmts.cli.sync.Remote
- tcmts.cli.sync.chunks
- tcmts.cli.sync.oldestFirst
- tcmts.cli.sync.sendClosure
- tcmts.store.NamespaceBody