33 lines
898 B
TypeScript
33 lines
898 B
TypeScript
import type { RequiredAfterSearchRates } from "./types";
|
|
|
|
interface SuccessCacheEntry {
|
|
rates: RequiredAfterSearchRates;
|
|
updatedAt: number;
|
|
}
|
|
|
|
export function createMarketCacheStore() {
|
|
const successCache = new Map<string, SuccessCacheEntry>();
|
|
const inflightRequests = new Map<string, Promise<unknown>>();
|
|
|
|
return {
|
|
clearInflight(authorId: string) {
|
|
inflightRequests.delete(authorId);
|
|
},
|
|
getInflight<T>(authorId: string) {
|
|
return inflightRequests.get(authorId) as Promise<T> | undefined;
|
|
},
|
|
getSuccess(authorId: string) {
|
|
return successCache.get(authorId);
|
|
},
|
|
setInflight<T>(authorId: string, promise: Promise<T>) {
|
|
inflightRequests.set(authorId, promise);
|
|
},
|
|
setSuccess(authorId: string, rates: RequiredAfterSearchRates) {
|
|
successCache.set(authorId, {
|
|
rates,
|
|
updatedAt: Date.now()
|
|
});
|
|
}
|
|
};
|
|
}
|