Skip to content

Commit

Permalink
Merge pull request #40 from resgateio/bugfix/gh-39-resource-not-found…
Browse files Browse the repository at this point in the history
…-in-cache

GH-39: Fixed bug where evict timeout isn't reset on ResClient.get.
  • Loading branch information
jirenius committed Mar 27, 2020
2 parents b0fb9a5 + 4d281ef commit a18b672
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/class/CacheItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 9 additions & 5 deletions src/class/ResClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit a18b672

Please sign in to comment.