Skip to content

Commit

Permalink
Metacom call timeout
Browse files Browse the repository at this point in the history
Closes: #92

PR-URL: #100
  • Loading branch information
tshemsedinov committed Dec 13, 2020
1 parent c26c9f0 commit 9aea454
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dist/metacom.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const CALL_TIMEOUT = 7 * 1000;
const PING_INTERVAL = 60 * 1000;
const RECONNECT_TIMEOUT = 2 * 1000;

Expand Down Expand Up @@ -35,6 +36,7 @@ export class Metacom {
this.calls = new Map();
this.active = true;
this.lastActivity = new Date().getTime();
this.callTimeout = options.callTimeout || CALL_TIMEOUT;
this.pingInterval = options.pingInterval || PING_INTERVAL;
this.reconnectTimeout = options.reconnectTimeout || RECONNECT_TIMEOUT;
this.open();
Expand Down Expand Up @@ -135,6 +137,12 @@ export class Metacom {
const target = interfaceName + '/' + methodName;
await this.open();
return new Promise((resolve, reject) => {
setTimeout(() => {
if (this.calls.has(callId)) {
this.calls.delete(callId);
reject(new Error('Request timeout'));
}
}, this.callTimeout);
this.calls.set(callId, [resolve, reject]);
const packet = { call: callId, [target]: args };
this.send(JSON.stringify(packet));
Expand Down

0 comments on commit 9aea454

Please sign in to comment.