From 4d281ef4eaaf885eb930b296d0696e1a42e452dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Jir=C3=A9nius?= Date: Fri, 27 Mar 2020 13:51:05 +0100 Subject: [PATCH] GH-39: Fixed bug where evict timeout isn't reset on ResClient.get. --- src/class/CacheItem.js | 8 ++++++++ src/class/ResClient.js | 14 +++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/class/CacheItem.js b/src/class/CacheItem.js index a905f5a..ae25959 100644 --- a/src/class/CacheItem.js +++ b/src/class/CacheItem.js @@ -69,6 +69,14 @@ class CacheItem { } } + resetTimeout() { + if (this.unsubTimeout) { + clearTimeout(this.unsubTimeout); + this.unsubTimeout = null; + this._checkUnsubscribe(); + } + } + _checkUnsubscribe() { if (!this.subscribed || this.direct || this.unsubTimeout) { return; diff --git a/src/class/ResClient.js b/src/class/ResClient.js index d579036..818ec92 100644 --- a/src/class/ResClient.js +++ b/src/class/ResClient.js @@ -309,7 +309,11 @@ class ResClient { // Check for resource in cache let ci = this.cache[rid]; if (ci) { - return ci.promise ? ci.promise : Promise.resolve(ci.item); + if (ci.promise) { + return ci.promise; + } + ci.resetTimeout(); + return Promise.resolve(ci.item); } ci = new CacheItem(rid, this._unsubscribe); @@ -341,9 +345,9 @@ class ResClient { authenticate(rid, method, params) { return this._call('auth', rid, method, params); } - + /** - * Creates a new resource by calling the 'new' method. + * Creates a new resource by calling the 'new' method. * Use call with 'new' as method parameter instead. * @param {*} rid Resource ID * @param {*} params Method parameters @@ -381,7 +385,7 @@ class ResClient { resourceOn(rid, events, handler) { let cacheItem = this.cache[rid]; if (!cacheItem) { - throw new Error("Resource not found in cache"); + throw new Error("Resource " + rid + " not found in cache"); } cacheItem.addDirect(); @@ -391,7 +395,7 @@ class ResClient { resourceOff(rid, events, handler) { let cacheItem = this.cache[rid]; if (!cacheItem) { - throw new Error("Resource not found in cache"); + throw new Error("Resource " + rid + " not found in cache"); } cacheItem.removeDirect();