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:
parent
a6a12dae10
commit
4ead40e776
6 changed files with 49 additions and 15 deletions
21
server/helpers/promise-cache.ts
Normal file
21
server/helpers/promise-cache.ts
Normal 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))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue