1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-05 02:39:33 +02:00

server/server -> server/core

This commit is contained in:
Chocobozzz 2023-10-04 15:13:25 +02:00
parent 114327d4ce
commit 5a3d0650c9
No known key found for this signature in database
GPG key ID: 583A612D890159BE
838 changed files with 111 additions and 111 deletions

View file

@ -0,0 +1,39 @@
export class CachePromiseFactory <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) {
return this.runWithContext(null, arg)
}
runWithContext (ctx: any, arg: A) {
const key = this.keyBuilder(arg)
if (this.running.has(key)) return this.running.get(key)
const p = this.fn.apply(ctx || this, [ arg ])
this.running.set(key, p)
return p.finally(() => this.running.delete(key))
}
}
export function CachePromise (options: {
keyBuilder: (...args: any[]) => string
}) {
return function (_target, _key, descriptor: PropertyDescriptor) {
const promiseCache = new CachePromiseFactory(descriptor.value, options.keyBuilder)
descriptor.value = function () {
if (arguments.length !== 1) throw new Error('Cache promise only support methods with 1 argument')
return promiseCache.runWithContext(this, arguments[0])
}
}
}