UNPKG

@aplus-frontend/ui

Version:

150 lines (149 loc) 4.53 kB
import { catchWrapper as v, measureWrapper as y } from "@aplus-frontend/utils"; import { throttle as k } from "lodash-unified"; 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 { LruCacher as L } from "./cacher/lru-cacher.mjs"; import { useApKeepAliveProvide as O } from "./hook.mjs"; import { forceStopTags as W, ApKeepAliveProps as R } from "./interface.mjs"; import I from "./keeper/index.mjs"; import { matches as D, getAliveKey as T, isNoValue as _, getVNodeObjByVNodeChild as h, getVNodeObjByVNodeArrayChildren as K, getVNodeChirldren as w, getFirstVNodeChirldren as x } from "./utils.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 || !D(n.include, e)) || n.exclude && e && D(n.exclude, e)), d = n.store || new L(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 } = T( e, a.value ); l && (o += c + l); const u = I[i]; if (u) { const r = u.get(e); !_(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 = h(r), r && t.push(...m(r, o)); }); else { const r = K(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 } = T( e, a.value ); l && (t += c + l); const u = I[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 = h(r), r && p(r, o, t); }); else { const r = K(e.children); r && p(r, o, t); } e.component?.subTree && p(e.component.subTree, o, t); }, N = k(() => { v(() => { const e = w(f), o = x(e), t = h(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), y(() => { N(); }); return; } N(); }), E(() => { if (n.debug) { s.log("[ onUpdated ] >", n.exclude), y(() => { N(); }); return; } N(); }); const V = k(() => { v(() => { const e = w(f), o = x(e), t = h(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 ] >"), y(() => { V(); }); return; } V(); }), F(() => { if (n.debug) { s.log("[ onBeforeUpdate ] >"), y(() => { V(); }); return; } V(); }), O(d, a), () => B.default?.(); } }); export { ee as default };