mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-27 12:36:14 +01:00
Clear sequentialize cache when empty to remove chance of memory leaks (#26)
This commit is contained in:
parent
9e97b1e656
commit
e7f4dccfc3
@ -120,7 +120,7 @@ class Foo {
|
||||
calls = 0;
|
||||
|
||||
@sequentialize((args) => 'bar' + args[0])
|
||||
bar(a: any) {
|
||||
bar(a: number) {
|
||||
this.calls++;
|
||||
return new Promise((res) => {
|
||||
setTimeout(() => {
|
||||
@ -130,7 +130,7 @@ class Foo {
|
||||
}
|
||||
|
||||
@sequentialize((args) => 'baz' + args[0])
|
||||
baz(a: any) {
|
||||
baz(a: number) {
|
||||
this.calls++;
|
||||
return new Promise((res) => {
|
||||
setTimeout(() => {
|
||||
|
@ -6,7 +6,6 @@
|
||||
*
|
||||
* Results are not cached, once the promise has returned, the next call will result in a fresh call
|
||||
*
|
||||
* WARNING: The decorator's scope is singleton, so using it on transient objects can lead to memory leaks.
|
||||
* Read more at https://github.com/bitwarden/jslib/pull/7
|
||||
*/
|
||||
export function sequentialize(cacheKey: (args: any[]) => string) {
|
||||
@ -26,20 +25,20 @@ export function sequentialize(cacheKey: (args: any[]) => string) {
|
||||
|
||||
return {
|
||||
value: function(...args: any[]) {
|
||||
const argsCacheKey = cacheKey(args);
|
||||
const cache = getCache(this);
|
||||
const argsCacheKey = cacheKey(args);
|
||||
let response = cache.get(argsCacheKey);
|
||||
if (response != null) {
|
||||
return response;
|
||||
}
|
||||
|
||||
response = originalMethod.apply(this, args).then((val: any) => {
|
||||
cache.delete(argsCacheKey);
|
||||
return val;
|
||||
}).catch((err: any) => {
|
||||
cache.delete(argsCacheKey);
|
||||
throw err;
|
||||
});
|
||||
response = originalMethod.apply(this, args)
|
||||
.finally(() => {
|
||||
cache.delete(argsCacheKey);
|
||||
if (cache.size === 0) {
|
||||
caches.delete(this);
|
||||
}
|
||||
});
|
||||
|
||||
cache.set(argsCacheKey, response);
|
||||
return response;
|
||||
|
Loading…
Reference in New Issue
Block a user