Skip to content

Commit

Permalink
Merge pull request #64 from cse210-works-on-my-machine/pre-fetch
Browse files Browse the repository at this point in the history
Pre-fetch posts to reduce load time of the webpage
  • Loading branch information
jacksonconte authored Dec 13, 2023
2 parents 664c9db + 8915cdb commit aad0b98
Show file tree
Hide file tree
Showing 18 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion docs/Display.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/Fetcher.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/InstanceEntry.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/Paginator_Paginator.html
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/entity_InstanceEntry.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/fetchers_Fetcher.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/fetchers_LemmyFetcher.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
15 changes: 7 additions & 8 deletions docs/fetchers_MastodonFetcher.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ <h1 class="page-title">Source: fetchers/MastodonFetcher.js</h1>

export const TAGS_SUFFIX = "/api/v1/trends/tags";
export const POST_SUFFIX = "/api/v1/timelines/tag/:";
export const NUM_MASTODON_POSTS = 15;

export class MastodonFetcher extends Fetcher {
/**
Expand All @@ -39,12 +40,10 @@ <h1 class="page-title">Source: fetchers/MastodonFetcher.js</h1>
async fetchPosts(instURL) {
try {
const hashtags = await this.#fetchTrendingTags(instURL + TAGS_SUFFIX);
const posts = [];
for (const tag of hashtags) {
console.log("Fetching posts for tag: " + tag.name);
let response = await this.#fetchPostsByHashtag(instURL, tag.name);
posts.push(...response);
}
const fetchPromises = hashtags.map(tag => this.#fetchPostsByHashtag(instURL, tag.name));
const responses = await Promise.all(fetchPromises);
const posts = responses.flat();
posts.push(...posts);
return posts;
}
catch (error) {
Expand Down Expand Up @@ -79,7 +78,7 @@ <h1 class="page-title">Source: fetchers/MastodonFetcher.js</h1>
* @returns {Promise&lt;Array>} - A promise that resolves to an array of posts for the given hashtag
*/
async #fetchPostsByHashtag(instURL, hashtag) {
const trendingPostURL = instURL + POST_SUFFIX + hashtag;
const trendingPostURL = instURL + POST_SUFFIX + hashtag + "?limit=" + NUM_MASTODON_POSTS;
try {
const response = await fetch(trendingPostURL);
if (!response.ok) {
Expand Down Expand Up @@ -109,7 +108,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/instanceList.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/pageBuilder.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/paginator.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/postBuilder_LemmyPostBuilder.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/postBuilder_MastodonPostBuilder.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/postBuilder_PostBuilder.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="InstanceE
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 01:06:01 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Dec 13 2023 03:40:25 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
13 changes: 6 additions & 7 deletions src/scripts/fetchers/MastodonFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Fetcher } from "./Fetcher.js";

export const TAGS_SUFFIX = "/api/v1/trends/tags";
export const POST_SUFFIX = "/api/v1/timelines/tag/:";
export const NUM_MASTODON_POSTS = 15;

export class MastodonFetcher extends Fetcher {
/**
Expand All @@ -11,12 +12,10 @@ export class MastodonFetcher extends Fetcher {
async fetchPosts(instURL) {
try {
const hashtags = await this.#fetchTrendingTags(instURL + TAGS_SUFFIX);
const posts = [];
for (const tag of hashtags) {
console.log("Fetching posts for tag: " + tag.name);
let response = await this.#fetchPostsByHashtag(instURL, tag.name);
posts.push(...response);
}
const fetchPromises = hashtags.map(tag => this.#fetchPostsByHashtag(instURL, tag.name));
const responses = await Promise.all(fetchPromises);
const posts = responses.flat();
posts.push(...posts);
return posts;
}
catch (error) {
Expand Down Expand Up @@ -51,7 +50,7 @@ export class MastodonFetcher extends Fetcher {
* @returns {Promise<Array>} - A promise that resolves to an array of posts for the given hashtag
*/
async #fetchPostsByHashtag(instURL, hashtag) {
const trendingPostURL = instURL + POST_SUFFIX + hashtag;
const trendingPostURL = instURL + POST_SUFFIX + hashtag + "?limit=" + NUM_MASTODON_POSTS;
try {
const response = await fetch(trendingPostURL);
if (!response.ok) {
Expand Down
8 changes: 4 additions & 4 deletions test/scripts/fetchers/MastodonFetcher.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, beforeEach } from "node:test";
import * as assert from "assert";
import { MastodonFetcher, TAGS_SUFFIX, POST_SUFFIX } from "../../../src/scripts/fetchers/MastodonFetcher.js";
import { MastodonFetcher, TAGS_SUFFIX, POST_SUFFIX, NUM_MASTODON_POSTS } from "../../../src/scripts/fetchers/MastodonFetcher.js";
import sinon from "sinon";

const FAKE_URL = "fakeTrendingURL";
Expand Down Expand Up @@ -39,12 +39,12 @@ describe("MastodonFetcher", () => {
};

// Mock trending post response for each tag
fetchStub.withArgs(FAKE_URL + POST_SUFFIX + "tag1").resolves(mockResponse);
fetchStub.withArgs(FAKE_URL + POST_SUFFIX + "tag2").resolves(mockResponse);
fetchStub.withArgs(FAKE_URL + POST_SUFFIX + "tag1" + "?limit=" + NUM_MASTODON_POSTS).resolves(mockResponse);
fetchStub.withArgs(FAKE_URL + POST_SUFFIX + "tag2" + "?limit=" + NUM_MASTODON_POSTS).resolves(mockResponse);

const posts = await mastodonFetcher.fetchPosts(FAKE_URL);

assert.strictEqual(posts.length, 2);
assert.strictEqual(posts.length, 4);
});
});
});

0 comments on commit aad0b98

Please sign in to comment.