fix data sync
This commit is contained in:
@@ -4,12 +4,12 @@ import {
|
||||
OPT_STYLE_ALL,
|
||||
OPT_LANGS_FROM,
|
||||
OPT_LANGS_TO,
|
||||
STOKEY_RULES_UPDATE_AT,
|
||||
} from "../config";
|
||||
import storage from "../libs/storage";
|
||||
import { useStorages } from "./Storage";
|
||||
import { matchValue } from "../libs/utils";
|
||||
import { apiSyncRules } from "../apis/data";
|
||||
import { syncRules } from "../libs/sync";
|
||||
import { useSync } from "./Sync";
|
||||
|
||||
/**
|
||||
* 匹配规则增删改查 hook
|
||||
@@ -18,13 +18,14 @@ import { apiSyncRules } from "../apis/data";
|
||||
export function useRules() {
|
||||
const storages = useStorages();
|
||||
const list = storages?.[STOKEY_RULES] || [];
|
||||
const sync = useSync();
|
||||
|
||||
const update = async (rules) => {
|
||||
const now = Date.now();
|
||||
const updateAt = sync.opt?.rulesUpdateAt ? Date.now() : 0;
|
||||
await storage.setObj(STOKEY_RULES, rules);
|
||||
await storage.setObj(STOKEY_RULES_UPDATE_AT, now);
|
||||
await sync.update({ rulesUpdateAt: updateAt });
|
||||
try {
|
||||
await apiSyncRules(rules, now);
|
||||
await syncRules();
|
||||
} catch (err) {
|
||||
console.log("[sync rules]", err);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useCallback } from "react";
|
||||
import { STOKEY_SETTING } from "../config";
|
||||
import storage from "../libs/storage";
|
||||
import { useStorages } from "./Storage";
|
||||
import { useSync } from "./Sync";
|
||||
|
||||
/**
|
||||
* 设置hook
|
||||
@@ -17,7 +17,10 @@ export function useSetting() {
|
||||
* @returns
|
||||
*/
|
||||
export function useSettingUpdate() {
|
||||
return useCallback(async (obj) => {
|
||||
await storage.putObj(STOKEY_SETTING, { ...obj, updateAt: Date.now() });
|
||||
}, []);
|
||||
const sync = useSync();
|
||||
return async (obj) => {
|
||||
const updateAt = sync.opt?.settingUpdateAt ? Date.now() : 0;
|
||||
await storage.putObj(STOKEY_SETTING, obj);
|
||||
await sync.update({ settingUpdateAt: updateAt });
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,9 +4,10 @@ import {
|
||||
STOKEY_SETTING,
|
||||
STOKEY_RULES,
|
||||
STOKEY_MSAUTH,
|
||||
STOKEY_SYNC,
|
||||
DEFAULT_SETTING,
|
||||
DEFAULT_RULES,
|
||||
STOKEY_RULES_UPDATE_AT,
|
||||
DEFAULT_SYNC,
|
||||
} from "../config";
|
||||
import storage from "../libs/storage";
|
||||
|
||||
@@ -17,7 +18,7 @@ export const defaultStorage = {
|
||||
[STOKEY_MSAUTH]: null,
|
||||
[STOKEY_SETTING]: DEFAULT_SETTING,
|
||||
[STOKEY_RULES]: DEFAULT_RULES,
|
||||
[STOKEY_RULES_UPDATE_AT]: 1,
|
||||
[STOKEY_SYNC]: DEFAULT_SYNC,
|
||||
};
|
||||
|
||||
const StoragesContext = createContext(null);
|
||||
|
||||
20
src/hooks/Sync.js
Normal file
20
src/hooks/Sync.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { useCallback } from "react";
|
||||
import { STOKEY_SYNC } from "../config";
|
||||
import storage from "../libs/storage";
|
||||
import { useStorages } from "./Storage";
|
||||
|
||||
/**
|
||||
* sync hook
|
||||
* @returns
|
||||
*/
|
||||
export function useSync() {
|
||||
const storages = useStorages();
|
||||
const opt = storages?.[STOKEY_SYNC];
|
||||
const update = useCallback(async (obj) => {
|
||||
await storage.putObj(STOKEY_SYNC, obj);
|
||||
}, []);
|
||||
return {
|
||||
opt,
|
||||
update,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user