subrules sync time
This commit is contained in:
@@ -31,6 +31,7 @@ export function useStorage(key, defaultVal = null) {
|
||||
if (val) {
|
||||
setData(val);
|
||||
} else if (defaultVal) {
|
||||
setData(defaultVal);
|
||||
await storage.setObj(key, defaultVal);
|
||||
}
|
||||
}, [key, defaultVal]);
|
||||
|
||||
@@ -7,8 +7,8 @@ import { useStorage } from "./Storage";
|
||||
* @returns
|
||||
*/
|
||||
export function useSync() {
|
||||
const { data, update } = useStorage(STOKEY_SYNC, DEFAULT_SYNC);
|
||||
return { sync: data, updateSync: update };
|
||||
const { data, update, reload } = useStorage(STOKEY_SYNC, DEFAULT_SYNC);
|
||||
return { sync: data, updateSync: update, reloadSync: reload };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -17,7 +17,7 @@ export function useSync() {
|
||||
* @returns
|
||||
*/
|
||||
export function useSyncCaches() {
|
||||
const { sync, updateSync } = useSync();
|
||||
const { sync, updateSync, reloadSync } = useSync();
|
||||
|
||||
const updateDataCache = useCallback(
|
||||
async (url) => {
|
||||
@@ -41,5 +41,6 @@ export function useSyncCaches() {
|
||||
dataCaches: sync.dataCaches || {},
|
||||
updateDataCache,
|
||||
deleteDataCache,
|
||||
reloadSync,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -49,9 +49,8 @@ export const matchRule = async (
|
||||
mixRule[key] = val;
|
||||
});
|
||||
|
||||
const subRules = (await loadOrFetchSubRules(selectedSub.url)).map(
|
||||
(item) => ({ ...item, ...mixRule })
|
||||
);
|
||||
let subRules = await loadOrFetchSubRules(selectedSub.url);
|
||||
subRules = subRules.map((item) => ({ ...item, ...mixRule }));
|
||||
rules.splice(-1, 0, ...subRules);
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
@@ -10,6 +10,16 @@ import { apiFetch } from "../apis";
|
||||
import { checkRules } from "./rules";
|
||||
import { isAllchar } from "./utils";
|
||||
|
||||
/**
|
||||
* 更新缓存同步时间
|
||||
* @param {*} url
|
||||
*/
|
||||
const updateSyncDataCache = async (url) => {
|
||||
const { dataCaches = {} } = await getSyncWithDefault();
|
||||
dataCaches[url] = Date.now();
|
||||
await updateSync({ dataCaches });
|
||||
};
|
||||
|
||||
/**
|
||||
* 同步订阅规则
|
||||
* @param {*} url
|
||||
@@ -35,6 +45,7 @@ export const syncAllSubRules = async (subrulesList, isBg = false) => {
|
||||
for (let subrules of subrulesList) {
|
||||
try {
|
||||
await syncSubRules(subrules.url, isBg);
|
||||
await updateSyncDataCache(subrules.url);
|
||||
} catch (err) {
|
||||
console.log(`[sync subrule error]: ${subrules.url}`, err);
|
||||
}
|
||||
@@ -70,9 +81,10 @@ export const trySyncAllSubRules = async ({ subrulesList }, isBg = false) => {
|
||||
* @returns
|
||||
*/
|
||||
export const loadOrFetchSubRules = async (url) => {
|
||||
const rules = await getSubRules(url);
|
||||
if (rules?.length) {
|
||||
return rules;
|
||||
let rules = await getSubRules(url);
|
||||
if (!rules || rules.length === 0) {
|
||||
rules = await syncSubRules(url);
|
||||
await updateSyncDataCache(url);
|
||||
}
|
||||
return syncSubRules(url);
|
||||
return rules || [];
|
||||
};
|
||||
|
||||
@@ -798,7 +798,7 @@ function SubRules({ subRules }) {
|
||||
setSelectedRules,
|
||||
loading,
|
||||
} = subRules;
|
||||
const { dataCaches, updateDataCache, deleteDataCache } =
|
||||
const { dataCaches, updateDataCache, deleteDataCache, reloadSync } =
|
||||
useSyncCaches();
|
||||
|
||||
const handleSelect = (e) => {
|
||||
@@ -806,6 +806,10 @@ function SubRules({ subRules }) {
|
||||
selectSub(url);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
reloadSync();
|
||||
}, [selectedRules, reloadSync]);
|
||||
|
||||
return (
|
||||
<Stack spacing={3}>
|
||||
<SubRulesEdit
|
||||
|
||||
Reference in New Issue
Block a user