Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Commit

Permalink
Disable timeouts by default (#162)
Browse files Browse the repository at this point in the history
Refs #160.
  • Loading branch information
glasserc authored Mar 2, 2017
1 parent 020c627 commit 3d4069b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class KintoClientBase {
* @param {Object} [options.retry=0] Number of retries when request fails (default: 0)
* @param {String} [options.bucket="default"] The default bucket to use.
* @param {String} [options.requestMode="cors"] The HTTP request mode (from ES6 fetch spec).
* @param {Number} [options.timeout=5000] The requests timeout in ms.
* @param {Number} [options.timeout=null] The request timeout in ms, if any.
*/
constructor(remote, options={}) {
if (typeof(remote) !== "string" || !remote.length) {
Expand Down
24 changes: 15 additions & 9 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export default class HTTP {
* @type {Object}
*/
static get defaultOptions() {
return {timeout: 5000, requestMode: "cors"};
return {timeout: null, requestMode: "cors"};
}

/**
* Constructor.
*
* @param {EventEmitter} events The event handler.
* @param {Object} [options={}} The options object.
* @param {Number} [options.timeout=5000] The request timeout in ms (default: `5000`).
* @param {Number} [options.timeout=null] The request timeout in ms, if any (default: `null`).
* @param {String} [options.requestMode="cors"] The HTTP request mode (default: `"cors"`).
*/
constructor(events, options={}) {
Expand Down Expand Up @@ -87,19 +87,25 @@ export default class HTTP {
options.mode = this.requestMode;
return new Promise((resolve, reject) => {
// Detect if a request has timed out.
const _timeoutId = setTimeout(() => {
hasTimedout = true;
reject(new Error("Request timeout."));
}, this.timeout);

let _timeoutId;
if (this.timeout) {
_timeoutId = setTimeout(() => {
hasTimedout = true;
reject(new Error("Request timeout."));
}, this.timeout);
}
fetch(url, options).then(res => {
if (!hasTimedout) {
clearTimeout(_timeoutId);
if (_timeoutId) {
clearTimeout(_timeoutId);
}
resolve(res);
}
}).catch(err => {
if (!hasTimedout) {
clearTimeout(_timeoutId);
if (_timeoutId) {
clearTimeout(_timeoutId);
}
reject(err);
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe("KintoClient", () => {
});

it("should keep the default timeout in the child HTTP instance", () => {
expect(new KintoClient(sampleRemote).http.timeout).eql(5000);
expect(new KintoClient(sampleRemote).http.timeout).eql(null);
});

it("should propagate the timeout option to the child HTTP instance", () => {
Expand Down

0 comments on commit 3d4069b

Please sign in to comment.