vue-hooks-plus
Version:
Vue hooks library
56 lines (55 loc) • 1.27 kB
JavaScript
const vue = require("vue");
const useAutoRunPlugin = (fetchInstance, { manual, ready = true, refreshDeps = [], refreshDepsAction }) => {
const hasAutoRun = vue.ref(false);
vue.watchEffect(() => {
if (!manual && fetchInstance.options.refreshDeps !== true) {
hasAutoRun.value = vue.unref(ready);
}
});
if (refreshDeps instanceof Array)
vue.watch(
[hasAutoRun, ...refreshDeps],
([autoRun]) => {
if (!autoRun)
return;
if (!manual && autoRun) {
if (refreshDepsAction) {
refreshDepsAction();
} else {
fetchInstance.refresh();
}
}
},
{
deep: true,
immediate: false
}
);
else
vue.watch(hasAutoRun, (h) => {
if (!manual && h) {
if (refreshDepsAction) {
refreshDepsAction();
} else {
fetchInstance.refresh();
}
}
});
return {
name: "autoRunPlugin",
onBefore: () => {
if (!vue.unref(ready)) {
return {
stopNow: true
};
}
}
};
};
useAutoRunPlugin.onInit = ({ ready = true, manual }) => {
return {
loading: !manual && vue.unref(ready)
};
};
module.exports = useAutoRunPlugin;
;