mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
refactor: Improve the structure of useMagnetData function
This commit is contained in:
parent
dce700b189
commit
2f0cd24fb8
1 changed files with 23 additions and 20 deletions
|
@ -1,10 +1,12 @@
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { TorrentData } from "./types";
|
import { TorrentData } from "./types";
|
||||||
|
|
||||||
const cache: Record<string, TorrentData> = {};
|
const cache: Record<string, TorrentData> = {};
|
||||||
|
|
||||||
export function useMagnetData(magnet: string) {
|
export function useMagnetData(magnet: string) {
|
||||||
const [magnetData, setMagnetData] = useState<TorrentData | null>(cache[magnet] || null);
|
const [magnetData, setMagnetData] = useState<TorrentData | null>(
|
||||||
|
cache[magnet] || null
|
||||||
|
);
|
||||||
const [isLoading, setIsLoading] = useState(() => {
|
const [isLoading, setIsLoading] = useState(() => {
|
||||||
if (cache[magnet]) {
|
if (cache[magnet]) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -40,9 +42,11 @@ export function useMagnetData(magnet: string) {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}, []);
|
}, [magnet]);
|
||||||
|
|
||||||
const invalidateCache = useCallback(() => {
|
|
||||||
|
useEffect(() => {
|
||||||
|
function invalidateCache() {
|
||||||
const TWO_MINUTES = 2 * 60 * 1000;
|
const TWO_MINUTES = 2 * 60 * 1000;
|
||||||
const cacheExpiresIn = TWO_MINUTES;
|
const cacheExpiresIn = TWO_MINUTES;
|
||||||
|
|
||||||
|
@ -57,14 +61,13 @@ export function useMagnetData(magnet: string) {
|
||||||
delete cache[key];
|
delete cache[key];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, []);
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
invalidateCache();
|
invalidateCache();
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
invalidateCache();
|
invalidateCache();
|
||||||
}
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue