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) => {
|
2023-09-17 22:34:28 +08:00
|
|
|
const dataCaches = sync?.dataCaches || {};
|
2023-09-11 17:56:31 +08:00
|
|
|
dataCaches[url] = Date.now();
|
|
|
|
|
await updateSync({ dataCaches });
|
|
|
|
|
},
|
|
|
|
|
[sync, updateSync]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const deleteDataCache = useCallback(
|
|
|
|
|
async (url) => {
|
2023-09-17 22:34:28 +08:00
|
|
|
const dataCaches = sync?.dataCaches || {};
|
2023-09-11 17:56:31 +08:00
|
|
|
delete dataCaches[url];
|
|
|
|
|
await updateSync({ dataCaches });
|
|
|
|
|
},
|
|
|
|
|
[sync, updateSync]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
2023-09-17 22:34:28 +08:00
|
|
|
dataCaches: sync?.dataCaches || {},
|
2023-09-11 17:56:31 +08:00
|
|
|
updateDataCache,
|
|
|
|
|
deleteDataCache,
|
2023-09-11 22:53:04 +08:00
|
|
|
reloadSync,
|
2023-09-11 17:56:31 +08:00
|
|
|
};
|
|
|
|
|
}
|