Skip to content

Commit

Permalink
Merge branch 'release/v2.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Oct 31, 2022
2 parents 7012735 + 9f1fd90 commit 8156868
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
1 change: 1 addition & 0 deletions docs/docs.md
Expand Up @@ -70,6 +70,7 @@ Creates a ResClient instance
| [opt] | <code>object</code> | Optional parameters. |
| [opt.onConnect] | <code>function</code> | On connect callback called prior resolving the connect promise and subscribing to stale resources. May return a promise. |
| [opt.namespace] | <code>string</code> | Event bus namespace. Defaults to 'resclient'. |
| [opt.debug] | <code>bool</code> | Flag to debug log all WebSocket communication. Defaults to false. |
| [opt.eventBus] | <code>module:modapp~EventBus</code> | Event bus. |

<a name="ResClient+supportedProtocol"></a>
Expand Down
16 changes: 8 additions & 8 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "resclient",
"version": "2.3.3",
"version": "2.4.0",
"description": "Resgate client implementing the RES-Client Protocol.",
"main": "lib/index.js",
"types": "types/index.d.ts",
Expand Down Expand Up @@ -38,9 +38,9 @@
"license": "MIT",
"homepage": "https://github.com/resgateio/resclient",
"devDependencies": {
"@babel/cli": "^7.12.1",
"@babel/core": "^7.12.3",
"@babel/preset-env": "^7.12.1",
"@babel/cli": "^7.19.3",
"@babel/core": "^7.19.6",
"@babel/preset-env": "^7.19.4",
"@rollup/plugin-babel": "^5.2.1",
"@rollup/plugin-node-resolve": "^8.4.0",
"babel-jest": "^26.6.3",
Expand All @@ -51,12 +51,12 @@
"jsdoc-to-markdown": "^6.0.1",
"mock-socket": "^7.1.0",
"rimraf": "^3.0.2",
"rollup": "^2.33.1",
"rollup": "^2.79.1",
"rollup-plugin-terser": "^6.1.0",
"typescript": "^4.4.4"
"typescript": "^4.8.4"
},
"dependencies": {
"modapp-eventbus": "^1.7.0",
"modapp-utils": "^1.7.0"
"modapp-eventbus": "^1.8.0",
"modapp-utils": "^1.8.0"
}
}
61 changes: 45 additions & 16 deletions src/class/ResClient.js
Expand Up @@ -96,6 +96,7 @@ class ResClient {
* @param {object} [opt] Optional parameters.
* @param {function} [opt.onConnect] On connect callback called prior resolving the connect promise and subscribing to stale resources. May return a promise.
* @param {string} [opt.namespace] Event bus namespace. Defaults to 'resclient'.
* @param {bool} [opt.debug] Flag to debug log all WebSocket communication. Defaults to false.
* @param {module:modapp~EventBus} [opt.eventBus] Event bus.
*/
constructor(hostUrlOrFactory, opt) {
Expand All @@ -109,6 +110,7 @@ class ResClient {
obj.update(this, opt, {
onConnect: { type: '?function' },
namespace: { type: 'string', default: defaultNamespace },
debug: { type: 'boolean', default: false },
eventBus: { type: 'object', default: eventBus }
});

Expand Down Expand Up @@ -177,16 +179,20 @@ class ResClient {
*/
connect() {
this.tryConnect = true;
if (!this.connectPromise) {
this.connectPromise = new Promise((resolve, reject) => {
this.connectCallback = { resolve, reject };
this.ws = this.wsFactory();

this.ws.onopen = this._handleOnopen;
this.ws.onerror = this._handleOnerror;
this.ws.onmessage = this._handleOnmessage;
this.ws.onclose = this._handleOnclose;
});
this.connectPromise.catch(err => this._emit('connectError', err));
}

return this.connectPromise = this.connectPromise || new Promise((resolve, reject) => {
this.connectCallback = { resolve, reject };
this.ws = this.wsFactory();

this.ws.onopen = this._handleOnopen;
this.ws.onerror = this._handleOnerror;
this.ws.onmessage = this._handleOnmessage;
this.ws.onclose = this._handleOnclose;
});
return this.connectPromise;
}

/**
Expand Down Expand Up @@ -449,6 +455,9 @@ class ResClient {
};

var json = JSON.stringify(req);
if (this.debug) {
console.debug("<== " + req.id + ":" + json);
}
this.ws.send(json);
});
}
Expand All @@ -462,6 +471,9 @@ class ResClient {
let data = JSON.parse(json.trim());

if (data.hasOwnProperty('id')) {
if (this.debug) {
console.debug("==> " + data.id + ":" + json);
}

// Find the stored request
let req = this.requests[data.id];
Expand All @@ -477,6 +489,9 @@ class ResClient {
this._handleSuccessResponse(req, data);
}
} else if (data.hasOwnProperty('event')) {
if (this.debug) {
console.debug("--> " + json);
}
this._handleEvent(data);
} else {
throw new Error("Invalid message from server: " + json);
Expand Down Expand Up @@ -728,6 +743,9 @@ class ResClient {
* @private
*/
_handleOnopen(e) {
if (this.debug) {
console.debug("ResClient open", e, this);
}
this._sendNow('version', { protocol: this.supportedProtocol })
.then(ver => {
this.protocol = versionToInt(ver.protocol) || legacyProtocol;
Expand Down Expand Up @@ -768,6 +786,9 @@ class ResClient {
* @private
*/
_handleOnerror(e) {
if (this.debug) {
console.debug("ResClient error", e, this);
}
this._connectReject({ code: 'system.connectionError', message: "Connection error", data: e });
}

Expand All @@ -786,8 +807,12 @@ class ResClient {
* @private
*/
_handleOnclose(e) {
if (this.debug) {
console.debug("ResClient close", e, this);
}
this.connectPromise = null;
this.ws = null;
let wasConnected = this.connected;
if (this.connected) {
this.connected = false;

Expand All @@ -813,7 +838,7 @@ class ResClient {
this.tryConnect = hasStale && this.tryConnect;

if (this.tryConnect) {
this._reconnect();
this._reconnect(wasConnected);
}
}

Expand Down Expand Up @@ -915,13 +940,13 @@ class ResClient {

let rid = ci.rid;
let r = refs[rid];
if (!r) {
refs[rid] = { ci, rc: ci.indirect - 1, st: stateNone };
return true;
if (r) {
r.rc--;
return false;
}

r.rc--;
return false;
refs[rid] = { ci, rc: ci.indirect - 1, st: stateNone };
return true;
}

/**
Expand Down Expand Up @@ -1243,7 +1268,11 @@ class ResClient {
this._tryDelete(cacheItem);
}

_reconnect() {
_reconnect(noDelay) {
if (noDelay) {
this.connect();
return;
}
setTimeout(() => {
if (!this.tryConnect) {
return;
Expand Down

0 comments on commit 8156868

Please sign in to comment.