sequentialize updates

This commit is contained in:
Kyle Spearrin 2018-07-23 15:12:32 -04:00
parent 8e586437e0
commit 61d2040518
2 changed files with 9 additions and 8 deletions

View File

@ -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<any> = descriptor.value;
const caches = new Map<any, Map<string, Promise<any>>>();
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;
},
};

View File

@ -104,7 +104,7 @@ export class CryptoService implements CryptoServiceAbstraction {
return this.storageService.get<string>(Keys.keyHash);
}
@sequentialize()
@sequentialize(() => 'getEncKey')
async getEncKey(): Promise<SymmetricCryptoKey> {
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<Map<string, SymmetricCryptoKey>> {
if (this.orgKeys != null && this.orgKeys.size > 0) {
return this.orgKeys;