feat(nvim-editor): cache
This commit is contained in:
@@ -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,10 +11,27 @@ 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
|
||||||
|
.get<string>(props.source)
|
||||||
|
.then(({ data }) => {
|
||||||
setData(data);
|
setData(data);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|
||||||
|
setCache((cache) => {
|
||||||
|
cache.set(props.source!, data);
|
||||||
|
return cache;
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
setLoading(false);
|
||||||
});
|
});
|
||||||
}, [props.source]);
|
}, [props.source]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user