@resourge/react-fetch
Version:
[](LICENSE)
43 lines (40 loc) • 1.07 kB
JavaScript
/**
* react-fetch v1.43.1
*
* Copyright (c) resourge.
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/
import { throttleMethod } from '../utils/throttleMethod';
import { useEffectEvent } from './useEffectEvent';
import { useOnFocus } from './useOnFocus/useOnFocus';
const threshold = 10 * 60 * 1000;
const useOnFocusFetch = (fetch, onWindowFocus) => {
const _fetch = useEffectEvent(fetch);
useOnFocus(() => {
let dateNow = Date.now();
const fetchOnWindowFocus = throttleMethod(() => {
_fetch().finally(() => {
dateNow = Date.now();
});
}, 1000);
return {
focus: () => {
const now = Date.now();
if (now - dateNow <= threshold) {
return;
}
fetchOnWindowFocus();
},
blur: () => {
dateNow = Date.now();
},
clear: fetchOnWindowFocus.clear
};
}, onWindowFocus);
};
export { useOnFocusFetch };
//# sourceMappingURL=useOnFocusFetch.js.map