diff --git a/src/misc/sequentialize.ts b/src/misc/sequentialize.ts index 8b946b6547..46b665f64b 100644 --- a/src/misc/sequentialize.ts +++ b/src/misc/sequentialize.ts @@ -6,10 +6,11 @@ * * Results are not cached, once the promise has returned, the next call will result in a fresh call */ -export function sequentialize(key: (args: any[]) => string = JSON.stringify) { +export function sequentialize(cacheKey: (args: any[]) => string) { return (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => { const originalMethod: () => Promise = descriptor.value; const caches = new Map>>(); + const getCache = (obj: any) => { let cache = caches.get(obj); if (cache != null) { @@ -22,22 +23,22 @@ export function sequentialize(key: (args: any[]) => string = JSON.stringify) { return { value: function(...args: any[]) { - const argsKey = key(args); + const argsCacheKey = cacheKey(args); const cache = getCache(this); - let response = cache.get(argsKey); + let response = cache.get(argsCacheKey); if (response != null) { return response; } response = originalMethod.apply(this, args).then((val: any) => { - cache.delete(argsKey); + cache.delete(argsCacheKey); return val; }).catch((err: any) => { - cache.delete(argsKey); + cache.delete(argsCacheKey); throw err; }); - cache.set(argsKey, response); + cache.set(argsCacheKey, response); return response; }, }; diff --git a/src/services/crypto.service.ts b/src/services/crypto.service.ts index 5315621992..9cf4d673d8 100644 --- a/src/services/crypto.service.ts +++ b/src/services/crypto.service.ts @@ -104,7 +104,7 @@ export class CryptoService implements CryptoServiceAbstraction { return this.storageService.get(Keys.keyHash); } - @sequentialize() + @sequentialize(() => 'getEncKey') async getEncKey(): Promise { if (this.encKey != null) { return this.encKey; @@ -166,7 +166,7 @@ export class CryptoService implements CryptoServiceAbstraction { return this.privateKey; } - @sequentialize() + @sequentialize(() => 'getOrgKeys') async getOrgKeys(): Promise> { if (this.orgKeys != null && this.orgKeys.size > 0) { return this.orgKeys;