subrules sync time

This commit is contained in:
Gabe Yuan
2023-09-11 22:53:04 +08:00
parent 79612f8a1b
commit fa244b2097
5 changed files with 28 additions and 11 deletions

View File

@@ -31,6 +31,7 @@ export function useStorage(key, defaultVal = null) {
if (val) { if (val) {
setData(val); setData(val);
} else if (defaultVal) { } else if (defaultVal) {
setData(defaultVal);
await storage.setObj(key, defaultVal); await storage.setObj(key, defaultVal);
} }
}, [key, defaultVal]); }, [key, defaultVal]);

View File

@@ -7,8 +7,8 @@ import { useStorage } from "./Storage";
* @returns * @returns
*/ */
export function useSync() { export function useSync() {
const { data, update } = useStorage(STOKEY_SYNC, DEFAULT_SYNC); const { data, update, reload } = useStorage(STOKEY_SYNC, DEFAULT_SYNC);
return { sync: data, updateSync: update }; return { sync: data, updateSync: update, reloadSync: reload };
} }
/** /**
@@ -17,7 +17,7 @@ export function useSync() {
* @returns * @returns
*/ */
export function useSyncCaches() { export function useSyncCaches() {
const { sync, updateSync } = useSync(); const { sync, updateSync, reloadSync } = useSync();
const updateDataCache = useCallback( const updateDataCache = useCallback(
async (url) => { async (url) => {
@@ -41,5 +41,6 @@ export function useSyncCaches() {
dataCaches: sync.dataCaches || {}, dataCaches: sync.dataCaches || {},
updateDataCache, updateDataCache,
deleteDataCache, deleteDataCache,
reloadSync,
}; };
} }

View File

@@ -49,9 +49,8 @@ export const matchRule = async (
mixRule[key] = val; mixRule[key] = val;
}); });
const subRules = (await loadOrFetchSubRules(selectedSub.url)).map( let subRules = await loadOrFetchSubRules(selectedSub.url);
(item) => ({ ...item, ...mixRule }) subRules = subRules.map((item) => ({ ...item, ...mixRule }));
);
rules.splice(-1, 0, ...subRules); rules.splice(-1, 0, ...subRules);
} }
} catch (err) { } catch (err) {

View File

@@ -10,6 +10,16 @@ import { apiFetch } from "../apis";
import { checkRules } from "./rules"; import { checkRules } from "./rules";
import { isAllchar } from "./utils"; import { isAllchar } from "./utils";
/**
* 更新缓存同步时间
* @param {*} url
*/
const updateSyncDataCache = async (url) => {
const { dataCaches = {} } = await getSyncWithDefault();
dataCaches[url] = Date.now();
await updateSync({ dataCaches });
};
/** /**
* 同步订阅规则 * 同步订阅规则
* @param {*} url * @param {*} url
@@ -35,6 +45,7 @@ export const syncAllSubRules = async (subrulesList, isBg = false) => {
for (let subrules of subrulesList) { for (let subrules of subrulesList) {
try { try {
await syncSubRules(subrules.url, isBg); await syncSubRules(subrules.url, isBg);
await updateSyncDataCache(subrules.url);
} catch (err) { } catch (err) {
console.log(`[sync subrule error]: ${subrules.url}`, err); console.log(`[sync subrule error]: ${subrules.url}`, err);
} }
@@ -70,9 +81,10 @@ export const trySyncAllSubRules = async ({ subrulesList }, isBg = false) => {
* @returns * @returns
*/ */
export const loadOrFetchSubRules = async (url) => { export const loadOrFetchSubRules = async (url) => {
const rules = await getSubRules(url); let rules = await getSubRules(url);
if (rules?.length) { if (!rules || rules.length === 0) {
return rules; rules = await syncSubRules(url);
await updateSyncDataCache(url);
} }
return syncSubRules(url); return rules || [];
}; };

View File

@@ -798,7 +798,7 @@ function SubRules({ subRules }) {
setSelectedRules, setSelectedRules,
loading, loading,
} = subRules; } = subRules;
const { dataCaches, updateDataCache, deleteDataCache } = const { dataCaches, updateDataCache, deleteDataCache, reloadSync } =
useSyncCaches(); useSyncCaches();
const handleSelect = (e) => { const handleSelect = (e) => {
@@ -806,6 +806,10 @@ function SubRules({ subRules }) {
selectSub(url); selectSub(url);
}; };
useEffect(() => {
reloadSync();
}, [selectedRules, reloadSync]);
return ( return (
<Stack spacing={3}> <Stack spacing={3}>
<SubRulesEdit <SubRulesEdit