Skip to content

Commit

Permalink
Replace XHR with the flatter async/await+fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jul 24, 2017
1 parent 86dfa7e commit f45d73c
Showing 1 changed file with 33 additions and 58 deletions.
91 changes: 33 additions & 58 deletions webext/data/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,69 +310,44 @@ function processWithData(user, repo, remoteDataStr, selfDataStr, isFreshData) {
}
}

function onreadystatechangeFactory(xhr, successFn) {
return () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
successFn();
} else if (xhr.status === 403) {
console.warn(_logName,
'Looks like the rate-limit was exceeded.');
} else {
console.warn(_logName,
'GitHub API returned status:', xhr.status);
}
} else {
// Request is still in progress
// Do nothing.
}
};
}
async function makeFreshRequest(user, repo) {
const response = await fetch(makeRemoteDataURL(user, repo));

function makeFreshRequest(user, repo) {
const xhrFork = new XMLHttpRequest();

xhrFork.onreadystatechange = onreadystatechangeFactory(
xhrFork,
() => {
const forksDataJson = JSON.parse(xhrFork.responseText);
if (!forksDataJson || forksDataJson.length === 0) {
if (DEBUG) {
console.log(_logName,
'Repository does not have any forks.');
}
return;
}
if (response.status === 403) {
return console.warn(_logName,
'Looks like the rate-limit was exceeded.');
}

const mostStarredFork = forksDataJson[0];
const forksDataStr = JSON.stringify([mostStarredFork]);
const defaultBranch = mostStarredFork['default_branch'];
const remoteUser = mostStarredFork['owner']['login'];

const xhrDiff = new XMLHttpRequest();

xhrDiff.onreadystatechange = () => {
if (xhrDiff.readyState === 4) {
if (xhrDiff.status === 200) {
const commitDiffJson = JSON.parse(xhrDiff.responseText);
// Dropping the list of commits to conserve space.
commitDiffJson['commits'] = [];
const commitDiffStr = JSON.stringify(commitDiffJson);
processWithData(user, repo, forksDataStr, commitDiffStr, true);
} else {
// In case of any error, ignore recency data.
processWithData(user, repo, forksDataStr, null, true);
}
}
};
if (!response.ok) {
return console.warn(_logName,
'GitHub API returned status:', response.status);
}

xhrDiff.open('GET', makeCommitDiffURL(user, repo, remoteUser, defaultBranch));
xhrDiff.send();
const [mostStarredFork] = await response.json();

if (!mostStarredFork) {
if (DEBUG) {
console.log(_logName,
'Repository does not have any forks.');
}
);
return;
}

const forksDataStr = JSON.stringify([mostStarredFork]);
const defaultBranch = mostStarredFork['default_branch'];
const remoteUser = mostStarredFork['owner']['login'];

const response2 = await fetch(makeCommitDiffURL(user, repo, remoteUser, defaultBranch));

let commitDiffStr = null;
if (response2.ok) {
const commitDiffJson = await response2.json();
// Dropping the list of commits to conserve space.
delete commitDiffJson.commits;
commitDiffStr = JSON.stringify(commitDiffJson);
}

xhrFork.open('GET', makeRemoteDataURL(user, repo));
xhrFork.send();
processWithData(user, repo, forksDataStr, commitDiffStr, true);
}

function getDataFor(user, repo) {
Expand Down

0 comments on commit f45d73c

Please sign in to comment.