@aplus-frontend/ui
Version:
66 lines (65 loc) • 1.14 kB
JavaScript
import { isFunction as d } from "lodash-unified";
import { ref as o, watch as v, onMounted as h } from "vue";
const y = (e) => {
let l = 0;
const a = o(
e.treeData || []
), f = o(!1);
v(
() => e.treeData,
(t) => {
a.value = t;
},
{
deep: !0
}
);
function c(t, r) {
if (!t?.length)
return;
const u = e.fieldNames?.value || "value";
for (const n of t) {
if (n[u] === r[u])
return n;
if (n.children?.length)
return c(n.children, r);
}
}
async function i(t, r = !1) {
if (!d(e.request))
return;
f.value = !0, l += 1;
const u = l;
try {
r && (a.value = []);
const n = await e.request(t, e.params);
if (u !== l)
return;
if (t) {
const s = c(a.value, t);
s && (s.children = n);
} else
a.value = n;
} finally {
f.value = !1;
}
}
return h(() => {
e.lazy || i();
}), v(
() => e.params,
() => {
i();
},
{
deep: !0
}
), {
treeData: a,
request: i,
loading: f
};
};
export {
y as useRequest
};