1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-04 10:19:35 +02:00

Cache refresh actor promise

This commit is contained in:
Chocobozzz 2021-06-09 13:34:40 +02:00
parent a6a12dae10
commit 4ead40e776
No known key found for this signature in database
GPG key ID: 583A612D890159BE
6 changed files with 49 additions and 15 deletions

View file

@ -0,0 +1,21 @@
export class PromiseCache <A, R> {
private readonly running = new Map<string, Promise<R>>()
constructor (
private readonly fn: (arg: A) => Promise<R>,
private readonly keyBuilder: (arg: A) => string
) {
}
run (arg: A) {
const key = this.keyBuilder(arg)
if (this.running.has(key)) return this.running.get(key)
const p = this.fn(arg)
this.running.set(key, p)
return p.finally(() => this.running.delete(key))
}
}