perf: cache author for faster lookup

This commit is contained in:
Oliver Booth 2023-08-11 17:15:21 +01:00
parent cd0f38764d
commit 597d7c8b4c
Signed by: oliverbooth
GPG Key ID: 725DB725A0D9EE61
1 changed files with 10 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import API from "./API";
import UI from "./UI";
import Input from "./Input";
import Author from "./Author";
const pkg = require("../../package.json");
@ -35,12 +36,20 @@ declare const Prism: any;
const blogPostContainer = UI.blogPostContainer;
if (blogPostContainer) {
const authors = [];
const template = Handlebars.compile(UI.blogPostTemplate.innerHTML);
API.getBlogPostCount().then(async (count) => {
for (let i = 0; i < count; i++) {
const posts = await API.getBlogPosts(i, 5);
for (const post of posts) {
const author = await API.getAuthor(post.authorId);
let author: Author;
if (authors[post.authorId]) {
author = authors[post.authorId];
} else {
author = await API.getAuthor(post.authorId);
authors[post.authorId] = author;
}
const card = UI.createBlogPostCard(template, post, author);
blogPostContainer.appendChild(card);
UI.updateUI(card);