From 61d2040518a02ddb9b8edfb2df6e13db186b1ef7 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 23 Jul 2018 15:12:32 -0400 Subject: [PATCH] sequentialize updates --- src/misc/sequentialize.ts | 13 +++++++------ src/services/crypto.service.ts | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) 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;