feat(nvim-editor): cache

This commit is contained in:
Pihkaal
2024-06-02 14:09:03 +02:00
parent 75dca10f91
commit 8b0d3d573f

View File

@@ -2,6 +2,7 @@ import axios from "axios";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
export const NvimEditor = (props: { source: string | undefined }) => { export const NvimEditor = (props: { source: string | undefined }) => {
const [cache, setCache] = useState(new Map<string, string>());
const [data, setData] = useState<string>(); const [data, setData] = useState<string>();
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
@@ -10,11 +11,28 @@ export const NvimEditor = (props: { source: string | undefined }) => {
useEffect(() => { useEffect(() => {
if (!props.source) return; if (!props.source) return;
const cached = cache.get(props.source);
if (cached) {
console.log("cache hit");
setData(cached);
return;
}
setLoading(true); setLoading(true);
axios.get<string>(props.source).then(({ data }) => { axios
setData(data); .get<string>(props.source)
setLoading(false); .then(({ data }) => {
}); setData(data);
setLoading(false);
setCache((cache) => {
cache.set(props.source!, data);
return cache;
});
})
.catch(() => {
setLoading(false);
});
}, [props.source]); }, [props.source]);
let rows = data?.split("\n") ?? []; let rows = data?.split("\n") ?? [];