diff --git a/src/background.js b/src/background.js index c930ea6..d023cb1 100644 --- a/src/background.js +++ b/src/background.js @@ -19,7 +19,7 @@ import { } from "./config"; import storage from "./libs/storage"; import { getSetting } from "./libs"; -import { syncAll } from "./libs/sync"; +import { trySyncAll } from "./libs/sync"; import { fetchData, fetchPool } from "./libs/fetch"; import { sendTabMsg } from "./libs/msg"; import { trySyncAllSubRules } from "./libs/rules"; @@ -45,7 +45,7 @@ browser.runtime.onStartup.addListener(async () => { console.log("browser onStartup"); // 同步数据 - await syncAll(true); + await trySyncAll(true); // 清除缓存 const setting = await getSetting(); diff --git a/src/config/i18n.js b/src/config/i18n.js index 2da2ee1..34c27bd 100644 --- a/src/config/i18n.js +++ b/src/config/i18n.js @@ -272,6 +272,18 @@ export const I18N = { zh: `数据同步密钥`, en: `Data Sync Key`, }, + data_sync_test: { + zh: `数据同步测试`, + en: `Data Sync Test`, + }, + data_sync_success: { + zh: `数据同步成功!`, + en: `Data Sync Success`, + }, + data_sync_error: { + zh: `数据同步失败!`, + en: `Data Sync Error`, + }, error_got_some_wrong: { zh: "抱歉,出错了!", en: "Sorry, something went wrong!", diff --git a/src/hooks/Rules.js b/src/hooks/Rules.js index 5cda528..9ca692c 100644 --- a/src/hooks/Rules.js +++ b/src/hooks/Rules.js @@ -1,7 +1,7 @@ import { STOKEY_RULES, DEFAULT_SUBRULES_LIST } from "../config"; import storage from "../libs/storage"; import { useStorages } from "./Storage"; -import { syncRules } from "../libs/sync"; +import { trySyncRules } from "../libs/sync"; import { useSync } from "./Sync"; import { useSetting, useSettingUpdate } from "./Setting"; import { checkRules } from "../libs/rules"; @@ -19,7 +19,7 @@ export function useRules() { const updateAt = sync.opt?.rulesUpdateAt ? Date.now() : 0; await storage.setObj(STOKEY_RULES, rules); await sync.update({ rulesUpdateAt: updateAt }); - syncRules(); + trySyncRules(); }; const add = async (rule) => { diff --git a/src/hooks/Setting.js b/src/hooks/Setting.js index ad19a9c..1757bbf 100644 --- a/src/hooks/Setting.js +++ b/src/hooks/Setting.js @@ -2,7 +2,7 @@ import { STOKEY_SETTING } from "../config"; import storage from "../libs/storage"; import { useStorages } from "./Storage"; import { useSync } from "./Sync"; -import { syncSetting } from "../libs/sync"; +import { trySyncSetting } from "../libs/sync"; /** * 设置hook @@ -23,6 +23,6 @@ export function useSettingUpdate() { const updateAt = sync.opt?.settingUpdateAt ? Date.now() : 0; await storage.putObj(STOKEY_SETTING, obj); await sync.update({ settingUpdateAt: updateAt }); - syncSetting(); + trySyncSetting(); }; } diff --git a/src/libs/sync.js b/src/libs/sync.js index 6cc95fb..fd44bf9 100644 --- a/src/libs/sync.js +++ b/src/libs/sync.js @@ -28,33 +28,37 @@ export const syncOpt = { * @returns */ export const syncSetting = async (isBg = false) => { + const { syncUrl, syncKey, settingUpdateAt } = await syncOpt.load(); + if (!syncUrl || !syncKey) { + return; + } + + const setting = await getSetting(); + const res = await apiSyncData( + syncUrl, + syncKey, + { + key: KV_SETTING_KEY, + value: setting, + updateAt: settingUpdateAt, + }, + isBg + ); + + if (res && res.updateAt > settingUpdateAt) { + await syncOpt.update({ + settingUpdateAt: res.updateAt, + settingSyncAt: res.updateAt, + }); + await storage.setObj(STOKEY_SETTING, res.value); + } else { + await syncOpt.update({ settingSyncAt: res.updateAt }); + } +}; + +export const trySyncSetting = async (isBg = false) => { try { - const { syncUrl, syncKey, settingUpdateAt } = await syncOpt.load(); - if (!syncUrl || !syncKey) { - return; - } - - const setting = await getSetting(); - const res = await apiSyncData( - syncUrl, - syncKey, - { - key: KV_SETTING_KEY, - value: setting, - updateAt: settingUpdateAt, - }, - isBg - ); - - if (res && res.updateAt > settingUpdateAt) { - await syncOpt.update({ - settingUpdateAt: res.updateAt, - settingSyncAt: res.updateAt, - }); - await storage.setObj(STOKEY_SETTING, res.value); - } else { - await syncOpt.update({ settingSyncAt: res.updateAt }); - } + await syncSetting(isBg); } catch (err) { console.log("[sync setting]", err); } @@ -65,33 +69,37 @@ export const syncSetting = async (isBg = false) => { * @returns */ export const syncRules = async (isBg = false) => { + const { syncUrl, syncKey, rulesUpdateAt } = await syncOpt.load(); + if (!syncUrl || !syncKey) { + return; + } + + const rules = await getRules(); + const res = await apiSyncData( + syncUrl, + syncKey, + { + key: KV_RULES_KEY, + value: rules, + updateAt: rulesUpdateAt, + }, + isBg + ); + + if (res && res.updateAt > rulesUpdateAt) { + await syncOpt.update({ + rulesUpdateAt: res.updateAt, + rulesSyncAt: res.updateAt, + }); + await storage.setObj(STOKEY_RULES, res.value); + } else { + await syncOpt.update({ rulesSyncAt: res.updateAt }); + } +}; + +export const trySyncRules = async (isBg = false) => { try { - const { syncUrl, syncKey, rulesUpdateAt } = await syncOpt.load(); - if (!syncUrl || !syncKey) { - return; - } - - const rules = await getRules(); - const res = await apiSyncData( - syncUrl, - syncKey, - { - key: KV_RULES_KEY, - value: rules, - updateAt: rulesUpdateAt, - }, - isBg - ); - - if (res && res.updateAt > rulesUpdateAt) { - await syncOpt.update({ - rulesUpdateAt: res.updateAt, - rulesSyncAt: res.updateAt, - }); - await storage.setObj(STOKEY_RULES, res.value); - } else { - await syncOpt.update({ rulesSyncAt: res.updateAt }); - } + await syncRules(isBg); } catch (err) { console.log("[sync user rules]", err); } @@ -121,3 +129,8 @@ export const syncAll = async (isBg = false) => { await syncSetting(isBg); await syncRules(isBg); }; + +export const trySyncAll = async (isBg = false) => { + await trySyncSetting(isBg); + await trySyncRules(isBg); +}; diff --git a/src/views/Options/SyncSetting.js b/src/views/Options/SyncSetting.js index 83bff35..822e158 100644 --- a/src/views/Options/SyncSetting.js +++ b/src/views/Options/SyncSetting.js @@ -7,12 +7,16 @@ import Alert from "@mui/material/Alert"; import Link from "@mui/material/Link"; import { URL_KISS_WORKER } from "../../config"; import { debounce } from "../../libs/utils"; -import { useMemo } from "react"; +import { useMemo, useState } from "react"; import { syncAll } from "../../libs/sync"; +import Button from "@mui/material/Button"; +import { useAlert } from "../../hooks/Alert"; export default function SyncSetting() { const i18n = useI18n(); const sync = useSync(); + const alert = useAlert(); + const [loading, setLoading] = useState(false); const handleChange = useMemo( () => @@ -22,11 +26,25 @@ export default function SyncSetting() { await sync.update({ [name]: value, }); - await syncAll(); - }, 1000), + // trySyncAll(); + }, 500), [sync] ); + const handleSyncTest = async (e) => { + e.preventDefault(); + try { + setLoading(true); + await syncAll(); + alert.success(i18n("data_sync_success")); + } catch (err) { + console.log("[sync all]", err); + alert.error(i18n("data_sync_error")); + } finally { + setLoading(false); + } + }; + if (!sync.opt) { return; } @@ -57,6 +75,17 @@ export default function SyncSetting() { defaultValue={syncKey} onChange={handleChange} /> + + + + ); diff --git a/src/views/Options/index.js b/src/views/Options/index.js index 9b3440b..b1fece5 100644 --- a/src/views/Options/index.js +++ b/src/views/Options/index.js @@ -10,7 +10,7 @@ import { useEffect, useState } from "react"; import { isGm } from "../../libs/browser"; import { sleep } from "../../libs/utils"; import CircularProgress from "@mui/material/CircularProgress"; -import { syncAll } from "../../libs/sync"; +import { trySyncAll } from "../../libs/sync"; import { AlertProvider } from "../../hooks/Alert"; export default function Options() { @@ -38,7 +38,7 @@ export default function Options() { } // 同步数据 - syncAll(); + trySyncAll(); })(); }, []);