Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel timer when server responds #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions MoleClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ class MoleClient {
const data = JSON.stringify(object);

return new Promise((resolve, reject) => {
this.pendingRequest[id] = { resolve, reject, sentObject: object };

setTimeout(() => {
const timer = setTimeout(() => {
if (this.pendingRequest[id]) {
delete this.pendingRequest[id];

reject(new X.RequestTimout());
}
}, this.requestTimeout);

this.pendingRequest[id] = { resolve, reject, sentObject: object, timer };

return this.transport.sendData(data).catch(error => {
delete this.pendingRequest[id];
reject(error); // TODO new X.InternalError()
Expand Down Expand Up @@ -99,6 +99,7 @@ class MoleClient {
delete this.pendingRequest[response.id];

if (!resolvers) return;
clearTimeout(resolvers.timer);

if (isSuccessfulResponse) {
resolvers.resolve(response.result);
Expand Down Expand Up @@ -127,8 +128,9 @@ class MoleClient {

if (!this.pendingRequest[batchId]) return;

const { sentObject, resolve } = this.pendingRequest[batchId];
const { sentObject, resolve, timer } = this.pendingRequest[batchId];
delete this.pendingRequest[batchId];
clearTimeout(timer);

const batchResults = [];
let errorIdx = 0;
Expand Down