Clear sequentialize cache when empty to remove chance of memory leaks (#26)

This commit is contained in:
Fred Cox 2019-02-02 16:23:16 +02:00 committed by Kyle Spearrin
parent 9e97b1e656
commit e7f4dccfc3
2 changed files with 10 additions and 11 deletions

View File

@ -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(() => {

View File

@ -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,19 +25,19 @@ 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) => {
response = originalMethod.apply(this, args)
.finally(() => {
cache.delete(argsCacheKey);
return val;
}).catch((err: any) => {
cache.delete(argsCacheKey);
throw err;
if (cache.size === 0) {
caches.delete(this);
}
});
cache.set(argsCacheKey, response);