| 1 | import type { ProjectNode } from "./types"; |
| 2 | import { sessionIdentityBaseKey } from "./sessionIdentity"; |
| 3 | |
| 4 | /** Stable row identity; topicId is only the last resort for an empty draft. */ |
| 5 | export function projectSessionIdentity(node: ProjectNode): string { |
| 6 | if (node.session?.sessionId) return sessionIdentityBaseKey(node); |
| 7 | if (node.remoteSession) { |
| 8 | const remote = node.remoteSession; |
| 9 | if (remote.sessionId) return sessionIdentityBaseKey({ session: { hostId: remote.hostId, sessionId: remote.sessionId } }); |
| 10 | return `source\u0000${remote.hostId}\u0000${remote.workspace}\u0000${remote.path || remote.name}`; |
| 11 | } |
| 12 | if (node.source) return `source\u0000${node.source.hostId || "local"}\u0000${node.source.sourceKey || `${node.source.path}\u0000${node.source.headId || ""}`}`; |
| 13 | if (node.sessionPath) return `path\u0000${node.sessionPath.trim()}`; |
| 14 | if (node.tabId) return `tab\u0000local\u0000${node.tabId}`; |
| 15 | return `topic\u0000${node.topicId || node.key}`; |
| 16 | } |
| 17 | |
| 18 | /** Only owner-verified aliases may join a source to its adopted session. */ |
| 19 | export function projectSessionKeys(node: ProjectNode): string[] { |
| 20 | return [projectSessionIdentity(node), ...(node.identityAliases ?? [])]; |
| 21 | } |
| 22 | |
| 23 | export function canonicalProjectSession(node: ProjectNode): string | undefined { |
| 24 | return node.session?.sessionId || node.remoteSession?.sessionId ? projectSessionIdentity(node) : undefined; |
| 25 | } |
| 26 | |
| 27 | /** Resolve legacy rows only when the complete page/overlay proves one owner. */ |
| 28 | export function projectSessionResolver(nodes: readonly ProjectNode[]): (node: ProjectNode) => string { |
| 29 | const owners = new Map<string, { identity: string | null; canonical: boolean }>(); |
| 30 | for (const node of nodes) { |
| 31 | const canonical = Boolean(canonicalProjectSession(node)); |
| 32 | const owner = canonicalProjectSession(node) ?? (node.tabId ? projectSessionIdentity(node) : undefined); |
| 33 | if (!owner) continue; |
| 34 | for (const key of projectSessionKeys(node)) { |
| 35 | const previous = owners.get(key); |
| 36 | if (previous?.canonical && !canonical) continue; |
| 37 | const identity = !previous || (canonical && !previous.canonical) || previous.identity === owner ? owner : null; |
| 38 | owners.set(key, { identity, canonical }); |
| 39 | } |
| 40 | } |
| 41 | return node => canonicalProjectSession(node) ?? owners.get(projectSessionIdentity(node))?.identity ?? projectSessionIdentity(node); |
| 42 | } |
| 43 | |
| 44 | export function projectSessionExcluded(node: ProjectNode, excluded: ReadonlySet<string>): boolean { |
| 45 | // A topic archive fences by the topicId it removed, whatever the row's identity. |
| 46 | if (node.topicId && excluded.has(node.topicId)) return true; |
| 47 | const canonical = canonicalProjectSession(node); |
| 48 | if (canonical) return excluded.has(canonical); |
| 49 | return projectSessionKeys(node).some(key => excluded.has(key)); |
| 50 | } |
| 51 | |
| 52 | /** A source keeps its mounted row key when its owner publishes canonical aliases. */ |
| 53 | const mountedRowKeys = new Map<string, { key: string; owner?: string }>(); |
| 54 | export function projectSessionRowKey(node: ProjectNode): string { |
| 55 | const keys = projectSessionKeys(node); |
| 56 | const identity = keys[0]; |
| 57 | const canonical = canonicalProjectSession(node); |
| 58 | // An ambiguous source can coexist with its historical versions. Its old |
| 59 | // mounted key already belongs to an adopted canonical row in that case. |
| 60 | if (!canonical && mountedRowKeys.get(identity)?.owner) return `source-row\u0000${identity}`; |
| 61 | // A legacy alias may outlive an adoption or arrive from an older host. |
| 62 | // It can preserve a source's mounted row, but cannot merge two SessionRefs. |
| 63 | const entry: { key: string; owner?: string } = keys.map(key => mountedRowKeys.get(key)).find(entry => |
| 64 | entry && (!entry.owner || entry.owner === canonical)) ?? { key: identity }; |
| 65 | entry.owner ??= canonical; |
| 66 | for (const key of keys) { |
| 67 | const previous = mountedRowKeys.get(key); |
| 68 | if (!previous?.owner || previous.owner === canonical) mountedRowKeys.set(key, entry); |
| 69 | } |
| 70 | return entry.key; |
| 71 | } |
| 72 | |
| 73 | export function sameProjectSession(a: ProjectNode, b: ProjectNode): boolean { |
| 74 | if (canonicalProjectSession(a) && canonicalProjectSession(b)) return projectSessionIdentity(a) === projectSessionIdentity(b); |
| 75 | const keys = new Set(projectSessionKeys(a)); |
| 76 | return projectSessionKeys(b).some(key => keys.has(key)); |
| 77 | } |
| 78 |