From 905672924b394cc6a4ad3116ecc07483548e4d59 Mon Sep 17 00:00:00 2001 From: QQ Date: Tue, 17 Nov 2026 08:30:38 +0800 Subject: [PATCH] Import apps/android/src/services/compaction-store.ts --- apps/android/src/services/compaction-store.ts | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 apps/android/src/services/compaction-store.ts diff --git a/apps/android/src/services/compaction-store.ts b/apps/android/src/services/compaction-store.ts new file mode 100644 index 0000000..a3c17d5 --- /dev/null +++ b/apps/android/src/services/compaction-store.ts @@ -0,0 +1,94 @@ +import type { StoredCompactionRecord } from './storage' + +type CompactionState = + | 'accepted' + | 'running' + | 'cancelling' + | 'succeeded' + | 'failed' + | 'cancelled' + | 'interrupted' + | 'unconfirmed' + +interface CompactionRecord { + sessionId: string + requestId: string + operationId?: string + state: CompactionState + updatedAt: number +} + +type Scopes = Record> + +const MAX_RECORDS = 32 +const MAX_RECORDS_PER_SCOPE = 8 + +let writeQueue: Promise = Promise.resolve() + +/** + * Load the SecureStore-backed module lazily. `storage.ts` pulls Expo native + * modules that unit tests do not compose; persistence failures must only make + * tracking unavailable, never break the connection or a compaction. + */ +async function persistence() { + return await import('./storage') +} + +function serialize(work: (scopes: Scopes) => T): Promise { + const run = writeQueue.then(async () => { + const { loadCompactionRecords, saveCompactionRecords } = await persistence() + const scopes = await loadCompactionRecords() as unknown as Scopes + const result = work(scopes) + await saveCompactionRecords(scopes as unknown as Record>) + return result + }) + writeQueue = run.then(() => undefined, () => undefined) + return run +} + +function prune(scopes: Scopes): void { + for (const [scope, records] of Object.entries(scopes)) { + const entries = Object.entries(records).sort((left, right) => right[1].updatedAt - left[1].updatedAt) + for (const [sessionId] of entries.slice(MAX_RECORDS_PER_SCOPE)) delete records[sessionId] + if (Object.keys(records).length === 0) delete scopes[scope] + } + const all = Object.values(scopes).flatMap(records => Object.values(records)) + if (all.length <= MAX_RECORDS) return + const keep = new Set(all.sort((left, right) => right.updatedAt - left.updatedAt).slice(0, MAX_RECORDS)) + for (const [scope, records] of Object.entries(scopes)) { + for (const [sessionId, record] of Object.entries(records)) { + if (!keep.has(record)) delete records[sessionId] + } + if (Object.keys(records).length === 0) delete scopes[scope] + } +} + +/** Product-side store for Host-owned compaction attempts (scoped per Host). */ +export const compactionStore = { + async load(scope: string, sessionId: string): Promise { + const { loadCompactionRecords } = await persistence() + const scopes = await loadCompactionRecords() as unknown as Scopes + return scopes[scope]?.[sessionId] + }, + async list(scope: string): Promise { + const { loadCompactionRecords } = await persistence() + const scopes = await loadCompactionRecords() as unknown as Scopes + return Object.values(scopes[scope] ?? {}) + }, + async save(scope: string, record: CompactionRecord): Promise { + await serialize(scopes => { + const records = scopes[scope] ?? {} + records[record.sessionId] = record + scopes[scope] = records + prune(scopes) + }) + }, + async remove(scope: string, sessionId: string): Promise { + await serialize(scopes => { + const records = scopes[scope] + if (records === undefined) return + delete records[sessionId] + if (Object.keys(records).length === 0) delete scopes[scope] + }) + }, +}