UNPKG

@aplus-frontend/ui

Version:

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