Files
openchamber/packages/ui/src/sync/session-prefetch-cache.ts
T
00821700de chore: remove dead code (59 unused files + ~125 unused exports) (#1835)
* chore: remove dead/unreferenced files across ui, vscode

Remove 59 unused source files (components, hooks, lib utils, stores,
barrels, and orphaned vscode github modules) that are not imported by
any entry-reachable code. Also drop a stale test mock for the removed
execCommands module.

Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>

* refactor: remove unused exported symbols (types, functions, consts, hooks)

Remove exported symbols whose identifier is referenced nowhere in the
repository (verified via repo-wide search), across ui types/contracts,
lib utilities, sync layer, stores, and components. Also drop the few
imports/private helpers orphaned by these removals.

Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>

* refactor: remove more unused exports (desktop, shortcuts, worktree, vscode)

Continue removing repo-wide unreferenced exported functions, consts and
types across lib/desktop, shortcuts, worktreeSessionCreator, sync, and
vscode gitService, with cascading orphaned helpers/imports cleaned up.

Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>

* chore: add dead-code cleanup tooling

* refactor: checkpoint dead-code cleanup

* refactor: remove dead-code suppressions

---------

Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>
Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
2026-06-26 19:27:53 +03:00

101 lines
2.7 KiB
TypeScript

/**
* Session prefetch TTL cache — prevents redundant session fetches
* within a short window. Port of OpenCode's session-prefetch.ts.
*
* Tracks: last fetch time, pagination cursor, completeness.
* Version counter invalidates stale inflight requests after eviction.
*/
const SESSION_PREFETCH_TTL = 15_000
type Meta = {
limit: number
cursor?: string
complete: boolean
at: number
}
const compositeKey = (directory: string, sessionID: string) =>
`${directory}\n${sessionID}`
const cache = new Map<string, Meta>()
const inflight = new Map<string, Promise<Meta | undefined>>()
const rev = new Map<string, number>()
const listeners = new Map<string, Set<() => void>>()
const version = (id: string) => rev.get(id) ?? 0
const notify = (id: string) => {
const callbacks = listeners.get(id)
if (!callbacks) return
callbacks.forEach((callback) => callback())
}
/** Check if a prefetch/sync can be skipped (recently fetched). */
export function shouldSkipSessionPrefetch(input: {
hasMessages: boolean
info?: Meta
pageSize: number
now?: number
}): boolean {
if (!input.hasMessages) {
return false
}
const info = input.info
if (!info) return true
if (info.complete) return true
if (info.limit > input.pageSize) return true
if (info.limit < input.pageSize) return false
return (input.now ?? Date.now()) - info.at < SESSION_PREFETCH_TTL
}
export function getSessionPrefetch(directory: string, sessionID: string): Meta | undefined {
return cache.get(compositeKey(directory, sessionID))
}
export function subscribeSessionPrefetch(directory: string, sessionID: string, callback: () => void) {
if (!sessionID) return () => undefined
const id = compositeKey(directory, sessionID)
let callbacks = listeners.get(id)
if (!callbacks) {
callbacks = new Set()
listeners.set(id, callbacks)
}
callbacks.add(callback)
return () => {
callbacks?.delete(callback)
if (callbacks?.size === 0) listeners.delete(id)
}
}
export function setSessionPrefetch(input: {
directory: string
sessionID: string
limit: number
cursor?: string
complete: boolean
at?: number
}) {
const id = compositeKey(input.directory, input.sessionID)
cache.set(id, {
limit: input.limit,
cursor: input.cursor,
complete: input.complete,
at: input.at ?? Date.now(),
})
notify(id)
}
/** Invalidate cache for specific sessions (e.g. after eviction). */
export function clearSessionPrefetch(directory: string, sessionIDs: Iterable<string>) {
for (const sessionID of sessionIDs) {
if (!sessionID) continue
const id = compositeKey(directory, sessionID)
rev.set(id, version(id) + 1)
cache.delete(id)
inflight.delete(id)
notify(id)
}
}