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

switch post loading to async #116

Merged
merged 5 commits into from
May 18, 2023
Merged
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
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/[email protected]/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/[email protected]/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