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

Rewrite data fetch using async/await. Refs #25 #32

Merged
merged 3 commits into from
Apr 8, 2021
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
115 changes: 52 additions & 63 deletions static/webmention.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,31 +364,6 @@ A more detailed example:
`;
}

/**
* Fetch the stored data.
*
* @param {string} url
* @param {*} callback
*/
function getData(url, callback) {
window
.fetch(url)
.then(function(response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response);
} else {
return Promise.reject(new Error(response.statusText));
}
})
.then(function(response) {
return response.json();
})
.then(callback)
.catch(function(error) {
console.error("Request failed", error);
});
}

/**
* @typedef WebmentionResponse
* @type {Object}
Expand All @@ -398,7 +373,7 @@ A more detailed example:
/**
* Register event listener.
*/
window.addEventListener("load", function () {
window.addEventListener("load", async function () {
const container = document.getElementById(containerID);
if (!container) {
// no container, so do nothing
Expand All @@ -418,48 +393,62 @@ A more detailed example:
apiURL += `&target[]=${encodeURIComponent('http:' + path)}&target[]=${encodeURIComponent('https:' + path)}`;
});

getData(apiURL, function(/** @type {WebmentionResponse} */json) {
/** @type {Array<Reaction>} */
let comments = [];
/** @type {Array<Reaction>} */
const collects = [];

if (commentsAreReactions) {
comments = collects;
/** @type {WebmentionResponse} */
let json = {};
try {
const response = await window.fetch(apiURL);
if (response.status >= 200 && response.status < 300) {
json = await response.json();
} else {
console.error("Could not parse response");
new Error(response.statusText);
}
} catch(error) {
console.error("Request failed", error);
// Burn the app with fire 🔥
throw error;
}

/** @type {Record<ReactEmoji, Array<Reaction>>} */
const mapping = {
"in-reply-to": comments,
"like-of": collects,
"repost-of": collects,
"bookmark-of": collects,
"follow-of": collects,
"mention-of": comments,
"rsvp": comments
};

json.children.forEach(function(child) {
// This seem to push the reaction into either comments or collects
const store = mapping[child['wm-property']];
if (store) {
store.push(child);
}
});
/** @type {Array<Reaction>} */
let comments = [];
/** @type {Array<Reaction>} */
const collects = [];

// format the comment-type things
let formattedComments = '';
if (comments.length > 0 && comments !== collects) {
formattedComments = formatComments(dedupe(comments));
}
if (commentsAreReactions) {
comments = collects;
}

// format the other reactions
let reactions = '';
if (collects.length > 0) {
reactions = formatReactions(dedupe(collects));
/** @type {Record<ReactEmoji, Array<Reaction>>} */
const mapping = {
"in-reply-to": comments,
"like-of": collects,
"repost-of": collects,
"bookmark-of": collects,
"follow-of": collects,
"mention-of": comments,
"rsvp": comments
};

json.children.forEach(function(child) {
// This seem to push the reaction into either comments or collects
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this comment "put the reaction into its appropriate destination"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added that comment, because I wasn't quite sure what's going on there.
Feels a bit dirty to have the store point to the respective object in memory …
=> Another issue?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is nothing dirty about that pattern. It's simple and straightforward and efficient.

const store = mapping[child['wm-property']];
if (store) {
store.push(child);
}

container.innerHTML = `${formattedComments}${reactions}`;
});

// format the comment-type things
let formattedComments = '';
if (comments.length > 0 && comments !== collects) {
formattedComments = formatComments(dedupe(comments));
}

// format the other reactions
let reactions = '';
if (collects.length > 0) {
reactions = formatReactions(dedupe(collects));
}

container.innerHTML = `${formattedComments}${reactions}`;
});
}());
2 changes: 1 addition & 1 deletion static/webmention.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.