1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-24 12:06:15 +01:00

sequentialize updates

This commit is contained in:
Kyle Spearrin 2018-07-23 14:42:37 -04:00
parent c7e8f1d13f
commit 003c730eb1
3 changed files with 11 additions and 19 deletions

View File

@ -9,42 +9,35 @@
export function sequentialize(key: (args: any[]) => string = JSON.stringify) { export function sequentialize(key: (args: any[]) => string = JSON.stringify) {
return (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => { return (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
const originalMethod: () => Promise<any> = descriptor.value; const originalMethod: () => Promise<any> = descriptor.value;
const caches = new Map<any, Map<string, Promise<any>>>(); const caches = new Map<any, Map<string, Promise<any>>>();
const getCache = (obj: any) => { const getCache = (obj: any) => {
let cache = caches.get(obj); let cache = caches.get(obj);
if (cache) { if (cache != null) {
return cache; return cache;
} }
cache = new Map<string, Promise<any>>(); cache = new Map<string, Promise<any>>();
caches.set(obj, cache); caches.set(obj, cache);
return cache; return cache;
}; };
return { return {
value: function(...args: any[]) { value: (...args: any[]) => {
const argsKey = key(args); const argsKey = key(args);
const cache = getCache(this); const cache = getCache(this);
let res = cache.get(argsKey); let res = cache.get(argsKey);
if (res) { if (res != null) {
return res; return res;
} }
res = originalMethod.apply(this, args) res = originalMethod.apply(this, args).then((val: any) => {
.then((val: any) => { cache.delete(argsKey);
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); cache.set(argsKey, res);
return res; return res;
}, },
}; };

View File

@ -2,7 +2,6 @@ import { EncryptionType } from '../../enums/encryptionType';
import { CryptoService } from '../../abstractions/crypto.service'; import { CryptoService } from '../../abstractions/crypto.service';
import { sequentialize } from '../../misc/sequentialize';
import { Utils } from '../../misc/utils'; import { Utils } from '../../misc/utils';
export class CipherString { export class CipherString {
@ -90,7 +89,6 @@ export class CipherString {
} }
} }
@sequentialize((args) => args[0])
async decrypt(orgId: string): Promise<string> { async decrypt(orgId: string): Promise<string> {
if (this.decryptedValue) { if (this.decryptedValue) {
return Promise.resolve(this.decryptedValue); return Promise.resolve(this.decryptedValue);

View File

@ -104,6 +104,7 @@ export class CryptoService implements CryptoServiceAbstraction {
return this.storageService.get<string>(Keys.keyHash); return this.storageService.get<string>(Keys.keyHash);
} }
@sequentialize()
async getEncKey(): Promise<SymmetricCryptoKey> { async getEncKey(): Promise<SymmetricCryptoKey> {
if (this.encKey != null) { if (this.encKey != null) {
return this.encKey; return this.encKey;