fix sync bug

This commit is contained in:
Gabe Yuan
2023-09-11 11:33:28 +08:00
parent 0d93cf78f7
commit 2ee4609192
4 changed files with 31 additions and 13 deletions

View File

@@ -14,7 +14,7 @@ export function useRules() {
const updateRules = useCallback(
async (rules) => {
await save(rules);
trySyncRules();
trySyncRules(false, true);
},
[save]
);

View File

@@ -19,7 +19,7 @@ export function SettingProvider({ children }) {
const syncSetting = useMemo(
() =>
debounce(() => {
trySyncSetting();
trySyncSetting(false, true);
}, [2000]),
[]
);

View File

@@ -150,5 +150,5 @@ export const saveRule = async (newRule) => {
rules.unshift(newRule);
}
await setRules(rules);
trySyncRules();
trySyncRules(false, true);
};

View File

@@ -19,12 +19,21 @@ import { sha256 } from "./utils";
* 同步设置
* @returns
*/
const syncSetting = async (isBg = false) => {
const { syncUrl, syncKey, settingUpdateAt = 0 } = await getSyncWithDefault();
const syncSetting = async (isBg = false, isForce = false) => {
let {
syncUrl,
syncKey,
settingUpdateAt = 0,
settingSyncAt = 0,
} = await getSyncWithDefault();
if (!syncUrl || !syncKey) {
return;
}
if (isForce) {
settingUpdateAt = Date.now();
}
const setting = await getSettingWithDefault();
const res = await apiSyncData(
syncUrl,
@@ -32,7 +41,7 @@ const syncSetting = async (isBg = false) => {
{
key: KV_SETTING_KEY,
value: setting,
updateAt: settingUpdateAt,
updateAt: settingSyncAt === 0 ? 0 : settingUpdateAt,
},
isBg
);
@@ -48,9 +57,9 @@ const syncSetting = async (isBg = false) => {
return res.value;
};
export const trySyncSetting = async (isBg = false) => {
export const trySyncSetting = async (isBg = false, isForce = false) => {
try {
return await syncSetting(isBg);
return await syncSetting(isBg, isForce);
} catch (err) {
console.log("[sync setting]", err);
}
@@ -60,12 +69,21 @@ export const trySyncSetting = async (isBg = false) => {
* 同步规则
* @returns
*/
const syncRules = async (isBg = false) => {
const { syncUrl, syncKey, rulesUpdateAt } = await getSyncWithDefault();
const syncRules = async (isBg = false, isForce = false) => {
let {
syncUrl,
syncKey,
rulesUpdateAt = 0,
rulesSyncAt = 0,
} = await getSyncWithDefault();
if (!syncUrl || !syncKey) {
return;
}
if (isForce) {
rulesUpdateAt = Date.now();
}
const rules = await getRulesWithDefault();
const res = await apiSyncData(
syncUrl,
@@ -73,7 +91,7 @@ const syncRules = async (isBg = false) => {
{
key: KV_RULES_KEY,
value: rules,
updateAt: rulesUpdateAt,
updateAt: rulesSyncAt === 0 ? 0 : rulesUpdateAt,
},
isBg
);
@@ -89,9 +107,9 @@ const syncRules = async (isBg = false) => {
return res.value;
};
export const trySyncRules = async (isBg = false) => {
export const trySyncRules = async (isBg = false, isForce = false) => {
try {
return await syncRules(isBg);
return await syncRules(isBg, isForce);
} catch (err) {
console.log("[sync user rules]", err);
}