import { throttleFunction } from "socket-function/src/misc"; import { IStorage } from "./IStorage"; import { setPending } from "./PendingManager"; export class PendingStorage implements IStorage { pending = new Map(); constructor( private pendingGroup: string, private storage: IStorage, ) { } public async get(key: string): Promise { return this.watchPending("get", this.storage.get(key)); } public async set(key: string, value: T): Promise { return this.watchPending("set", this.storage.set(key, value)); } public async remove(key: string): Promise { return this.watchPending("remove", this.storage.remove(key)); } public async getKeys(): Promise { return this.watchPending("getKeys", this.storage.getKeys()); } public async getInfo(key: string) { return this.watchPending("getInfo", this.storage.getInfo(key)); } private watchPending(type: string, promise: Promise): Promise { this.pending.set(type, (this.pending.get(type) || 0) + 1); void this.updatePending(); void promise.finally(() => { this.pending.set(type, (this.pending.get(type) || 0) - 1); if (this.pending.get(type) === 0) { this.pending.delete(type); } void this.updatePending(); }); return promise; } private updatePending = throttleFunction(100, () => { let text = Array.from(this.pending.entries()).map(([key, value]) => `${key}: ${value}`).join(", "); setPending(this.pendingGroup, text); }); public async reset() { return this.storage.reset(); } public watchResync(callback: () => void): void { this.storage.watchResync?.(callback); } }