diff --git a/src/renderer/src/store/keyed-record-transitions.ts b/src/renderer/src/store/keyed-record-transitions.ts index a42a18074f5..4001c97df9d 100644 --- a/src/renderer/src/store/keyed-record-transitions.ts +++ b/src/renderer/src/store/keyed-record-transitions.ts @@ -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. diff --git a/src/renderer/src/store/keyed-tab-row-changes.ts b/src/renderer/src/store/keyed-tab-row-changes.ts index 0c5d031a26b..081e14f2a0b 100644 --- a/src/renderer/src/store/keyed-tab-row-changes.ts +++ b/src/renderer/src/store/keyed-tab-row-changes.ts @@ -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 = { - before: T | undefined - after: T | undefined -} -type Changes = ReadonlyMap>> -const rowsBySnapshot = new WeakMap | '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 -): Set { - const tabIds = new Set() - const changed = new Map>>() - 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>() - 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( next: Readonly>, previous: Readonly> -): Changes | 'unknown' | null { +): TabRowChanges | 'unknown' | null { if (next === previous) { return null } - const steps: Changes[] = [] + const steps: TabRowChanges[] = [] 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' } diff --git a/src/renderer/src/store/keyed-tab-row-diff.ts b/src/renderer/src/store/keyed-tab-row-diff.ts new file mode 100644 index 00000000000..930f2c526de --- /dev/null +++ b/src/renderer/src/store/keyed-tab-row-diff.ts @@ -0,0 +1,61 @@ +type KeyedSnapshot = Readonly> +type Keyed = KeyedSnapshot | ReadonlyMap +type Row = { id: string } +export type TabRowChange = { + before: T | undefined + after: T | undefined +} +export type TabRowChanges = ReadonlyMap>> +const rowsBySnapshot = new WeakMap | '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 +): Set { + const tabIds = new Set() + const changed = new Map>>() + 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>() + 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 | 'unknown' | undefined { + return rowsBySnapshot.get(snapshot) +}