Skip to content

Commit

Permalink
GH-39: Calling off on ResModel or ResCollection now only logs warning…
Browse files Browse the repository at this point in the history
… if resource is not in cache, but still removes listener from eventBus.

Added debug trace logging.
  • Loading branch information
jirenius committed Mar 31, 2021
1 parent 7144c8a commit 2ca2e50
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
37 changes: 27 additions & 10 deletions src/class/ResClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,24 +385,36 @@ class ResClient {
return this._send('call', modelId, 'set', props);
}

resourceOn(rid, events, handler) {
resourceOn(rid, events, handler, item) {
let cacheItem = this.cache[rid];
if (!cacheItem) {
throw new Error("Resource " + rid + " not found in cache");
if (cacheItem) {
if (!item || cacheItem.item === item) {
cacheItem.addDirect();
}
item = cacheItem.item;
} else {
console.warn("Resource " + rid + " not found in cache");
}

cacheItem.addDirect();
this.eventBus.on(cacheItem.item, events, handler, this.namespace + '.resource.' + rid);
if (item) {
this.eventBus.on(item, events, handler, this.namespace + '.resource.' + rid);
}
}

resourceOff(rid, events, handler) {
resourceOff(rid, events, handler, item) {
let cacheItem = this.cache[rid];
if (!cacheItem) {
throw new Error("Resource " + rid + " not found in cache");
if (cacheItem) {
if (!item || cacheItem.item === item) {
cacheItem.removeDirect();
}
item = cacheItem.item;
} else {
console.warn("Resource " + rid + " not found in cache");
}

cacheItem.removeDirect();
this.eventBus.off(cacheItem.item, events, handler, this.namespace + '.resource.' + rid);
if (item) {
this.eventBus.off(item, events, handler, this.namespace + '.resource.' + rid);
}
}

/**
Expand Down Expand Up @@ -446,6 +458,7 @@ class ResClient {

var json = JSON.stringify(req);
this.ws.send(json);
console.debug("RC > ", json);
});
}

Expand Down Expand Up @@ -724,6 +737,7 @@ class ResClient {
* @private
*/
_handleOnopen(e) {
console.debug("RC : open");
this._sendNow('version', { protocol: this.supportedProtocol })
.then(ver => {
this.protocol = versionToInt(ver.protocol) || legacyProtocol;
Expand Down Expand Up @@ -764,6 +778,7 @@ class ResClient {
* @private
*/
_handleOnerror(e) {
console.debug("RC error: ", e);
this._connectReject({ code: 'system.connectionError', message: "Connection error", data: e });
}

Expand All @@ -773,6 +788,7 @@ class ResClient {
* @private
*/
_handleOnmessage(e) {
console.debug("RC < ", e.data);
this._receive(e.data);
}

Expand All @@ -782,6 +798,7 @@ class ResClient {
* @private
*/
_handleOnclose(e) {
console.debug("RC close: ", e);
this.connectPromise = null;
this.ws = null;
if (this.connected) {
Expand Down
4 changes: 2 additions & 2 deletions src/class/ResCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class ResCollection {
* @returns {this}
*/
on(events, handler) {
this._api.resourceOn(this._rid, events, handler);
this._api.resourceOn(this._rid, events, handler, this);
return this;
}

Expand All @@ -103,7 +103,7 @@ class ResCollection {
* @returns {this}
*/
off(events, handler) {
this._api.resourceOff(this._rid, events, handler);
this._api.resourceOff(this._rid, events, handler, this);
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions src/class/ResModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ResModel {
* @returns {this}
*/
on(events, handler) {
this._api.resourceOn(this._rid, events, handler);
this._api.resourceOn(this._rid, events, handler, this);
return this;
}

Expand All @@ -79,7 +79,7 @@ class ResModel {
* @returns {this}
*/
off(events, handler) {
this._api.resourceOff(this._rid, events, handler);
this._api.resourceOff(this._rid, events, handler, this);
return this;
}

Expand Down

0 comments on commit 2ca2e50

Please sign in to comment.