UNPKG

vue-hooks-plus

Version:
34 lines (33 loc) 931 B
import { ref, watchEffect, unref, onScopeDispose } from "vue"; import limit from "../utils/limit"; import subscribeFocus from "../utils/subscribeFocus"; const useRefreshOnWindowFocusPlugin = (fetchInstance, { refreshOnWindowFocus, focusTimespan = 5e3 }) => { const unsubscribeRef = ref(); const stopSubscribe = () => { var _a; (_a = unsubscribeRef.value) == null ? void 0 : _a.call(unsubscribeRef); }; watchEffect((onInvalidate) => { if (unref(refreshOnWindowFocus)) { const limitRefresh = limit( fetchInstance.refresh.bind(fetchInstance), unref(focusTimespan) ); unsubscribeRef.value = subscribeFocus(() => { limitRefresh(); }); } onInvalidate(() => { stopSubscribe(); }); }); onScopeDispose(() => { stopSubscribe(); }); return { name: "refreshOnWindowFocusPlugin" }; }; export { useRefreshOnWindowFocusPlugin as default };