Skip to content

Commit

Permalink
fix: always return the same value when resolving requests multiple ti…
Browse files Browse the repository at this point in the history
…mes (#522)
  • Loading branch information
pablopalacios authored Aug 15, 2024
1 parent 60f6d97 commit 9cecb4c
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 71 deletions.
78 changes: 43 additions & 35 deletions libs/fetcher.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function Request(operation, resource, options) {
this._body = null;
this._clientConfig = {};
this._startTime = 0;
this._request = null;
}

/**
Expand Down Expand Up @@ -104,22 +105,39 @@ Request.prototype._captureMetaAndStats = function (err, result) {
}
};

Request.prototype._send = function () {
if (this._request) {
return this._request;
}

this._startTime = Date.now();
this._request = httpRequest(normalizeOptions(this));
var captureMetaAndStats = this._captureMetaAndStats.bind(this);

this._request.then(
function (result) {
captureMetaAndStats(null, result);
return result;
},
function (err) {
captureMetaAndStats(err);
throw err;
},
);

return this._request;
};

Request.prototype.then = function (resolve, reject) {
return this.end(function (err, data, meta) {
if (err) {
reject(err);
} else {
resolve({ data, meta });
}
});
return this._send().then(resolve, reject);
};

Request.prototype.catch = function (reject) {
return this.end(function (err) {
if (err) {
reject(err);
}
});
return this._send().catch(reject);
};

Request.prototype.abort = function () {
return this._request.abort();
};

/**
Expand All @@ -130,36 +148,26 @@ Request.prototype.catch = function (reject) {
* @async
*/
Request.prototype.end = function (callback) {
if (!callback) {
if (!arguments.length) {
console.warn(
'You called .end() without a callback. This will become an error in the future. Use .then() instead.',
);
}

var self = this;
self._startTime = Date.now();
this._send();

var onResponse = function (err, result) {
self._captureMetaAndStats(err, result);
if (callback) {
setTimeout(function () {
callback(err, result && result.data, result && result.meta);
});
} else if (err) {
throw err;
} else {
return result;
}
};
if (callback) {
this._request.then(
function (result) {
callback(null, result && result.data, result && result.meta);
},
function (err) {
callback(err);
},
);
}

return httpRequest(normalizeOptions(self)).then(
function (result) {
return onResponse(null, result);
},
function (err) {
return onResponse(err);
},
);
return this._request;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion libs/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class Request {
* is complete.
*/
end(callback) {
if (!callback) {
if (!arguments.length) {
console.warn(
'You called .end() without a callback. This will become an error in the future. Use .then() instead.',
);
Expand Down
10 changes: 4 additions & 6 deletions libs/util/httpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,28 +136,26 @@ function _fetch() {
}

function _send() {
this._promise = _fetch.call(this);
this._request = _fetch.call(this);
}

function FetchrHttpRequest(options) {
this._controller = new AbortController();
this._currentAttempt = 0;
this._options = options;
this._promise = null;
this._request = null;
}

FetchrHttpRequest.prototype.abort = function () {
return this._controller.abort();
};

FetchrHttpRequest.prototype.then = function (resolve, reject) {
this._promise = this._promise.then(resolve, reject);
return this;
return this._request.then(resolve, reject);
};

FetchrHttpRequest.prototype.catch = function (reject) {
this._promise = this._promise.catch(reject);
return this;
return this._request.catch(reject);
};

function httpRequest(options) {
Expand Down
Loading

0 comments on commit 9cecb4c

Please sign in to comment.