Files
kiss-translator/src/hooks/Sync.js

47 lines
1.0 KiB
JavaScript
Raw Normal View History

2023-09-11 17:56:31 +08:00
import { useCallback } from "react";
2023-08-30 18:05:37 +08:00
import { STOKEY_SYNC, DEFAULT_SYNC } from "../config";
import { useStorage } from "./Storage";
2023-07-31 15:08:51 +08:00
/**
* sync hook
* @returns
*/
export function useSync() {
2023-09-11 22:53:04 +08:00
const { data, update, reload } = useStorage(STOKEY_SYNC, DEFAULT_SYNC);
return { sync: data, updateSync: update, reloadSync: reload };
2023-07-31 15:08:51 +08:00
}
2023-09-11 17:56:31 +08:00
/**
* caches sync hook
* @param {*} url
* @returns
*/
export function useSyncCaches() {
2023-09-11 22:53:04 +08:00
const { sync, updateSync, reloadSync } = useSync();
2023-09-11 17:56:31 +08:00
const updateDataCache = useCallback(
async (url) => {
const dataCaches = sync.dataCaches || {};
dataCaches[url] = Date.now();
await updateSync({ dataCaches });
},
[sync, updateSync]
);
const deleteDataCache = useCallback(
async (url) => {
const dataCaches = sync.dataCaches || {};
delete dataCaches[url];
await updateSync({ dataCaches });
},
[sync, updateSync]
);
return {
dataCaches: sync.dataCaches || {},
updateDataCache,
deleteDataCache,
2023-09-11 22:53:04 +08:00
reloadSync,
2023-09-11 17:56:31 +08:00
};
}