Skip to content

Commit

Permalink
perf: concurrent post loading (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
willpuckett committed May 18, 2023
1 parent b6cf193 commit 4e99642
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
UnoCSS,
walk,
} from "./deps.ts";
import { pooledMap } from "https://deno.land/std@0.187.0/async/pool.ts";
import { Index, PostPage } from "./components.tsx";
import type { ConnInfo, FeedItem } from "./deps.ts";
import type {
Expand All @@ -36,6 +37,7 @@ import type {
BlogState,
Post,
} from "./types.d.ts";
import { WalkEntry } from "https://deno.land/std@0.176.0/fs/walk.ts";

export { Fragment, h };

Expand Down Expand Up @@ -192,15 +194,23 @@ async function loadContent(blogDirectory: string, isDev: boolean) {
// Read posts from the current directory and store them in memory.
const postsDirectory = join(blogDirectory, "posts");

// TODO(@satyarohith): not efficient for large number of posts.
for await (
const entry of walk(postsDirectory)
) {
const traversal: WalkEntry[] = [];
for await (const entry of walk(postsDirectory)) {
if (entry.isFile && entry.path.endsWith(".md")) {
await loadPost(postsDirectory, entry.path);
traversal.push(entry);
}
}

const pool = pooledMap(
25,
traversal,
(entry) => loadPost(postsDirectory, entry.path),
);

for await (const _ of pool) {
// noop
}

if (isDev) {
watchForChanges(postsDirectory).catch(() => {});
}
Expand Down

0 comments on commit 4e99642

Please sign in to comment.