Separate tab row recording from provenance reads

This commit is contained in:
Brennan Benson
2026-11-09 12:36:13 -08:00
parent f4f7631f58
commit ef7523dd20
3 changed files with 73 additions and 57 deletions
@@ -1,4 +1,4 @@
import { recordTabRowChanges } from './keyed-tab-row-changes'
import { recordTabRowChanges } from './keyed-tab-row-diff'
// Accepted-write provenance for keyed store records: the writer's helper names what it replaced,
// so readers update only those keys; anything it did not annotate reads as a full resync.
+11 -56
View File
@@ -1,72 +1,27 @@
import { linkOf, type Keyed, type KeyedSnapshot } from './keyed-record-transitions'
import { linkOf, type Keyed } from './keyed-record-transitions'
import {
tabRowChangesForSnapshot,
type TabRowChange,
type TabRowChanges
} from './keyed-tab-row-diff'
export type { TabRowChange } from './keyed-tab-row-diff'
type Row = { id: string }
export type TabRowChange<T extends Row> = {
before: T | undefined
after: T | undefined
}
type Changes<T extends Row> = ReadonlyMap<string, ReadonlyMap<string, TabRowChange<T>>>
const rowsBySnapshot = new WeakMap<Keyed, Changes<Row> | 'unknown'>()
function rows(record: KeyedSnapshot, key: string): readonly Row[] | undefined {
const value = Object.hasOwn(record, key) ? record[key] : undefined
return Array.isArray(value) ? value : undefined
}
export function recordTabRowChanges(
previous: KeyedSnapshot,
next: KeyedSnapshot,
keys: Iterable<string>
): Set<string> {
const tabIds = new Set<string>()
const changed = new Map<string, Map<string, TabRowChange<Row>>>()
let reordered = false
for (const workspaceId of keys) {
const previousRows = rows(previous, workspaceId)
const nextRows = rows(next, workspaceId)
if (previousRows === nextRows) {
continue
}
const before = new Map((previousRows ?? []).map((tab, index) => [tab.id, index]))
const changes = new Map<string, TabRowChange<Row>>()
let lastPosition = -1
for (const tab of nextRows ?? []) {
const position = before.get(tab.id)
const previousTab = position === undefined ? undefined : previousRows?.[position]
if (position !== undefined) {
reordered ||= position < lastPosition
lastPosition = position
}
if (previousTab !== tab) {
tabIds.add(tab.id)
changes.set(tab.id, { before: previousTab, after: tab })
}
before.delete(tab.id)
}
for (const [id, position] of before) {
tabIds.add(id)
changes.set(id, { before: previousRows?.[position], after: undefined })
}
changed.set(workspaceId, changes)
}
// Changed rows cannot describe which duplicate entity wins after a reorder.
rowsBySnapshot.set(next, reordered ? 'unknown' : changed)
return tabIds
}
/** Only changed row references survive the writer's existing diff; unavailable histories resync. */
export function readTabRowChanges<T extends Row>(
next: Readonly<Record<string, readonly T[]>>,
previous: Readonly<Record<string, readonly T[]>>
): Changes<T> | 'unknown' | null {
): TabRowChanges<T> | 'unknown' | null {
if (next === previous) {
return null
}
const steps: Changes<Row>[] = []
const steps: TabRowChanges<Row>[] = []
let current: Keyed = next
for (let depth = 0; ; depth++) {
const link = linkOf(current)
const changes = rowsBySnapshot.get(current)
const changes = tabRowChangesForSnapshot(current)
if (depth === 64 || !link || 'reason' in link || !changes || changes === 'unknown') {
return 'unknown'
}
@@ -0,0 +1,61 @@
type KeyedSnapshot = Readonly<Record<string, unknown>>
type Keyed = KeyedSnapshot | ReadonlyMap<string, unknown>
type Row = { id: string }
export type TabRowChange<T extends Row> = {
before: T | undefined
after: T | undefined
}
export type TabRowChanges<T extends Row> = ReadonlyMap<string, ReadonlyMap<string, TabRowChange<T>>>
const rowsBySnapshot = new WeakMap<Keyed, TabRowChanges<Row> | 'unknown'>()
function rows(record: KeyedSnapshot, key: string): readonly Row[] | undefined {
const value = Object.hasOwn(record, key) ? record[key] : undefined
return Array.isArray(value) ? value : undefined
}
export function recordTabRowChanges(
previous: KeyedSnapshot,
next: KeyedSnapshot,
keys: Iterable<string>
): Set<string> {
const tabIds = new Set<string>()
const changed = new Map<string, Map<string, TabRowChange<Row>>>()
let reordered = false
for (const workspaceId of keys) {
const previousRows = rows(previous, workspaceId)
const nextRows = rows(next, workspaceId)
if (previousRows === nextRows) {
continue
}
const before = new Map((previousRows ?? []).map((tab, index) => [tab.id, index]))
const changes = new Map<string, TabRowChange<Row>>()
let lastPosition = -1
for (const tab of nextRows ?? []) {
const position = before.get(tab.id)
const previousTab = position === undefined ? undefined : previousRows?.[position]
if (position !== undefined) {
reordered ||= position < lastPosition
lastPosition = position
}
if (previousTab !== tab) {
tabIds.add(tab.id)
changes.set(tab.id, { before: previousTab, after: tab })
}
before.delete(tab.id)
}
for (const [id, position] of before) {
tabIds.add(id)
changes.set(id, { before: previousRows?.[position], after: undefined })
}
changed.set(workspaceId, changes)
}
// Changed rows cannot describe which duplicate entity wins after a reorder.
rowsBySnapshot.set(next, reordered ? 'unknown' : changed)
return tabIds
}
export function tabRowChangesForSnapshot(
snapshot: Keyed
): TabRowChanges<Row> | 'unknown' | undefined {
return rowsBySnapshot.get(snapshot)
}