@noveo/dual-rpc-ws
Advanced tools
Comparing version 0.0.22 to 0.0.24
@@ -22,2 +22,3 @@ export declare type Id = string; | ||
result?: boolean; | ||
message?: string; | ||
}; | ||
@@ -24,0 +25,0 @@ } |
@@ -14,2 +14,5 @@ export declare namespace Errors { | ||
} | ||
class AlreadyConnected extends Error { | ||
constructor(); | ||
} | ||
} |
@@ -33,3 +33,10 @@ "use strict"; | ||
Errors.RequestError = RequestError; | ||
class AlreadyConnected extends Error { | ||
constructor() { | ||
super(); | ||
this.name = 'AlreadyConnected'; | ||
} | ||
} | ||
Errors.AlreadyConnected = AlreadyConnected; | ||
})(Errors = exports.Errors || (exports.Errors = {})); | ||
//# sourceMappingURL=errors.js.map |
@@ -87,11 +87,14 @@ "use strict"; | ||
return this.error(new errors_1.Errors.InvalidJSONRPCError(`No connection id presents in ${message.id}`)); | ||
// throw new Errors.InvalidJSONRPCError( | ||
// `No connection id presents in ${message.id}`, | ||
// ); | ||
} | ||
this.devices.set(message.params.id, ws); | ||
ws.token = message.params.id; | ||
let result = await this.handshake(message.params.id, ws); | ||
ws.send(common_1.RPCHelpers.rpcRequest('connect', { result }, message.id)); | ||
if (!result) { | ||
try { | ||
const result = await this.handshake(message.params.id, ws); | ||
ws.send(common_1.RPCHelpers.rpcRequest('connect', { result }, message.id)); | ||
if (!result) { | ||
ws.close(); | ||
} | ||
} | ||
catch (e) { | ||
ws.send(common_1.RPCHelpers.rpcRequest('connect', { result: false, message: e.message }, message.id)); | ||
ws.close(); | ||
@@ -98,0 +101,0 @@ } |
@@ -16,3 +16,3 @@ import { Id, JSONValue, Name, RPCContext } from './common'; | ||
*/ | ||
handshake?: (result: boolean) => void; | ||
handshake?: (result: boolean, message?: string) => void; | ||
/** | ||
@@ -82,3 +82,3 @@ * Optional function to define initial context for the {@link Client.register} | ||
private requests; | ||
handshake: (connected: boolean) => void; | ||
handshake: (connected: boolean, message?: string) => void; | ||
private readonly prepareContext; | ||
@@ -314,3 +314,3 @@ private readonly errorHandler; | ||
*/ | ||
connectError(): void; | ||
connectError(message?: string): void; | ||
/** | ||
@@ -317,0 +317,0 @@ * Call the server method using params as one argument construction. |
@@ -75,3 +75,3 @@ "use strict"; | ||
case common_1.MessageType.Connect: | ||
this.handshake(message.params.result); | ||
this.handshake(message.params.result, message.params.message); | ||
break; | ||
@@ -315,3 +315,3 @@ case common_1.MessageType.Request: | ||
this.interval = 3000; | ||
this.serverRejected = false; | ||
this.serverRejected = ''; | ||
this.connectedForTheFirstTime = false; | ||
@@ -329,2 +329,6 @@ this.listeners = new Map(); | ||
async init() { | ||
var _a; | ||
if (this.instance && this.instance.readyState === ((_a = this.instance) === null || _a === void 0 ? void 0 : _a.OPEN)) { | ||
throw new errors_1.Errors.AlreadyConnected(); | ||
} | ||
this.connectedForTheFirstTime = false; | ||
@@ -334,3 +338,3 @@ return new Promise((resolve, reject) => { | ||
...this.params, | ||
handshake: (connected) => { | ||
handshake: (connected, message) => { | ||
if (connected) { | ||
@@ -341,3 +345,3 @@ this.connect(); // event | ||
else { | ||
this.connectError(); // event | ||
this.connectError(message); // event | ||
} | ||
@@ -351,13 +355,10 @@ }, | ||
this.removeAllListeners(false); | ||
try { | ||
if (this.serverRejected && !this.connectedForTheFirstTime) { | ||
const msg = this.serverRejected; | ||
this.serverRejected = ''; | ||
reject(new errors_1.Errors.NotConnectedError(msg)); | ||
} | ||
else { | ||
await this.close(); | ||
if (!this.connectedForTheFirstTime) { | ||
resolve(this); | ||
} | ||
} | ||
catch (e) { | ||
if (!this.connectedForTheFirstTime) { | ||
reject(e); | ||
} | ||
} | ||
}); | ||
@@ -371,14 +372,9 @@ }); | ||
async close() { | ||
if (!this.serverRejected) { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
this.init() | ||
.then(() => resolve(this)) | ||
.catch(reject); | ||
}, this.interval); | ||
}); | ||
} | ||
else { | ||
throw new errors_1.Errors.NotConnectedError(`The server rejected the connection`); | ||
} | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
this.init() | ||
.then(() => resolve(this)) | ||
.catch(reject); | ||
}, this.interval); | ||
}); | ||
} | ||
@@ -399,5 +395,7 @@ /** | ||
*/ | ||
connectError() { | ||
this.serverRejected = true; | ||
this.dispatchEvent('connectError', new CustomEvent('connectError')); | ||
connectError(message) { | ||
this.serverRejected = message !== null && message !== void 0 ? message : 'Server rejected the connection'; | ||
this.dispatchEvent('connectError', new CustomEvent('connectError', { | ||
detail: this.serverRejected, | ||
})); | ||
} | ||
@@ -404,0 +402,0 @@ /** |
@@ -22,2 +22,3 @@ export declare type Id = string; | ||
result?: boolean; | ||
message?: string; | ||
}; | ||
@@ -24,0 +25,0 @@ } |
@@ -14,2 +14,5 @@ export declare namespace Errors { | ||
} | ||
class AlreadyConnected extends Error { | ||
constructor(); | ||
} | ||
} |
@@ -33,3 +33,10 @@ "use strict"; | ||
Errors.RequestError = RequestError; | ||
class AlreadyConnected extends Error { | ||
constructor() { | ||
super(); | ||
this.name = 'AlreadyConnected'; | ||
} | ||
} | ||
Errors.AlreadyConnected = AlreadyConnected; | ||
})(Errors = exports.Errors || (exports.Errors = {})); | ||
//# sourceMappingURL=errors.js.map |
{ | ||
"name": "@noveo/dual-rpc-ws", | ||
"version": "0.0.22", | ||
"version": "0.0.24", | ||
"description": "Remote procedure call", | ||
@@ -12,4 +12,2 @@ "types": "build/index.d.ts", | ||
"prepublishOnly": "npm run build && npm run docs", | ||
"node": "npm run build && node build/test.js", | ||
"react": "npm run build && copy \"./*.js\" \"../RNN/node_modules/@noveo/dual-rpc-ws\" && copy \"./*.d.ts\" \"../RNN/node_modules/@noveo/dual-rpc-ws\"", | ||
"test": "jest", | ||
@@ -34,3 +32,3 @@ "coverage": "jest --coverage" | ||
"copy": "^0.3.2", | ||
"jest": "^25.3.0", | ||
"jest": "^25.4.0", | ||
"prettier": "^1.19.1", | ||
@@ -37,0 +35,0 @@ "ts-jest": "^25.3.1", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
97197
23
2033