vue-hooks-plus
Version:
Vue hooks library
34 lines (33 loc) • 931 B
JavaScript
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
};