aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/promise-cache.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-09 13:34:40 +0200
committerChocobozzz <me@florianbigard.com>2021-06-09 13:34:40 +0200
commit4ead40e7766f5964ecd9a11766ff56b95090fe1c (patch)
treef1fd6b2e1604c4025eae86fb87cf81daf609850f /server/helpers/promise-cache.ts
parenta6a12dae10658a1fa7120ad39702c07137143954 (diff)
downloadPeerTube-4ead40e7766f5964ecd9a11766ff56b95090fe1c.tar.gz
PeerTube-4ead40e7766f5964ecd9a11766ff56b95090fe1c.tar.zst
PeerTube-4ead40e7766f5964ecd9a11766ff56b95090fe1c.zip
Cache refresh actor promise
Diffstat (limited to 'server/helpers/promise-cache.ts')
-rw-r--r--server/helpers/promise-cache.ts21
1 files changed, 21 insertions, 0 deletions
diff --git a/server/helpers/promise-cache.ts b/server/helpers/promise-cache.ts
new file mode 100644
index 000000000..07e8a9962
--- /dev/null
+++ b/server/helpers/promise-cache.ts
@@ -0,0 +1,21 @@
1export class PromiseCache <A, R> {
2 private readonly running = new Map<string, Promise<R>>()
3
4 constructor (
5 private readonly fn: (arg: A) => Promise<R>,
6 private readonly keyBuilder: (arg: A) => string
7 ) {
8 }
9
10 run (arg: A) {
11 const key = this.keyBuilder(arg)
12
13 if (this.running.has(key)) return this.running.get(key)
14
15 const p = this.fn(arg)
16
17 this.running.set(key, p)
18
19 return p.finally(() => this.running.delete(key))
20 }
21}