@aplus-frontend/ui
Version:
150 lines (149 loc) • 4.53 kB
JavaScript
import { defineComponent as U, getCurrentInstance as j, ref as C, onMounted as M, onUpdated as E, onBeforeUnmount as z, onBeforeUpdate as F } from "vue";
import v from "./keeper/index.mjs";
import { matches as k, getAliveKey as I, isNoValue as L, getVNodeObjByVNodeChild as y, getVNodeObjByVNodeArrayChildren as D, getVNodeChirldren as T, getFirstVNodeChirldren as K } from "./utils.mjs";
import { catchWrapper as w, measureWrapper as h } from "@aplus-frontend/utils";
import { LruCacher as O } from "./cacher/lru-cacher.mjs";
import { forceStopTags as W, ApKeepAliveProps as R } from "./interface.mjs";
import { throttle as x } from "lodash-unified";
import { useApKeepAliveProvide as _ } from "./hook.mjs";
const s = console, c = "|", ee = U({
name: "ApKeepAlive",
inheritAttrs: !1,
props: R(),
setup(n, { slots: B }) {
const f = j(), b = (e) => !(n.include && (!e || !k(n.include, e)) || n.exclude && e && k(n.exclude, e)), d = n.store || new O(n.max), a = C("");
let g = "";
const A = /* @__PURE__ */ new Set([...W, ...n.stopTags]), m = (e, o = "") => {
const t = [];
if (!e) return t;
const { VnodeName: i, vnodeSubPath: l } = I(
e,
a.value
);
l && (o += c + l);
const u = v[i];
if (u) {
const r = u.get(e);
!L(r) && t.push([o, r]);
}
if (A.has(i))
return n.debug && s.log("[ stop-VnodeName ] >", i, o), t;
if (e.children)
if (Array.isArray(e.children))
e.children.forEach((r) => {
r = y(r), r && t.push(...m(r, o));
});
else {
const r = D(e.children);
r && t.push(...m(r, o));
}
return e.component?.subTree && t.push(...m(e.component.subTree, o)), t;
}, p = async (e, o, t = "") => {
if (!e || !o || o?.size < 1) return;
const { VnodeName: i, vnodeSubPath: l } = I(
e,
a.value
);
l && (t += c + l);
const u = v[i];
if (u) {
n.debug && s.log("[ vnkeeper ] >", i, e.key);
const r = t.split(c).slice(-1).join(c), S = o.get(r);
await u.set(e, S);
}
if (A.has(i)) {
n.debug && s.log("[ stop VnodeName ] >", i, t);
return;
}
if (e.children)
if (Array.isArray(e.children))
e.children.forEach((r) => {
r = y(r), r && p(r, o, t);
});
else {
const r = D(e.children);
r && p(r, o, t);
}
e.component?.subTree && p(e.component.subTree, o, t);
}, N = x(() => {
w(() => {
const e = T(f), o = K(e), t = y(o);
if (!t || typeof t.type == "string" || (typeof t.type == "object" && "name" in t.type && (g = t.type.name || ""), a.value = t.key || g || "", !b(g)) || !a.value)
return;
const i = d.get(a.value);
p(t, i), s.log(
"resetDataInVNode",
f.vnode.key,
a.value,
i
);
});
}, 100, {
trailing: !1
});
M(() => {
if (n.debug) {
s.log("[ onMounted ] >", n.exclude), h(() => {
N();
});
return;
}
N();
}), E(() => {
if (n.debug) {
s.log("[ onUpdated ] >", n.exclude), h(() => {
N();
});
return;
}
N();
});
const V = x(() => {
w(() => {
const e = T(f), o = K(e), t = y(o);
if (!t) return;
if (!a.value) {
s.warn(
"ap-keep-alive 子组件 key 不存在,请检查组件是否设置了 key"
);
return;
}
if (!b(g)) return;
let i = m(t);
i = i.map((u) => [u[0].split(c).slice(-1).join(c), u[1]]);
const l = i.map((u) => u[0]);
l.length > new Set(l).size && s.warn(
"需要缓存的路径重复请合理设置组件key,保证在当前ApKeepAlive模块下唯一",
l
), d.set(a.value, new Map(i)), n.debug && s.log(
"[ loadDataInVNode ] >",
f.vnode.key,
a.value,
d.get(a.value)
);
});
}, 100, {
trailing: !1
});
return z(() => {
if (n.debug) {
s.log("[ onBeforeUnmount ] >"), h(() => {
V();
});
return;
}
V();
}), F(() => {
if (n.debug) {
s.log("[ onBeforeUpdate ] >"), h(() => {
V();
});
return;
}
V();
}), _(d, a), () => B.default?.();
}
});
export {
ee as default
};