mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 08:13:42 +00:00
sequentialize updates
This commit is contained in:
@@ -9,42 +9,35 @@
|
||||
export function sequentialize(key: (args: any[]) => string = JSON.stringify) {
|
||||
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) {
|
||||
if (cache != null) {
|
||||
return cache;
|
||||
}
|
||||
cache = new Map<string, Promise<any>>();
|
||||
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;
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user