fix: change store apis to async (#37329)

This commit is contained in:
Yunlu Wen authored and GitHub committed 2026-06-15 02:24:51 +00:00
1 parent e0773c4d8f
commit d315ae3b80
48 files changed
+398 -379

No files matched your search

+7 -7
View File
@@ -42,17 +42,17 @@ export type AppInfoCacheOptions = {
export async function loadAppInfoCache(opts: AppInfoCacheOptions = {}): Promise<AppInfoCache> {
const store = opts.store ?? getCache(CACHE_APP_INFO)
const ttlMs = opts.ttlMs ?? APP_INFO_TTL_MS
const state: State = { entries: readEntries(store) }
const state: State = { entries: await readEntries(store) }
return {
get: (host, appId) => state.entries.get(key(host, appId)),
set: async (host, appId, meta) => {
const record: AppMetaCacheRecord = { meta, fetchedAt: (opts.now ?? (() => new Date()))().toISOString() }
state.entries.set(key(host, appId), record)
writeEntries(store, state.entries)
await writeEntries(store, state.entries)
},
delete: async (host, appId) => {
state.entries.delete(key(host, appId))
writeEntries(store, state.entries)
await writeEntries(store, state.entries)
},
isFresh: (record, now) => {
const t = (now ?? new Date()).getTime() - new Date(record.fetchedAt).getTime()
@@ -65,11 +65,11 @@ function key(host: string, appId: string): string {
return `${host}::${appId}`
}
function readEntries(store: Store): Map<string, AppMetaCacheRecord> {
async function readEntries(store: Store): Promise<Map<string, AppMetaCacheRecord>> {
const out = new Map<string, AppMetaCacheRecord>()
let raw: Record<string, DiskEntry>
try {
raw = store.get(ENTRIES_KEY)
raw = await store.get(ENTRIES_KEY)
}
catch {
return out
@@ -111,8 +111,8 @@ function serialize(record: AppMetaCacheRecord): DiskEntry {
}
}
function writeEntries(store: Store, entries: Map<string, AppMetaCacheRecord>): void {
async function writeEntries(store: Store, entries: Map<string, AppMetaCacheRecord>): Promise<void> {
const out: Record<string, DiskEntry> = {}
for (const [k, v] of entries) out[k] = serialize(v)
store.set(ENTRIES_KEY, out)
await store.set(ENTRIES_KEY, out)
}
+7 -7
View File
@@ -23,7 +23,7 @@ export async function loadNudgeStore(opts: NudgeStoreOptions = {}): Promise<Nudg
const store = opts.store ?? getCache(CACHE_NUDGE)
const intervalMs = opts.intervalMs ?? WARN_INTERVAL_MS
const clock = opts.now ?? (() => new Date())
const memory = readWarned(store)
const memory = await readWarned(store)
return {
canWarn: (host, now) => {
@@ -39,18 +39,18 @@ export async function loadNudgeStore(opts: NudgeStoreOptions = {}): Promise<Nudg
// Re-read disk inside the write cycle so concurrent processes touching
// different hosts don't clobber each other's stamps. Same-host writers
// converge on a near-identical timestamp, so order doesn't matter.
const onDisk = readWarned(store)
const onDisk = await readWarned(store)
onDisk.set(host, stamp)
writeWarned(store, onDisk)
await writeWarned(store, onDisk)
},
}
}
function readWarned(store: Store): Map<string, number> {
async function readWarned(store: Store): Promise<Map<string, number>> {
const out = new Map<string, number>()
let raw: Record<string, string>
try {
raw = store.get(WARNED_KEY)
raw = await store.get(WARNED_KEY)
}
catch {
return out
@@ -63,9 +63,9 @@ function readWarned(store: Store): Map<string, number> {
return out
}
function writeWarned(store: Store, state: Map<string, number>): void {
async function writeWarned(store: Store, state: Map<string, number>): Promise<void> {
const warned: Record<string, string> = {}
for (const [host, t] of state)
warned[host] = new Date(t).toISOString()
store.set(WARNED_KEY, warned)
await store.set(WARNED_KEY, warned)
}