From 003c730eb1f959d30d1b83a5101a1c0530d9f369 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 23 Jul 2018 14:42:37 -0400 Subject: [PATCH] sequentialize updates --- src/misc/sequentialize.ts | 27 ++++++++++----------------- src/models/domain/cipherString.ts | 2 -- src/services/crypto.service.ts | 1 + 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/misc/sequentialize.ts b/src/misc/sequentialize.ts index 221cadd1180..10925c59519 100644 --- a/src/misc/sequentialize.ts +++ b/src/misc/sequentialize.ts @@ -9,42 +9,35 @@ export function sequentialize(key: (args: any[]) => string = JSON.stringify) { 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) { + if (cache != null) { return cache; } cache = new Map>(); caches.set(obj, cache); - return cache; }; return { - value: function(...args: any[]) { + value: (...args: any[]) => { const argsKey = key(args); const cache = getCache(this); - let res = cache.get(argsKey); - if (res) { + if (res != null) { return res; } - res = originalMethod.apply(this, args) - .then((val: any) => { - cache.delete(argsKey); + res = originalMethod.apply(this, args).then((val: any) => { + cache.delete(argsKey); + return val; + }).catch((err: any) => { + cache.delete(argsKey); + throw err; + }); - return val; - }) - .catch((err: any) => { - cache.delete(argsKey); - - throw err; - }); cache.set(argsKey, res); - return res; }, }; diff --git a/src/models/domain/cipherString.ts b/src/models/domain/cipherString.ts index fdc4c9098c2..6d49d1724ea 100644 --- a/src/models/domain/cipherString.ts +++ b/src/models/domain/cipherString.ts @@ -2,7 +2,6 @@ import { EncryptionType } from '../../enums/encryptionType'; import { CryptoService } from '../../abstractions/crypto.service'; -import { sequentialize } from '../../misc/sequentialize'; import { Utils } from '../../misc/utils'; export class CipherString { @@ -90,7 +89,6 @@ export class CipherString { } } - @sequentialize((args) => args[0]) async decrypt(orgId: string): Promise { if (this.decryptedValue) { return Promise.resolve(this.decryptedValue); diff --git a/src/services/crypto.service.ts b/src/services/crypto.service.ts index 54adfc7945d..53156219922 100644 --- a/src/services/crypto.service.ts +++ b/src/services/crypto.service.ts @@ -104,6 +104,7 @@ export class CryptoService implements CryptoServiceAbstraction { return this.storageService.get(Keys.keyHash); } + @sequentialize() async getEncKey(): Promise { if (this.encKey != null) { return this.encKey;