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

fix: always return the same value when resolving requests multiple times #522

Merged
merged 5 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
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
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
Loading