mirror of
https://github.com/bitwarden/browser
synced 2025-12-14 15:23:33 +00:00
sequentialize updates
This commit is contained in:
@@ -6,10 +6,11 @@
|
|||||||
*
|
*
|
||||||
* Results are not cached, once the promise has returned, the next call will result in a fresh call
|
* 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) => {
|
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 != null) {
|
if (cache != null) {
|
||||||
@@ -22,22 +23,22 @@ export function sequentialize(key: (args: any[]) => string = JSON.stringify) {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
value: function(...args: any[]) {
|
value: function(...args: any[]) {
|
||||||
const argsKey = key(args);
|
const argsCacheKey = cacheKey(args);
|
||||||
const cache = getCache(this);
|
const cache = getCache(this);
|
||||||
let response = cache.get(argsKey);
|
let response = cache.get(argsCacheKey);
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
response = originalMethod.apply(this, args).then((val: any) => {
|
response = originalMethod.apply(this, args).then((val: any) => {
|
||||||
cache.delete(argsKey);
|
cache.delete(argsCacheKey);
|
||||||
return val;
|
return val;
|
||||||
}).catch((err: any) => {
|
}).catch((err: any) => {
|
||||||
cache.delete(argsKey);
|
cache.delete(argsCacheKey);
|
||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
|
|
||||||
cache.set(argsKey, response);
|
cache.set(argsCacheKey, response);
|
||||||
return response;
|
return response;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
return this.storageService.get<string>(Keys.keyHash);
|
return this.storageService.get<string>(Keys.keyHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
@sequentialize()
|
@sequentialize(() => 'getEncKey')
|
||||||
async getEncKey(): Promise<SymmetricCryptoKey> {
|
async getEncKey(): Promise<SymmetricCryptoKey> {
|
||||||
if (this.encKey != null) {
|
if (this.encKey != null) {
|
||||||
return this.encKey;
|
return this.encKey;
|
||||||
@@ -166,7 +166,7 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
return this.privateKey;
|
return this.privateKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
@sequentialize()
|
@sequentialize(() => 'getOrgKeys')
|
||||||
async getOrgKeys(): Promise<Map<string, SymmetricCryptoKey>> {
|
async getOrgKeys(): Promise<Map<string, SymmetricCryptoKey>> {
|
||||||
if (this.orgKeys != null && this.orgKeys.size > 0) {
|
if (this.orgKeys != null && this.orgKeys.size > 0) {
|
||||||
return this.orgKeys;
|
return this.orgKeys;
|
||||||
|
|||||||
Reference in New Issue
Block a user