vue-hooks-plus
Version:
Vue hooks library
36 lines (35 loc) • 1.23 kB
JavaScript
const vue = require("vue");
const limit = require("../utils/limit");
const subscribeFocus = require("../utils/subscribeFocus");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const limit__default = /* @__PURE__ */ _interopDefaultLegacy(limit);
const subscribeFocus__default = /* @__PURE__ */ _interopDefaultLegacy(subscribeFocus);
const useRefreshOnWindowFocusPlugin = (fetchInstance, { refreshOnWindowFocus, focusTimespan = 5e3 }) => {
const unsubscribeRef = vue.ref();
const stopSubscribe = () => {
var _a;
(_a = unsubscribeRef.value) == null ? void 0 : _a.call(unsubscribeRef);
};
vue.watchEffect((onInvalidate) => {
if (vue.unref(refreshOnWindowFocus)) {
const limitRefresh = limit__default.default(
fetchInstance.refresh.bind(fetchInstance),
vue.unref(focusTimespan)
);
unsubscribeRef.value = subscribeFocus__default.default(() => {
limitRefresh();
});
}
onInvalidate(() => {
stopSubscribe();
});
});
vue.onScopeDispose(() => {
stopSubscribe();
});
return {
name: "refreshOnWindowFocusPlugin"
};
};
module.exports = useRefreshOnWindowFocusPlugin;
;