refactor: commands from menu
This commit is contained in:
24
lib/cache/cache.ts
vendored
24
lib/cache/cache.ts
vendored
@ -1,4 +1,5 @@
|
||||
import {
|
||||
Bulk,
|
||||
connect,
|
||||
Redis,
|
||||
RedisConnectOptions,
|
||||
@ -14,16 +15,31 @@ async function createCache<T>(): Promise<Map<string, T> | Redis> {
|
||||
const conf: RedisConnectOptions = {
|
||||
hostname: REDIS_HOST,
|
||||
port: REDIS_PORT || 6379,
|
||||
maxRetryCount: 2,
|
||||
};
|
||||
if (REDIS_PASS) {
|
||||
conf.password = REDIS_PASS;
|
||||
}
|
||||
const client = await connect(conf);
|
||||
console.log("[redis] connected");
|
||||
return client;
|
||||
try {
|
||||
const client = await connect(conf);
|
||||
console.log("[redis] connected");
|
||||
return client;
|
||||
} catch (_err) {
|
||||
console.log("[cache] cant connect to redis, falling back to mock");
|
||||
}
|
||||
}
|
||||
|
||||
return new Map<string, T>();
|
||||
const mockRedis = new Map<string, RedisValue>();
|
||||
|
||||
return {
|
||||
async set(key: string, value: RedisValue) {
|
||||
mockRedis.set(key, value);
|
||||
return value.toString();
|
||||
},
|
||||
async get(key: string) {
|
||||
return mockRedis.get(key) as Bulk;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const cache = await createCache();
|
||||
|
@ -76,3 +76,15 @@ export const createStreamResponse = () => {
|
||||
enqueue,
|
||||
};
|
||||
};
|
||||
|
||||
export function debounce<T extends (...args: Parameters<T>) => void>(
|
||||
this: ThisParameterType<T>,
|
||||
fn: T,
|
||||
delay = 300,
|
||||
) {
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
return (...args: Parameters<T>) => {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => fn.apply(this, args), delay);
|
||||
};
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { parseDocument, renderMarkdown } from "@lib/documents.ts";
|
||||
import { parse } from "yaml";
|
||||
import { parse, stringify } from "yaml";
|
||||
import { createCrud } from "@lib/crud.ts";
|
||||
import { extractHashTags } from "@lib/string.ts";
|
||||
import { extractHashTags, formatDate } from "@lib/string.ts";
|
||||
import { fixRenderedMarkdown } from "@lib/helpers.ts";
|
||||
|
||||
export type Movie = {
|
||||
id: string;
|
||||
@ -18,6 +19,27 @@ export type Movie = {
|
||||
};
|
||||
};
|
||||
|
||||
function renderMovie(movie: Movie) {
|
||||
const meta = movie.meta;
|
||||
if ("date" in meta) {
|
||||
meta.date = formatDate(meta.date);
|
||||
}
|
||||
|
||||
return fixRenderedMarkdown(`${
|
||||
meta
|
||||
? `---
|
||||
${stringify(meta)}
|
||||
---`
|
||||
: `---
|
||||
---`
|
||||
}
|
||||
# ${movie.name}
|
||||
${movie.meta.image ? `` : ""}
|
||||
${movie.tags.map((t) => `#${t}`).join(" ")}
|
||||
${movie.description}
|
||||
`);
|
||||
}
|
||||
|
||||
export function parseMovie(original: string, id: string): Movie {
|
||||
const doc = parseDocument(original);
|
||||
|
||||
@ -80,3 +102,8 @@ const crud = createCrud<Movie>({
|
||||
|
||||
export const getMovie = crud.read;
|
||||
export const getAllMovies = crud.readAll;
|
||||
export const createMovie = (movie: Movie) => {
|
||||
console.log("creating movie", { movie });
|
||||
const content = renderMovie(movie);
|
||||
return crud.create(movie.id, content);
|
||||
};
|
||||
|
Reference in New Issue
Block a user