fix sync bug
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import { STOKEY_RULES, DEFAULT_RULES } from "../config";
|
import { STOKEY_RULES, DEFAULT_RULES } from "../config";
|
||||||
import { useStorage } from "./Storage";
|
import { useStorage } from "./Storage";
|
||||||
import { trySyncRules } from "../libs/sync";
|
import { trySyncRules } from "../libs/sync";
|
||||||
import { useSync } from "./Sync";
|
|
||||||
import { checkRules } from "../libs/rules";
|
import { checkRules } from "../libs/rules";
|
||||||
import { useCallback } from "react";
|
import { useCallback } from "react";
|
||||||
|
|
||||||
@@ -11,19 +10,13 @@ import { useCallback } from "react";
|
|||||||
*/
|
*/
|
||||||
export function useRules() {
|
export function useRules() {
|
||||||
const { data: list, save } = useStorage(STOKEY_RULES, DEFAULT_RULES);
|
const { data: list, save } = useStorage(STOKEY_RULES, DEFAULT_RULES);
|
||||||
const {
|
|
||||||
sync: { rulesUpdateAt },
|
|
||||||
updateSync,
|
|
||||||
} = useSync();
|
|
||||||
|
|
||||||
const updateRules = useCallback(
|
const updateRules = useCallback(
|
||||||
async (rules) => {
|
async (rules) => {
|
||||||
const updateAt = rulesUpdateAt ? Date.now() : 0;
|
|
||||||
await save(rules);
|
await save(rules);
|
||||||
await updateSync({ rulesUpdateAt: updateAt });
|
|
||||||
trySyncRules();
|
trySyncRules();
|
||||||
},
|
},
|
||||||
[rulesUpdateAt, save, updateSync]
|
[save]
|
||||||
);
|
);
|
||||||
|
|
||||||
const add = useCallback(
|
const add = useCallback(
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { STOKEY_SETTING, DEFAULT_SETTING } from "../config";
|
import { STOKEY_SETTING, DEFAULT_SETTING } from "../config";
|
||||||
import { useStorage } from "./Storage";
|
import { useStorage } from "./Storage";
|
||||||
import { useSync } from "./Sync";
|
|
||||||
import { trySyncSetting } from "../libs/sync";
|
import { trySyncSetting } from "../libs/sync";
|
||||||
import { createContext, useCallback, useContext, useMemo } from "react";
|
import { createContext, useCallback, useContext, useMemo } from "react";
|
||||||
import { debounce } from "../libs/utils";
|
import { debounce } from "../libs/utils";
|
||||||
@@ -16,10 +15,6 @@ export function SettingProvider({ children }) {
|
|||||||
STOKEY_SETTING,
|
STOKEY_SETTING,
|
||||||
DEFAULT_SETTING
|
DEFAULT_SETTING
|
||||||
);
|
);
|
||||||
const {
|
|
||||||
sync: { settingUpdateAt },
|
|
||||||
updateSync,
|
|
||||||
} = useSync();
|
|
||||||
|
|
||||||
const syncSetting = useMemo(
|
const syncSetting = useMemo(
|
||||||
() =>
|
() =>
|
||||||
@@ -31,12 +26,10 @@ export function SettingProvider({ children }) {
|
|||||||
|
|
||||||
const updateSetting = useCallback(
|
const updateSetting = useCallback(
|
||||||
async (obj) => {
|
async (obj) => {
|
||||||
const updateAt = settingUpdateAt ? Date.now() : 0;
|
|
||||||
await update(obj);
|
await update(obj);
|
||||||
await updateSync({ settingUpdateAt: updateAt });
|
|
||||||
syncSetting();
|
syncSetting();
|
||||||
},
|
},
|
||||||
[settingUpdateAt, update, updateSync, syncSetting]
|
[update, syncSetting]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import { sha256 } from "./utils";
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
const syncSetting = async (isBg = false) => {
|
const syncSetting = async (isBg = false) => {
|
||||||
const { syncUrl, syncKey, settingUpdateAt } = await getSyncWithDefault();
|
const { syncUrl, syncKey, settingUpdateAt = 0 } = await getSyncWithDefault();
|
||||||
if (!syncUrl || !syncKey) {
|
if (!syncUrl || !syncKey) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -37,16 +37,15 @@ const syncSetting = async (isBg = false) => {
|
|||||||
isBg
|
isBg
|
||||||
);
|
);
|
||||||
|
|
||||||
if (res && res.updateAt > settingUpdateAt) {
|
if (res.updateAt > settingUpdateAt) {
|
||||||
await updateSync({
|
|
||||||
settingUpdateAt: res.updateAt,
|
|
||||||
settingSyncAt: res.updateAt,
|
|
||||||
});
|
|
||||||
await setSetting(res.value);
|
await setSetting(res.value);
|
||||||
return res.value;
|
|
||||||
} else {
|
|
||||||
await updateSync({ settingSyncAt: res.updateAt });
|
|
||||||
}
|
}
|
||||||
|
await updateSync({
|
||||||
|
settingUpdateAt: res.updateAt,
|
||||||
|
settingSyncAt: Date.now(),
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.value;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const trySyncSetting = async (isBg = false) => {
|
export const trySyncSetting = async (isBg = false) => {
|
||||||
@@ -79,16 +78,15 @@ const syncRules = async (isBg = false) => {
|
|||||||
isBg
|
isBg
|
||||||
);
|
);
|
||||||
|
|
||||||
if (res && res.updateAt > rulesUpdateAt) {
|
if (res.updateAt > rulesUpdateAt) {
|
||||||
await updateSync({
|
|
||||||
rulesUpdateAt: res.updateAt,
|
|
||||||
rulesSyncAt: res.updateAt,
|
|
||||||
});
|
|
||||||
await setRules(res.value);
|
await setRules(res.value);
|
||||||
return res.value;
|
|
||||||
} else {
|
|
||||||
await updateSync({ rulesSyncAt: res.updateAt });
|
|
||||||
}
|
}
|
||||||
|
await updateSync({
|
||||||
|
rulesUpdateAt: res.updateAt,
|
||||||
|
rulesSyncAt: Date.now(),
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.value;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const trySyncRules = async (isBg = false) => {
|
export const trySyncRules = async (isBg = false) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user