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