optimize debounce form & sync data

This commit is contained in:
Gabe Yuan
2023-08-17 13:27:22 +08:00
parent 30af4c11d0
commit 5d2e767e74
10 changed files with 155 additions and 144 deletions

View File

@@ -13,62 +13,66 @@ import { apiSyncData } from "../apis";
const loadOpt = async () => (await storage.getObj(STOKEY_SYNC)) || DEFAULT_SYNC;
export const syncSetting = async () => {
const { syncUrl, syncKey, settingUpdateAt } = await loadOpt();
if (!syncUrl || !syncKey) {
return;
}
try {
const { syncUrl, syncKey, settingUpdateAt } = await loadOpt();
if (!syncUrl || !syncKey) {
return;
}
const setting = await getSetting();
const res = await apiSyncData(syncUrl, syncKey, {
key: KV_SETTING_KEY,
value: setting,
updateAt: settingUpdateAt,
});
const setting = await getSetting();
const res = await apiSyncData(syncUrl, syncKey, {
key: KV_SETTING_KEY,
value: setting,
updateAt: settingUpdateAt,
});
if (res && res.updateAt > settingUpdateAt) {
await storage.putObj(STOKEY_SYNC, {
settingUpdateAt: res.updateAt,
settingSyncAt: res.updateAt,
});
await storage.setObj(STOKEY_SETTING, res.value);
} else {
await storage.putObj(STOKEY_SYNC, {
settingSyncAt: res.updateAt,
});
if (res && res.updateAt > settingUpdateAt) {
await storage.putObj(STOKEY_SYNC, {
settingUpdateAt: res.updateAt,
settingSyncAt: res.updateAt,
});
await storage.setObj(STOKEY_SETTING, res.value);
} else {
await storage.putObj(STOKEY_SYNC, {
settingSyncAt: res.updateAt,
});
}
} catch (err) {
console.log("[sync setting]", err);
}
};
export const syncRules = async () => {
const { syncUrl, syncKey, rulesUpdateAt } = await loadOpt();
if (!syncUrl || !syncKey) {
return;
}
try {
const { syncUrl, syncKey, rulesUpdateAt } = await loadOpt();
if (!syncUrl || !syncKey) {
return;
}
const rules = await getRules();
const res = await apiSyncData(syncUrl, syncKey, {
key: KV_RULES_KEY,
value: rules,
updateAt: rulesUpdateAt,
});
const rules = await getRules();
const res = await apiSyncData(syncUrl, syncKey, {
key: KV_RULES_KEY,
value: rules,
updateAt: rulesUpdateAt,
});
if (res && res.updateAt > rulesUpdateAt) {
await storage.putObj(STOKEY_SYNC, {
rulesUpdateAt: res.updateAt,
rulesSyncAt: res.updateAt,
});
await storage.setObj(STOKEY_RULES, res.value);
} else {
await storage.putObj(STOKEY_SYNC, {
rulesSyncAt: res.updateAt,
});
if (res && res.updateAt > rulesUpdateAt) {
await storage.putObj(STOKEY_SYNC, {
rulesUpdateAt: res.updateAt,
rulesSyncAt: res.updateAt,
});
await storage.setObj(STOKEY_RULES, res.value);
} else {
await storage.putObj(STOKEY_SYNC, {
rulesSyncAt: res.updateAt,
});
}
} catch (err) {
console.log("[sync rules]", err);
}
};
export const syncAll = async () => {
try {
await syncSetting();
await syncRules();
} catch (err) {
console.log("[sync all]", err);
}
await syncSetting();
await syncRules();
};