From 59c0fab1a65070539f60e9745cd28119229d1f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=91=AB=E5=87=AF?= Date: Thu, 23 Sep 2021 16:03:32 +0800 Subject: [PATCH 1/6] fix: #23 --- src/utils.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/utils.js b/src/utils.js index 3abba21..fa5cd2b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -5,10 +5,7 @@ const PLACEHOLDER_VM = { __placeholder: true, }; export const resolvePushedVm = (currentVm) => { - if (!isDef(currentVm)) { - return undefined; - } - return !currentVm.$vnode.data.keepAlive ? PLACEHOLDER_VM : currentVm; + return (isDef(currentVm) && currentVm.$vnode.data.keepAlive) ? currentVm : PLACEHOLDER_VM; }; export const isPlaceHolderVm = (vm) => vm && !!vm.__placeholder; From 47b92a32dd22d966fc50858ece817050861a290c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=91=AB=E5=87=AF?= Date: Sun, 26 Sep 2021 14:58:33 +0800 Subject: [PATCH 2/6] chore:build for test fix issue 23 --- dist/helper.esm.js | 2 +- dist/helper.esm.js.map | 2 +- dist/index.js | 2 +- dist/index.js.map | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/helper.esm.js b/dist/helper.esm.js index 3c3778e..dcd21cd 100644 --- a/dist/helper.esm.js +++ b/dist/helper.esm.js @@ -1,2 +1,2 @@ -function t(t,e){for(var n=0;n0?t.history.current.matched[0].instances.default:void 0},s=function(t){if(Array.isArray(t))for(var n=0;n0&&(e="?"+e),e}(n.history.current.query);"hash"!==t&&(i+=o);var a=e(history.state)?history.state:{};a.id=r;var s=window.location.href.startsWith("file://");history.replaceState(a,"",s?null:i)},u="undefined"!=typeof window,p=function(){function t(){this.historyStackMap=[]}var e=t.prototype;return e.push=function(t,e){var n=this.historyStackMap[e];if(Array.isArray(n))!n.includes(t)&&n.push(t),this.historyStackMap[e]=n.filter((function(t){return!t._isDestroyed}));else{var r=[];r.push(t),this.historyStackMap[e]=r}},e.pop=function(){var t=this.historyStackMap.pop();Array.isArray(t)&&t.forEach((function(t){return t&&t.$keepAliveDestroy&&t.$keepAliveDestroy()}))},e.removeGreater=function(t){for(;this.historyStackMap.length>=t;)this.pop()},e.clear=function(){this.historyStackMap=[]},t}(),l=function(){function i(t){var e=t.Vue,n=t.router,r=t.replaceStay;this.Vue=e,this.router=n,this.router._stack=0,this.mode=n.mode,this.historyShouldChange=!1,this.isReplace=!1,this.replacePrePath=void 0,this.preStateId=0,this.pre=null,this.replaceStay=r||[],this.hacked=!1,this.historyStack=new p,this.init()}var c,u,l,f=i.prototype;return f.init=function(){this.routerHooks(),this.hackRouter()},f.routerHooks=function(){var t=this,r=this.router;r.afterEach((function(i,s){t.historyShouldChange=!0,t.Vue.nextTick((function(){var i,s=t.currentVm,c=function(t){if(e(t))return t.$vnode.data.keepAlive?t:n}(s);null===t.pre?t.onInitial(c):t.isReplace?t.onReplace(c):t.isPush?t.onPush(c):t.onBack(c),t.pre=s,t.preStateId=t.stackPointer,(i=c)&&i.__placeholder||(!function(t,e){var n=a(t);n&&n._vnode&&(n._vnode.parent.key=e)}(r,o(t.stackPointer,r)),!t.hacked&&s&&t.hackKeepAliveRender(s.$vnode.parent.componentInstance),t.historyShouldChange=!1)}))}))},f.hackRouter=function(){var t=this,n=this.router,r=n.replace;n.replace=function(i,o,a){!function(i,o,a){t.isReplace=!0,t.replacePrePath=n.history.current.path,r.call(n,i,o,(function(n){t.isReplace=!1,t.replacePrePath=void 0,e(a)&&a(n)}))}(i,o,a)};var i=n.go;n.go=function(e){return r=e,t.isReplace=!1,i.call(n,r);var r};var o=n.push;n.push=function(e,r,i){return function(e,r,i){if(t.isReplace=!1,!r&&!i&&"undefined"!=typeof Promise)return o.call(n,e,r,i);o.call(n,e,r,i)}(e,r,i)}},f.hackKeepAliveRender=function(t){!function(t,n){if(e(t)&&e(t.cache)&&e(t.keys)){var r=t.keys,i=t.cache;if(1===r.length){var o=i[r[0]];delete i[r[0]],r.splice(0,1),r.push(n),i[n]=o}}}(t,o(this.stackPointer,this.router));var n=t.$options.render,r=this,i=this.router;t.$options.render=function(){var t=this.$slots.default,a=s(t);return r.historyShouldChange?a&&!e(a.key)&&(r.isReplace?a.key=o(r.stackPointer,i):r.isPush?a.key=o(r.stackPointer+1,i):a.key=o(r.stackPointer-1,i)):a.key=o(r.stackPointer,i),n.apply(this,arguments)},this.hacked=!0},f.onInitial=function(t){var n=r();e(n)?this.setStackPointer(n):this.setState(0),this.historyStack.push(t,this.stackPointer)},f.onPush=function(t){this.setState(this.increaseStackPointer()),this.historyStack.push(t,this.stackPointer)},f.onBack=function(t){this.historyStack.pop(),this.decreaseStackPointer(),this.historyStack.push(t,this.stackPointer)},f.onReplace=function(t){!(e(this.replacePrePath)&&this.replaceStay.includes(this.replacePrePath))&&this.pre.$keepAliveDestroy(),this.setState(this.stackPointer),this.historyStack.push(t,this.stackPointer),this.isReplace=!1,this.replacePrePath=void 0},f.setState=function(t){this.setStackPointer(t),h(this.mode,this.router,t)},f.setStackPointer=function(t){this.router._stack=t},f.increaseStackPointer=function(){return this.router._stack+=1},f.decreaseStackPointer=function(){return this.router._stack-=1},c=i,(u=[{key:"currentVm",get:function(){return a(this.router)}},{key:"isPush",get:function(){if(!this.isReplace){var t=r();return!e(t)||this.preStateId<=t}return!1}},{key:"stackPointer",get:function(){return this.router._stack}}])&&t(c.prototype,u),l&&t(c,l),i}();var f=void 0;function d(t){var n,r;if(void 0!==t.Vue&&void 0!==t.router)return f||(u&&function(t){var e=t.replaceState;t.replaceState=function(n,r,i){var o=Object.assign({},t.state),a=Object.assign(o,n);e.call(t,a,r,i)};var n=t.pushState;t.pushState=function(e,r,i){var o=Object.assign({},t.state),a=Object.assign(o,e);n.call(t,a,r,i)}}(window.history),n=t.Vue,r=n.prototype.$destroy,n.prototype.$keepAliveDestroy=function(){if(this.$vnode&&this.$vnode.data.keepAlive&&this.$vnode.parent&&this.$vnode.parent.componentInstance&&this.$vnode.parent.componentInstance.cache&&this.$vnode.componentOptions){var t=e(this.$vnode.key)?this.$vnode.key:this.$vnode.componentOptions.Ctor.cid+(this.$vnode.componentOptions.tag?"::"+this.$vnode.componentOptions.tag:""),n=this.$vnode.parent.componentInstance.cache,i=this.$vnode.parent.componentInstance.keys;if(n[t]){if(i.length){var o=i.indexOf(t);o>-1&&i.splice(o,1)}delete n[t]}}r.apply(this,arguments)},f=new l(t));console.warn("warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper")}export default d; +function t(t,e){for(var n=0;n0?t.history.current.matched[0].instances.default:void 0},s=function(t){if(Array.isArray(t))for(var n=0;n=t;)this.pop()},e.clear=function(){this.historyStackMap=[]},t}(),p=function(){function i(t){var e=t.Vue,n=t.router,r=t.replaceStay;this.Vue=e,this.router=n,this.router._stack=0,this.mode=n.mode,this.historyShouldChange=!1,this.isReplace=!1,this.replacePrePath=void 0,this.preStateId=0,this.pre=null,this.replaceStay=r||[],this.hacked=!1,this.historyStack=new u,this.init()}var c,h,p,l=i.prototype;return l.init=function(){this.routerHooks(),this.hackRouter()},l.routerHooks=function(){var t=this,r=this.router;r.afterEach((function(i,s){t.historyShouldChange=!0,t.Vue.nextTick((function(){var i,s,c=t.currentVm,h=e(i=c)&&i.$vnode.data.keepAlive?i:n;null===t.pre?t.onInitial(h):t.isReplace?t.onReplace(h):t.isPush?t.onPush(h):t.onBack(h),t.pre=c,t.preStateId=t.stackPointer,(s=h)&&s.__placeholder||(!function(t,e){var n=a(t);n&&n._vnode&&(n._vnode.parent.key=e)}(r,o(t.stackPointer,r)),!t.hacked&&c&&t.hackKeepAliveRender(c.$vnode.parent.componentInstance),t.historyShouldChange=!1)}))}))},l.hackRouter=function(){var t=this,n=this.router,r=n.replace;n.replace=function(i,o,a){!function(i,o,a){t.isReplace=!0,t.replacePrePath=n.history.current.path,r.call(n,i,o,(function(n){t.isReplace=!1,t.replacePrePath=void 0,e(a)&&a(n)}))}(i,o,a)};var i=n.go;n.go=function(e){return r=e,t.isReplace=!1,i.call(n,r);var r};var o=n.push;n.push=function(e,r,i){return function(e,r,i){if(t.isReplace=!1,!r&&!i&&"undefined"!=typeof Promise)return o.call(n,e,r,i);o.call(n,e,r,i)}(e,r,i)}},l.hackKeepAliveRender=function(t){!function(t,n){if(e(t)&&e(t.cache)&&e(t.keys)){var r=t.keys,i=t.cache;if(1===r.length){var o=i[r[0]];delete i[r[0]],r.splice(0,1),r.push(n),i[n]=o}}}(t,o(this.stackPointer,this.router));var n=t.$options.render,r=this,i=this.router;t.$options.render=function(){var t=this.$slots.default,a=s(t);return r.historyShouldChange?a&&!e(a.key)&&(r.isReplace?a.key=o(r.stackPointer,i):r.isPush?a.key=o(r.stackPointer+1,i):a.key=o(r.stackPointer-1,i)):a.key=o(r.stackPointer,i),n.apply(this,arguments)},this.hacked=!0},l.onInitial=function(t){var n=r();e(n)?this.setStackPointer(n):this.setState(0),this.historyStack.push(t,this.stackPointer)},l.onPush=function(t){this.setState(this.increaseStackPointer()),this.historyStack.push(t,this.stackPointer)},l.onBack=function(t){this.historyStack.pop(),this.decreaseStackPointer(),this.historyStack.push(t,this.stackPointer)},l.onReplace=function(t){!(e(this.replacePrePath)&&this.replaceStay.includes(this.replacePrePath))&&this.pre.$keepAliveDestroy(),this.setState(this.stackPointer),this.historyStack.push(t,this.stackPointer),this.isReplace=!1,this.replacePrePath=void 0},l.setState=function(t){this.setStackPointer(t),function(t,n,r){var i=window.location,o=""+i.pathname+i.search+i.hash,a=e(history.state)?history.state:{};a.id=r;var s=window.location.href.startsWith("file://");history.replaceState(a,"",s?null:o)}(this.mode,this.router,t)},l.setStackPointer=function(t){this.router._stack=t},l.increaseStackPointer=function(){return this.router._stack+=1},l.decreaseStackPointer=function(){return this.router._stack-=1},c=i,(h=[{key:"currentVm",get:function(){return a(this.router)}},{key:"isPush",get:function(){if(!this.isReplace){var t=r();return!e(t)||this.preStateId<=t}return!1}},{key:"stackPointer",get:function(){return this.router._stack}}])&&t(c.prototype,h),p&&t(c,p),i}();var l=void 0;function f(t){var n,r;if(void 0!==t.Vue&&void 0!==t.router)return l||(h&&function(t){var e=t.replaceState;t.replaceState=function(n,r,i){var o=Object.assign({},t.state),a=Object.assign(o,n);e.call(t,a,r,i)};var n=t.pushState;t.pushState=function(e,r,i){var o=Object.assign({},t.state),a=Object.assign(o,e);n.call(t,a,r,i)}}(window.history),n=t.Vue,r=n.prototype.$destroy,n.prototype.$keepAliveDestroy=function(){if(this.$vnode&&this.$vnode.data.keepAlive&&this.$vnode.parent&&this.$vnode.parent.componentInstance&&this.$vnode.parent.componentInstance.cache&&this.$vnode.componentOptions){var t=e(this.$vnode.key)?this.$vnode.key:this.$vnode.componentOptions.Ctor.cid+(this.$vnode.componentOptions.tag?"::"+this.$vnode.componentOptions.tag:""),n=this.$vnode.parent.componentInstance.cache,i=this.$vnode.parent.componentInstance.keys;if(n[t]){if(i.length){var o=i.indexOf(t);o>-1&&i.splice(o,1)}delete n[t]}}r.apply(this,arguments)},l=new p(t));console.warn("warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper")}export default f; //# sourceMappingURL=helper.esm.js.map diff --git a/dist/helper.esm.js.map b/dist/helper.esm.js.map index cf46276..9362073 100644 --- a/dist/helper.esm.js.map +++ b/dist/helper.esm.js.map @@ -1 +1 @@ -{"version":3,"file":"helper.esm.js","sources":["../src/utils.js","../src/historyStack.js","../src/helper.js","../src/index.js","../src/extension.js"],"sourcesContent":["export const isDef = function (v) {\n return v !== undefined && v !== null;\n};\nconst PLACEHOLDER_VM = {\n __placeholder: true,\n};\nexport const resolvePushedVm = (currentVm) => {\n if (!isDef(currentVm)) {\n return undefined;\n }\n return !currentVm.$vnode.data.keepAlive ? PLACEHOLDER_VM : currentVm;\n};\nexport const isPlaceHolderVm = (vm) => vm && !!vm.__placeholder;\n\nexport const getStateId = function () {\n const state = getCurrentState();\n return isDef(state) ? state.id : undefined;\n};\n\nexport const getQuery = function (params) {\n let query = \"\";\n query = Object.keys(params)\n .map(\n (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`\n )\n .join(\"&\");\n if (query.length > 0) {\n query = `?${query}`;\n }\n return query;\n};\nconst getCurrentState = function () {\n return history.state;\n};\n\nexport const genKey = function (num, router) {\n return `keep-alive-vnode-key${Number(num)}${router.history.current.path}`;\n};\nexport const getCurrentVM = function (router) {\n return router.history.current.matched.length > 0\n ? router.history.current.matched[0].instances.default\n : undefined;\n};\nexport const setCurrentVnodeKey = function (router, key) {\n const current = getCurrentVM(router);\n if (current && current._vnode) {\n current._vnode.parent.key = key;\n }\n};\nexport const replaceFirstKeyAndCache = function (vm, key) {\n if (!isDef(vm) || !isDef(vm.cache) || !isDef(vm.keys)) {\n return;\n }\n const keys = vm.keys;\n const cache = vm.cache;\n if (keys.length === 1) {\n const vnode = cache[keys[0]];\n delete cache[keys[0]];\n keys.splice(0, 1);\n keys.push(key);\n cache[key] = vnode;\n }\n};\nexport const getFirstComponentChild = function (children) {\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length; i++) {\n const c = children[i];\n if (isDef(c) && (isDef(c.componentOptions) || isAsyncPlaceholder(c))) {\n return c;\n }\n }\n }\n};\nconst isAsyncPlaceholder = function (node) {\n return node.isComment && node.asyncFactory;\n};\n\nexport const replaceState = function (mode, router, id) {\n let path =\n window.location.pathname + (mode === \"hash\" ? window.location.hash : \"\");\n let query = getQuery(router.history.current.query);\n if (mode !== \"hash\") { //fixed issue#18: in hash mode issue\n path = path + query;\n }\n let state = isDef(history.state) ? history.state : {};\n state[\"id\"] = id;\n // optimize file:// URL\n const isFilSys = window.location.href.startsWith(\"file://\");\n history.replaceState(state, \"\", isFilSys ? null : path);\n};\n\nexport const inBrowser = typeof window !== 'undefined';","export default class HistoryStack {\n constructor() {\n this.historyStackMap = [];\n }\n push(vm, index) {\n // const cur = stackPointer();\n const stack = this.historyStackMap[index];\n if (Array.isArray(stack)) {\n !stack.includes(vm) && stack.push(vm);\n this.historyStackMap[index] = stack.filter((item) => !item._isDestroyed);\n } else {\n const vms = [];\n vms.push(vm);\n this.historyStackMap[index] = vms;\n }\n }\n pop() {\n const last = this.historyStackMap.pop();\n Array.isArray(last) &&\n last.forEach(\n (vm) => vm && vm.$keepAliveDestroy && vm.$keepAliveDestroy()\n );\n }\n removeGreater(index) {\n while (this.historyStackMap.length >= index) {\n this.pop();\n }\n }\n clear() {\n this.historyStackMap = [];\n }\n}\n","import {\n getCurrentVM,\n isDef,\n getStateId,\n resolvePushedVm,\n genKey,\n replaceFirstKeyAndCache,\n getFirstComponentChild,\n isPlaceHolderVm,\n setCurrentVnodeKey,\n replaceState,\n} from \"./utils\";\nimport HistoryStack from \"./historyStack\";\n\nexport default class VueRouterKeepAliveHelper{\n constructor({ Vue, router, replaceStay }) {\n this.Vue = Vue;\n this.router = router;\n this.router._stack = 0;\n this.mode = router.mode; // hash or history\n this.historyShouldChange = false;\n this.isReplace = false;\n this.replacePrePath = undefined;\n this.preStateId = 0;\n this.pre = null;\n this.replaceStay = replaceStay || [];\n this.hacked = false;\n this.historyStack = new HistoryStack();\n this.init();\n }\n init() {\n this.routerHooks();\n this.hackRouter();\n }\n /**\n * use afterEach hook to set state.key and add the reference of vm to the historyStack\n */\n routerHooks() {\n const router = this.router;\n router.afterEach((to, from) => {\n this.historyShouldChange = true;\n // get the vm instance after render\n this.Vue.nextTick(() => {\n const current = this.currentVm;\n const pendingToPushVm = resolvePushedVm(current);\n if (this.pre === null) {\n this.onInitial(pendingToPushVm);\n } else if (this.isReplace) {\n this.onReplace(pendingToPushVm);\n } else if (this.isPush) {\n this.onPush(pendingToPushVm);\n } else {\n this.onBack(pendingToPushVm);\n }\n this.pre = current;\n this.preStateId = this.stackPointer;\n if (!isPlaceHolderVm(pendingToPushVm)) {\n setCurrentVnodeKey(router, genKey(this.stackPointer, router));\n if (!this.hacked && current) {\n this.hackKeepAliveRender(current.$vnode.parent.componentInstance);\n }\n this.historyShouldChange = false;\n }\n });\n });\n }\n /**\n * @description hack router go , replace and push functions to tell replace from back and push\n */\n hackRouter() {\n const router = this.router;\n const rtmp = router.replace;\n const rtmpf = (location, onComplete, onAbort) => {\n this.isReplace = true;\n this.replacePrePath = router.history.current.path;\n rtmp.call(router, location, onComplete, (e) => {\n this.isReplace = false;\n this.replacePrePath = undefined;\n isDef(onAbort) && onAbort(e);\n });\n };\n router.replace = function (ocation, onComplete, onAbort) {\n rtmpf(ocation, onComplete, onAbort);\n };\n\n const gstmp = router.go;\n const gstmpf = (number) => {\n this.isReplace = false;\n return gstmp.call(router, number);\n };\n router.go = function (num) {\n return gstmpf(num);\n };\n const pstmp = router.push;\n const pstmpf = (location, onComplete, onAbort) => {\n this.isReplace = false;\n if (!onComplete && !onAbort && typeof Promise !== \"undefined\") {\n return pstmp.call(router, location, onComplete, onAbort);\n } else {\n pstmp.call(router, location, onComplete, onAbort);\n }\n };\n router.push = function (location, onComplete, onAbort) {\n return pstmpf(location, onComplete, onAbort);\n };\n }\n /**\n * @description hack the render function of keep-alive component, modify the key of vnode for cache\n * @param {*} vm keep-alive component instance\n */\n hackKeepAliveRender(vm) {\n // modify the first keep alive key and cache\n replaceFirstKeyAndCache(vm, genKey(this.stackPointer, this.router));\n\n const tmp = vm.$options.render;\n const self = this;\n const router = this.router;\n vm.$options.render = function () {\n const slot = this.$slots.default;\n const vnode = getFirstComponentChild(slot); // vnode is a keep-alive-component-vnode\n if (self.historyShouldChange) {\n if (vnode && !isDef(vnode.key)) {\n if (self.isReplace) {\n vnode.key = genKey(self.stackPointer, router);\n } else if (self.isPush) {\n vnode.key = genKey(self.stackPointer + 1, router);\n } else {\n vnode.key = genKey(self.stackPointer - 1, router);\n }\n }\n } else {\n // when historyShouldChange is false should rerender only, should not create new vm ,use the same vnode.key issue#7\n vnode.key = genKey(self.stackPointer, router);\n }\n return tmp.apply(this, arguments);\n };\n this.hacked = true;\n }\n /********** callback functions ************/\n onInitial(vm) {\n const currentStateId = getStateId();\n if (isDef(currentStateId)) {\n this.setStackPointer(currentStateId);\n } else {\n this.setState(0);\n }\n this.historyStack.push(vm, this.stackPointer);\n }\n onPush(vm) {\n this.setState(this.increaseStackPointer());\n this.historyStack.push(vm, this.stackPointer);\n }\n onBack(vm) {\n this.historyStack.pop();\n this.decreaseStackPointer();\n this.historyStack.push(vm, this.stackPointer);\n }\n onReplace(vm) {\n const shouldDestroy = !(\n isDef(this.replacePrePath) &&\n this.replaceStay.includes(this.replacePrePath)\n );\n if (shouldDestroy) {\n this.pre.$keepAliveDestroy();\n }\n this.setState(this.stackPointer);\n this.historyStack.push(vm, this.stackPointer);\n this.isReplace = false;\n this.replacePrePath = undefined;\n }\n get currentVm() {\n return getCurrentVM(this.router);\n }\n get isPush() {\n if (!this.isReplace) {\n const stateId = getStateId();\n return !isDef(stateId) || this.preStateId <= stateId;\n }\n return false;\n }\n get stackPointer() {\n return this.router._stack;\n }\n setState(id) {\n this.setStackPointer(id);\n replaceState(this.mode, this.router, id);\n }\n setStackPointer(val) {\n this.router._stack = val;\n }\n increaseStackPointer() {\n return (this.router._stack += 1);\n }\n decreaseStackPointer() {\n return (this.router._stack -= 1);\n }\n}\n","import Helper from \"./helper\";\nimport { extendVue, extendHistory } from \"./extension\";\nimport { inBrowser } from \"./utils\";\n\nlet singleton = undefined;\n// TODO: 1. abstract mode support\nexport default function createHelper(config) {\n if (config.Vue === undefined || config.router === undefined) {\n console.warn(\n \"warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper\"\n );\n return;\n }\n if(singleton) {\n return singleton;\n }\n if (inBrowser) {\n extendHistory(window.history);\n }\n extendVue(config.Vue);\n return (singleton = new Helper(config));\n}","import { isDef } from \"./utils\";\n\nexport function extendHistory(history) {\n const rstmp = history.replaceState;\n history.replaceState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n rstmp.call(history, s, op, path);\n }\n const historyPushState = history.pushState;\n history.pushState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n historyPushState.call(history, s, op, path);\n }\n}\n\nexport function extendVue(Vue) {\n const dtmp = Vue.prototype.$destroy;\n /**\n * @description remove the cache in component before invoke $destroy\n */\n Vue.prototype.$keepAliveDestroy = function () {\n if (this.$vnode && this.$vnode.data.keepAlive) {\n if (\n this.$vnode.parent &&\n this.$vnode.parent.componentInstance &&\n this.$vnode.parent.componentInstance.cache\n ) {\n if (this.$vnode.componentOptions) {\n const key = !isDef(this.$vnode.key)\n ? this.$vnode.componentOptions.Ctor.cid +\n (this.$vnode.componentOptions.tag\n ? `::${this.$vnode.componentOptions.tag}`\n : \"\")\n : this.$vnode.key;\n const cache = this.$vnode.parent.componentInstance.cache;\n const keys = this.$vnode.parent.componentInstance.keys;\n if (cache[key]) {\n if (keys.length) {\n const index = keys.indexOf(key);\n if (index > -1) {\n keys.splice(index, 1);\n }\n }\n delete cache[key];\n }\n }\n }\n }\n dtmp.apply(this, arguments);\n };\n}\n"],"names":["isDef","v","PLACEHOLDER_VM","__placeholder","getStateId","state","getCurrentState","id","undefined","history","genKey","num","router","Number","current","path","getCurrentVM","matched","length","instances","default","getFirstComponentChild","children","Array","isArray","i","c","componentOptions","isAsyncPlaceholder","node","isComment","asyncFactory","replaceState","mode","window","location","pathname","hash","query","params","Object","keys","map","key","encodeURIComponent","join","getQuery","isFilSys","href","startsWith","inBrowser","HistoryStack","historyStackMap","push","vm","index","stack","this","includes","filter","item","_isDestroyed","vms","pop","last","forEach","$keepAliveDestroy","removeGreater","clear","VueRouterKeepAliveHelper","Vue","replaceStay","_stack","historyShouldChange","isReplace","replacePrePath","preStateId","pre","hacked","historyStack","init","routerHooks","hackRouter","afterEach","to","from","_this","nextTick","currentVm","pendingToPushVm","$vnode","data","keepAlive","resolvePushedVm","onInitial","onReplace","isPush","onPush","onBack","stackPointer","_vnode","parent","setCurrentVnodeKey","hackKeepAliveRender","componentInstance","rtmp","replace","ocation","onComplete","onAbort","_this2","call","e","rtmpf","gstmp","go","number","pstmp","Promise","pstmpf","cache","vnode","splice","replaceFirstKeyAndCache","tmp","$options","render","self","slot","$slots","apply","arguments","currentStateId","setStackPointer","setState","increaseStackPointer","decreaseStackPointer","val","stateId","singleton","createHelper","config","dtmp","rstmp","op","old","assign","s","historyPushState","pushState","extendHistory","prototype","$destroy","Ctor","cid","tag","indexOf","Helper","console","warn"],"mappings":"sKAAO,IAAMA,EAAQ,SAAUC,UACtBA,MAAAA,GAEHC,EAAiB,CACrBC,eAAe,GAUJC,EAAa,eAClBC,EAAQC,WACPN,EAAMK,GAASA,EAAME,QAAKC,GAe7BF,EAAkB,kBACfG,QAAQJ,OAGJK,EAAS,SAAUC,EAAKC,gCACLC,OAAOF,GAAOC,EAAOH,QAAQK,QAAQC,MAExDC,EAAe,SAAUJ,UAC7BA,EAAOH,QAAQK,QAAQG,QAAQC,OAAS,EAC3CN,EAAOH,QAAQK,QAAQG,QAAQ,GAAGE,UAAUC,aAC5CZ,GAsBOa,EAAyB,SAAUC,MAC1CC,MAAMC,QAAQF,OACX,IAAIG,EAAI,EAAGA,EAAIH,EAASJ,OAAQO,IAAK,KAClCC,EAAIJ,EAASG,MACfzB,EAAM0B,KAAO1B,EAAM0B,EAAEC,mBAAqBC,EAAmBF,WACxDA,IAKTE,EAAqB,SAAUC,UAC5BA,EAAKC,WAAaD,EAAKE,cAGnBC,EAAe,SAAUC,EAAMrB,EAAQL,OAC9CQ,EACFmB,OAAOC,SAASC,UAAqB,SAATH,EAAkBC,OAAOC,SAASE,KAAO,IACnEC,EA7DkB,SAAUC,OAC5BD,EAAQ,UACZA,EAAQE,OAAOC,KAAKF,GACjBG,KACC,SAACC,UAAWC,mBAAmBD,OAAQC,mBAAmBL,EAAOI,OAElEE,KAAK,MACE3B,OAAS,IACjBoB,MAAYA,GAEPA,EAmDKQ,CAASlC,EAAOH,QAAQK,QAAQwB,OAC/B,SAATL,IACFlB,GAAcuB,OAEZjC,EAAQL,EAAMS,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AOxE,EAAIiF,UAAUC,SAI3BlF,EAAIiF,UAAUrF,kBAAoB,cAC5BT,KAAKiC,QAAUjC,KAAKiC,OAAOC,KAAKC,WAEhCnC,KAAKiC,OAAOW,QACZ5C,KAAKiC,OAAOW,OAAOG,mBACnB/C,KAAKiC,OAAOW,OAAOG,kBAAkBgB,OAEjC/D,KAAKiC,OAAO/D,iBAAkB,KAC1BgB,EAAO3C,EAAMyD,KAAKiC,OAAO/C,KAK3Bc,KAAKiC,OAAO/C,IAJZc,KAAKiC,OAAO/D,iBAAiB8H,KAAKC,KACjCjG,KAAKiC,OAAO/D,iBAAiBgI,SACrBlG,KAAKiC,OAAO/D,iBAAiBgI,IAClC,IAEFnC,EAAQ/D,KAAKiC,OAAOW,OAAOG,kBAAkBgB,MAC7C/E,EAAOgB,KAAKiC,OAAOW,OAAOG,kBAAkB/D,QAC9C+E,EAAM7E,GAAM,IACVF,EAAKvB,OAAQ,KACTqC,EAAQd,EAAKmH,QAAQjH,GACvBY,GAAS,GACXd,EAAKiF,OAAOnE,EAAO,UAGhBiE,EAAM7E,IAKrBmG,EAAKZ,MAAMzE,KAAM0E,YD9BXQ,EAAY,IAAIkB,EAAOhB,IAZ7BiB,QAAQC,KACN"} \ No newline at end of file +{"version":3,"file":"helper.esm.js","sources":["../src/utils.js","../src/historyStack.js","../src/helper.js","../src/index.js","../src/extension.js"],"sourcesContent":["export const isDef = function (v) {\n return v !== undefined && v !== null;\n};\nconst PLACEHOLDER_VM = {\n __placeholder: true,\n};\nexport const resolvePushedVm = (currentVm) => {\n return (isDef(currentVm) && currentVm.$vnode.data.keepAlive) ? currentVm : PLACEHOLDER_VM;\n};\nexport const isPlaceHolderVm = (vm) => vm && !!vm.__placeholder;\n\nexport const getStateId = function () {\n const state = getCurrentState();\n return isDef(state) ? state.id : undefined;\n};\n\nexport const getQuery = function (params) {\n let query = \"\";\n query = Object.keys(params)\n .map(\n (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`\n )\n .join(\"&\");\n if (query.length > 0) {\n query = `?${query}`;\n }\n return query;\n};\nconst getCurrentState = function () {\n return history.state;\n};\n\nexport const genKey = function (num, router) {\n return `keep-alive-vnode-key${Number(num)}${router.history.current.path}`;\n};\nexport const getCurrentVM = function (router) {\n return router.history.current.matched.length > 0\n ? router.history.current.matched[0].instances.default\n : undefined;\n};\nexport const setCurrentVnodeKey = function (router, key) {\n const current = getCurrentVM(router);\n if (current && current._vnode) {\n current._vnode.parent.key = key;\n }\n};\nexport const replaceFirstKeyAndCache = function (vm, key) {\n if (!isDef(vm) || !isDef(vm.cache) || !isDef(vm.keys)) {\n return;\n }\n const keys = vm.keys;\n const cache = vm.cache;\n if (keys.length === 1) {\n const vnode = cache[keys[0]];\n delete cache[keys[0]];\n keys.splice(0, 1);\n keys.push(key);\n cache[key] = vnode;\n }\n};\nexport const getFirstComponentChild = function (children) {\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length; i++) {\n const c = children[i];\n if (isDef(c) && (isDef(c.componentOptions) || isAsyncPlaceholder(c))) {\n return c;\n }\n }\n }\n};\nconst isAsyncPlaceholder = function (node) {\n return node.isComment && node.asyncFactory;\n};\n\nexport const replaceState = function (mode, router, id) {\n const { pathname, search, hash } = window.location;\n let path = `${pathname}${search}${hash}`;\n let state = isDef(history.state) ? history.state : {};\n state[\"id\"] = id;\n // optimize file:// URL\n const isFilSys = window.location.href.startsWith(\"file://\");\n history.replaceState(state, \"\", isFilSys ? null : path);\n};\n\nexport const inBrowser = typeof window !== \"undefined\";\n","export default class HistoryStack {\n constructor() {\n this.historyStackMap = [];\n }\n push(vm, index) {\n // const cur = stackPointer();\n const stack = this.historyStackMap[index];\n if (Array.isArray(stack)) {\n !stack.includes(vm) && stack.push(vm);\n this.historyStackMap[index] = stack.filter((item) => !item._isDestroyed);\n } else {\n const vms = [];\n vms.push(vm);\n this.historyStackMap[index] = vms;\n }\n }\n pop() {\n const last = this.historyStackMap.pop();\n Array.isArray(last) &&\n last.forEach(\n (vm) => vm && vm.$keepAliveDestroy && vm.$keepAliveDestroy()\n );\n }\n removeGreater(index) {\n while (this.historyStackMap.length >= index) {\n this.pop();\n }\n }\n clear() {\n this.historyStackMap = [];\n }\n}\n","import {\n getCurrentVM,\n isDef,\n getStateId,\n resolvePushedVm,\n genKey,\n replaceFirstKeyAndCache,\n getFirstComponentChild,\n isPlaceHolderVm,\n setCurrentVnodeKey,\n replaceState,\n} from \"./utils\";\nimport HistoryStack from \"./historyStack\";\n\nexport default class VueRouterKeepAliveHelper{\n constructor({ Vue, router, replaceStay }) {\n this.Vue = Vue;\n this.router = router;\n this.router._stack = 0;\n this.mode = router.mode; // hash or history\n this.historyShouldChange = false;\n this.isReplace = false;\n this.replacePrePath = undefined;\n this.preStateId = 0;\n this.pre = null;\n this.replaceStay = replaceStay || [];\n this.hacked = false;\n this.historyStack = new HistoryStack();\n this.init();\n }\n init() {\n this.routerHooks();\n this.hackRouter();\n }\n /**\n * use afterEach hook to set state.key and add the reference of vm to the historyStack\n */\n routerHooks() {\n const router = this.router;\n router.afterEach((to, from) => {\n this.historyShouldChange = true;\n // get the vm instance after render\n this.Vue.nextTick(() => {\n const current = this.currentVm;\n const pendingToPushVm = resolvePushedVm(current);\n if (this.pre === null) {\n this.onInitial(pendingToPushVm);\n } else if (this.isReplace) {\n this.onReplace(pendingToPushVm);\n } else if (this.isPush) {\n this.onPush(pendingToPushVm);\n } else {\n this.onBack(pendingToPushVm);\n }\n this.pre = current;\n this.preStateId = this.stackPointer;\n if (!isPlaceHolderVm(pendingToPushVm)) {\n setCurrentVnodeKey(router, genKey(this.stackPointer, router));\n if (!this.hacked && current) {\n this.hackKeepAliveRender(current.$vnode.parent.componentInstance);\n }\n this.historyShouldChange = false;\n }\n });\n });\n }\n /**\n * @description hack router go , replace and push functions to tell replace from back and push\n */\n hackRouter() {\n const router = this.router;\n const rtmp = router.replace;\n const rtmpf = (location, onComplete, onAbort) => {\n this.isReplace = true;\n this.replacePrePath = router.history.current.path;\n rtmp.call(router, location, onComplete, (e) => {\n this.isReplace = false;\n this.replacePrePath = undefined;\n isDef(onAbort) && onAbort(e);\n });\n };\n router.replace = function (ocation, onComplete, onAbort) {\n rtmpf(ocation, onComplete, onAbort);\n };\n\n const gstmp = router.go;\n const gstmpf = (number) => {\n this.isReplace = false;\n return gstmp.call(router, number);\n };\n router.go = function (num) {\n return gstmpf(num);\n };\n const pstmp = router.push;\n const pstmpf = (location, onComplete, onAbort) => {\n this.isReplace = false;\n if (!onComplete && !onAbort && typeof Promise !== \"undefined\") {\n return pstmp.call(router, location, onComplete, onAbort);\n } else {\n pstmp.call(router, location, onComplete, onAbort);\n }\n };\n router.push = function (location, onComplete, onAbort) {\n return pstmpf(location, onComplete, onAbort);\n };\n }\n /**\n * @description hack the render function of keep-alive component, modify the key of vnode for cache\n * @param {*} vm keep-alive component instance\n */\n hackKeepAliveRender(vm) {\n // modify the first keep alive key and cache\n replaceFirstKeyAndCache(vm, genKey(this.stackPointer, this.router));\n\n const tmp = vm.$options.render;\n const self = this;\n const router = this.router;\n vm.$options.render = function () {\n const slot = this.$slots.default;\n const vnode = getFirstComponentChild(slot); // vnode is a keep-alive-component-vnode\n if (self.historyShouldChange) {\n if (vnode && !isDef(vnode.key)) {\n if (self.isReplace) {\n vnode.key = genKey(self.stackPointer, router);\n } else if (self.isPush) {\n vnode.key = genKey(self.stackPointer + 1, router);\n } else {\n vnode.key = genKey(self.stackPointer - 1, router);\n }\n }\n } else {\n // when historyShouldChange is false should rerender only, should not create new vm ,use the same vnode.key issue#7\n vnode.key = genKey(self.stackPointer, router);\n }\n return tmp.apply(this, arguments);\n };\n this.hacked = true;\n }\n /********** callback functions ************/\n onInitial(vm) {\n const currentStateId = getStateId();\n if (isDef(currentStateId)) {\n this.setStackPointer(currentStateId);\n } else {\n this.setState(0);\n }\n this.historyStack.push(vm, this.stackPointer);\n }\n onPush(vm) {\n this.setState(this.increaseStackPointer());\n this.historyStack.push(vm, this.stackPointer);\n }\n onBack(vm) {\n this.historyStack.pop();\n this.decreaseStackPointer();\n this.historyStack.push(vm, this.stackPointer);\n }\n onReplace(vm) {\n const shouldDestroy = !(\n isDef(this.replacePrePath) &&\n this.replaceStay.includes(this.replacePrePath)\n );\n if (shouldDestroy) {\n this.pre.$keepAliveDestroy();\n }\n this.setState(this.stackPointer);\n this.historyStack.push(vm, this.stackPointer);\n this.isReplace = false;\n this.replacePrePath = undefined;\n }\n get currentVm() {\n return getCurrentVM(this.router);\n }\n get isPush() {\n if (!this.isReplace) {\n const stateId = getStateId();\n return !isDef(stateId) || this.preStateId <= stateId;\n }\n return false;\n }\n get stackPointer() {\n return this.router._stack;\n }\n setState(id) {\n this.setStackPointer(id);\n replaceState(this.mode, this.router, id);\n }\n setStackPointer(val) {\n this.router._stack = val;\n }\n increaseStackPointer() {\n return (this.router._stack += 1);\n }\n decreaseStackPointer() {\n return (this.router._stack -= 1);\n }\n}\n","import Helper from \"./helper\";\nimport { extendVue, extendHistory } from \"./extension\";\nimport { inBrowser } from \"./utils\";\n\nlet singleton = undefined;\n// TODO: 1. abstract mode support\nexport default function createHelper(config) {\n if (config.Vue === undefined || config.router === undefined) {\n console.warn(\n \"warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper\"\n );\n return;\n }\n if(singleton) {\n return singleton;\n }\n if (inBrowser) {\n extendHistory(window.history);\n }\n extendVue(config.Vue);\n return (singleton = new Helper(config));\n}","import { isDef } from \"./utils\";\n\nexport function extendHistory(history) {\n const rstmp = history.replaceState;\n history.replaceState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n rstmp.call(history, s, op, path);\n }\n const historyPushState = history.pushState;\n history.pushState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n historyPushState.call(history, s, op, path);\n }\n}\n\nexport function extendVue(Vue) {\n const dtmp = Vue.prototype.$destroy;\n /**\n * @description remove the cache in component before invoke $destroy\n */\n Vue.prototype.$keepAliveDestroy = function () {\n if (this.$vnode && this.$vnode.data.keepAlive) {\n if (\n this.$vnode.parent &&\n this.$vnode.parent.componentInstance &&\n this.$vnode.parent.componentInstance.cache\n ) {\n if (this.$vnode.componentOptions) {\n const key = !isDef(this.$vnode.key)\n ? this.$vnode.componentOptions.Ctor.cid +\n (this.$vnode.componentOptions.tag\n ? `::${this.$vnode.componentOptions.tag}`\n : \"\")\n : this.$vnode.key;\n const cache = this.$vnode.parent.componentInstance.cache;\n const keys = this.$vnode.parent.componentInstance.keys;\n if (cache[key]) {\n if (keys.length) {\n const index = keys.indexOf(key);\n if (index > -1) {\n keys.splice(index, 1);\n }\n }\n delete cache[key];\n }\n }\n }\n }\n dtmp.apply(this, arguments);\n };\n}\n"],"names":["isDef","v","PLACEHOLDER_VM","__placeholder","getStateId","state","getCurrentState","id","undefined","history","genKey","num","router","Number","current","path","getCurrentVM","matched","length","instances","default","getFirstComponentChild","children","Array","isArray","i","c","componentOptions","isAsyncPlaceholder","node","isComment","asyncFactory","inBrowser","window","HistoryStack","historyStackMap","push","vm","index","stack","this","includes","filter","item","_isDestroyed","vms","pop","last","forEach","$keepAliveDestroy","removeGreater","clear","VueRouterKeepAliveHelper","Vue","replaceStay","_stack","mode","historyShouldChange","isReplace","replacePrePath","preStateId","pre","hacked","historyStack","init","routerHooks","hackRouter","afterEach","to","from","_this","nextTick","currentVm","pendingToPushVm","$vnode","data","keepAlive","onInitial","onReplace","isPush","onPush","onBack","stackPointer","key","_vnode","parent","setCurrentVnodeKey","hackKeepAliveRender","componentInstance","rtmp","replace","ocation","onComplete","onAbort","location","_this2","call","e","rtmpf","gstmp","go","number","pstmp","Promise","pstmpf","cache","keys","vnode","splice","replaceFirstKeyAndCache","tmp","$options","render","self","slot","$slots","apply","arguments","currentStateId","setStackPointer","setState","increaseStackPointer","decreaseStackPointer","pathname","search","hash","isFilSys","href","startsWith","replaceState","val","stateId","singleton","createHelper","config","dtmp","rstmp","op","old","Object","assign","s","historyPushState","pushState","extendHistory","prototype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tB,GAAM,IACVuB,EAAKxF,OAAQ,KACToB,EAAQoE,EAAK4C,QAAQnE,GACvB7C,GAAS,GACXoE,EAAKE,OAAOtE,EAAO,UAGhBmE,EAAMtB,IAKrBoD,EAAKnB,MAAM5E,KAAM6E,YD9BXe,EAAY,IAAImB,EAAOjB,IAZ7BkB,QAAQC,KACN"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index ea80e76..90fa39f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,2 +1,2 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).createHelper=e()}(this,(function(){"use strict";function t(t,e){for(var n=0;n0?t.history.current.matched[0].instances.default:void 0},a=function(t){if(Array.isArray(t))for(var n=0;n0&&(e="?"+e),e}(n.history.current.query);"hash"!==t&&(i+=o);var s=e(history.state)?history.state:{};s.id=r;var a=window.location.href.startsWith("file://");history.replaceState(s,"",a?null:i)},u="undefined"!=typeof window,p=function(){function t(){this.historyStackMap=[]}var e=t.prototype;return e.push=function(t,e){var n=this.historyStackMap[e];if(Array.isArray(n))!n.includes(t)&&n.push(t),this.historyStackMap[e]=n.filter((function(t){return!t._isDestroyed}));else{var r=[];r.push(t),this.historyStackMap[e]=r}},e.pop=function(){var t=this.historyStackMap.pop();Array.isArray(t)&&t.forEach((function(t){return t&&t.$keepAliveDestroy&&t.$keepAliveDestroy()}))},e.removeGreater=function(t){for(;this.historyStackMap.length>=t;)this.pop()},e.clear=function(){this.historyStackMap=[]},t}(),l=function(){function i(t){var e=t.Vue,n=t.router,r=t.replaceStay;this.Vue=e,this.router=n,this.router._stack=0,this.mode=n.mode,this.historyShouldChange=!1,this.isReplace=!1,this.replacePrePath=void 0,this.preStateId=0,this.pre=null,this.replaceStay=r||[],this.hacked=!1,this.historyStack=new p,this.init()}var c,u,l,f=i.prototype;return f.init=function(){this.routerHooks(),this.hackRouter()},f.routerHooks=function(){var t=this,r=this.router;r.afterEach((function(i,a){t.historyShouldChange=!0,t.Vue.nextTick((function(){var i,a=t.currentVm,c=function(t){if(e(t))return t.$vnode.data.keepAlive?t:n}(a);null===t.pre?t.onInitial(c):t.isReplace?t.onReplace(c):t.isPush?t.onPush(c):t.onBack(c),t.pre=a,t.preStateId=t.stackPointer,(i=c)&&i.__placeholder||(!function(t,e){var n=s(t);n&&n._vnode&&(n._vnode.parent.key=e)}(r,o(t.stackPointer,r)),!t.hacked&&a&&t.hackKeepAliveRender(a.$vnode.parent.componentInstance),t.historyShouldChange=!1)}))}))},f.hackRouter=function(){var t=this,n=this.router,r=n.replace;n.replace=function(i,o,s){!function(i,o,s){t.isReplace=!0,t.replacePrePath=n.history.current.path,r.call(n,i,o,(function(n){t.isReplace=!1,t.replacePrePath=void 0,e(s)&&s(n)}))}(i,o,s)};var i=n.go;n.go=function(e){return r=e,t.isReplace=!1,i.call(n,r);var r};var o=n.push;n.push=function(e,r,i){return function(e,r,i){if(t.isReplace=!1,!r&&!i&&"undefined"!=typeof Promise)return o.call(n,e,r,i);o.call(n,e,r,i)}(e,r,i)}},f.hackKeepAliveRender=function(t){!function(t,n){if(e(t)&&e(t.cache)&&e(t.keys)){var r=t.keys,i=t.cache;if(1===r.length){var o=i[r[0]];delete i[r[0]],r.splice(0,1),r.push(n),i[n]=o}}}(t,o(this.stackPointer,this.router));var n=t.$options.render,r=this,i=this.router;t.$options.render=function(){var t=this.$slots.default,s=a(t);return r.historyShouldChange?s&&!e(s.key)&&(r.isReplace?s.key=o(r.stackPointer,i):r.isPush?s.key=o(r.stackPointer+1,i):s.key=o(r.stackPointer-1,i)):s.key=o(r.stackPointer,i),n.apply(this,arguments)},this.hacked=!0},f.onInitial=function(t){var n=r();e(n)?this.setStackPointer(n):this.setState(0),this.historyStack.push(t,this.stackPointer)},f.onPush=function(t){this.setState(this.increaseStackPointer()),this.historyStack.push(t,this.stackPointer)},f.onBack=function(t){this.historyStack.pop(),this.decreaseStackPointer(),this.historyStack.push(t,this.stackPointer)},f.onReplace=function(t){!(e(this.replacePrePath)&&this.replaceStay.includes(this.replacePrePath))&&this.pre.$keepAliveDestroy(),this.setState(this.stackPointer),this.historyStack.push(t,this.stackPointer),this.isReplace=!1,this.replacePrePath=void 0},f.setState=function(t){this.setStackPointer(t),h(this.mode,this.router,t)},f.setStackPointer=function(t){this.router._stack=t},f.increaseStackPointer=function(){return this.router._stack+=1},f.decreaseStackPointer=function(){return this.router._stack-=1},c=i,(u=[{key:"currentVm",get:function(){return s(this.router)}},{key:"isPush",get:function(){if(!this.isReplace){var t=r();return!e(t)||this.preStateId<=t}return!1}},{key:"stackPointer",get:function(){return this.router._stack}}])&&t(c.prototype,u),l&&t(c,l),i}();var f=void 0;return function(t){var n,r;if(void 0!==t.Vue&&void 0!==t.router)return f||(u&&function(t){var e=t.replaceState;t.replaceState=function(n,r,i){var o=Object.assign({},t.state),s=Object.assign(o,n);e.call(t,s,r,i)};var n=t.pushState;t.pushState=function(e,r,i){var o=Object.assign({},t.state),s=Object.assign(o,e);n.call(t,s,r,i)}}(window.history),n=t.Vue,r=n.prototype.$destroy,n.prototype.$keepAliveDestroy=function(){if(this.$vnode&&this.$vnode.data.keepAlive&&this.$vnode.parent&&this.$vnode.parent.componentInstance&&this.$vnode.parent.componentInstance.cache&&this.$vnode.componentOptions){var t=e(this.$vnode.key)?this.$vnode.key:this.$vnode.componentOptions.Ctor.cid+(this.$vnode.componentOptions.tag?"::"+this.$vnode.componentOptions.tag:""),n=this.$vnode.parent.componentInstance.cache,i=this.$vnode.parent.componentInstance.keys;if(n[t]){if(i.length){var o=i.indexOf(t);o>-1&&i.splice(o,1)}delete n[t]}}r.apply(this,arguments)},f=new l(t));console.warn("warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper")}})); +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).createHelper=e()}(this,(function(){"use strict";function t(t,e){for(var n=0;n0?t.history.current.matched[0].instances.default:void 0},a=function(t){if(Array.isArray(t))for(var n=0;n=t;)this.pop()},e.clear=function(){this.historyStackMap=[]},t}(),p=function(){function r(t){var e=t.Vue,n=t.router,i=t.replaceStay;this.Vue=e,this.router=n,this.router._stack=0,this.mode=n.mode,this.historyShouldChange=!1,this.isReplace=!1,this.replacePrePath=void 0,this.preStateId=0,this.pre=null,this.replaceStay=i||[],this.hacked=!1,this.historyStack=new u,this.init()}var c,h,p,l=r.prototype;return l.init=function(){this.routerHooks(),this.hackRouter()},l.routerHooks=function(){var t=this,i=this.router;i.afterEach((function(r,a){t.historyShouldChange=!0,t.Vue.nextTick((function(){var r,a,c=t.currentVm,h=e(r=c)&&r.$vnode.data.keepAlive?r:n;null===t.pre?t.onInitial(h):t.isReplace?t.onReplace(h):t.isPush?t.onPush(h):t.onBack(h),t.pre=c,t.preStateId=t.stackPointer,(a=h)&&a.__placeholder||(!function(t,e){var n=s(t);n&&n._vnode&&(n._vnode.parent.key=e)}(i,o(t.stackPointer,i)),!t.hacked&&c&&t.hackKeepAliveRender(c.$vnode.parent.componentInstance),t.historyShouldChange=!1)}))}))},l.hackRouter=function(){var t=this,n=this.router,i=n.replace;n.replace=function(r,o,s){!function(r,o,s){t.isReplace=!0,t.replacePrePath=n.history.current.path,i.call(n,r,o,(function(n){t.isReplace=!1,t.replacePrePath=void 0,e(s)&&s(n)}))}(r,o,s)};var r=n.go;n.go=function(e){return i=e,t.isReplace=!1,r.call(n,i);var i};var o=n.push;n.push=function(e,i,r){return function(e,i,r){if(t.isReplace=!1,!i&&!r&&"undefined"!=typeof Promise)return o.call(n,e,i,r);o.call(n,e,i,r)}(e,i,r)}},l.hackKeepAliveRender=function(t){!function(t,n){if(e(t)&&e(t.cache)&&e(t.keys)){var i=t.keys,r=t.cache;if(1===i.length){var o=r[i[0]];delete r[i[0]],i.splice(0,1),i.push(n),r[n]=o}}}(t,o(this.stackPointer,this.router));var n=t.$options.render,i=this,r=this.router;t.$options.render=function(){var t=this.$slots.default,s=a(t);return i.historyShouldChange?s&&!e(s.key)&&(i.isReplace?s.key=o(i.stackPointer,r):i.isPush?s.key=o(i.stackPointer+1,r):s.key=o(i.stackPointer-1,r)):s.key=o(i.stackPointer,r),n.apply(this,arguments)},this.hacked=!0},l.onInitial=function(t){var n=i();e(n)?this.setStackPointer(n):this.setState(0),this.historyStack.push(t,this.stackPointer)},l.onPush=function(t){this.setState(this.increaseStackPointer()),this.historyStack.push(t,this.stackPointer)},l.onBack=function(t){this.historyStack.pop(),this.decreaseStackPointer(),this.historyStack.push(t,this.stackPointer)},l.onReplace=function(t){!(e(this.replacePrePath)&&this.replaceStay.includes(this.replacePrePath))&&this.pre.$keepAliveDestroy(),this.setState(this.stackPointer),this.historyStack.push(t,this.stackPointer),this.isReplace=!1,this.replacePrePath=void 0},l.setState=function(t){this.setStackPointer(t),function(t,n,i){var r=window.location,o=""+r.pathname+r.search+r.hash,s=e(history.state)?history.state:{};s.id=i;var a=window.location.href.startsWith("file://");history.replaceState(s,"",a?null:o)}(this.mode,this.router,t)},l.setStackPointer=function(t){this.router._stack=t},l.increaseStackPointer=function(){return this.router._stack+=1},l.decreaseStackPointer=function(){return this.router._stack-=1},c=r,(h=[{key:"currentVm",get:function(){return s(this.router)}},{key:"isPush",get:function(){if(!this.isReplace){var t=i();return!e(t)||this.preStateId<=t}return!1}},{key:"stackPointer",get:function(){return this.router._stack}}])&&t(c.prototype,h),p&&t(c,p),r}();var l=void 0;return function(t){var n,i;if(void 0!==t.Vue&&void 0!==t.router)return l||(h&&function(t){var e=t.replaceState;t.replaceState=function(n,i,r){var o=Object.assign({},t.state),s=Object.assign(o,n);e.call(t,s,i,r)};var n=t.pushState;t.pushState=function(e,i,r){var o=Object.assign({},t.state),s=Object.assign(o,e);n.call(t,s,i,r)}}(window.history),n=t.Vue,i=n.prototype.$destroy,n.prototype.$keepAliveDestroy=function(){if(this.$vnode&&this.$vnode.data.keepAlive&&this.$vnode.parent&&this.$vnode.parent.componentInstance&&this.$vnode.parent.componentInstance.cache&&this.$vnode.componentOptions){var t=e(this.$vnode.key)?this.$vnode.key:this.$vnode.componentOptions.Ctor.cid+(this.$vnode.componentOptions.tag?"::"+this.$vnode.componentOptions.tag:""),n=this.$vnode.parent.componentInstance.cache,r=this.$vnode.parent.componentInstance.keys;if(n[t]){if(r.length){var o=r.indexOf(t);o>-1&&r.splice(o,1)}delete n[t]}}i.apply(this,arguments)},l=new p(t));console.warn("warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper")}})); //# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map index 22c6b4a..b1b9340 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sources":["../src/utils.js","../src/historyStack.js","../src/helper.js","../src/index.js","../src/extension.js"],"sourcesContent":["export const isDef = function (v) {\n return v !== undefined && v !== null;\n};\nconst PLACEHOLDER_VM = {\n __placeholder: true,\n};\nexport const resolvePushedVm = (currentVm) => {\n if (!isDef(currentVm)) {\n return undefined;\n }\n return !currentVm.$vnode.data.keepAlive ? PLACEHOLDER_VM : currentVm;\n};\nexport const isPlaceHolderVm = (vm) => vm && !!vm.__placeholder;\n\nexport const getStateId = function () {\n const state = getCurrentState();\n return isDef(state) ? state.id : undefined;\n};\n\nexport const getQuery = function (params) {\n let query = \"\";\n query = Object.keys(params)\n .map(\n (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`\n )\n .join(\"&\");\n if (query.length > 0) {\n query = `?${query}`;\n }\n return query;\n};\nconst getCurrentState = function () {\n return history.state;\n};\n\nexport const genKey = function (num, router) {\n return `keep-alive-vnode-key${Number(num)}${router.history.current.path}`;\n};\nexport const getCurrentVM = function (router) {\n return router.history.current.matched.length > 0\n ? router.history.current.matched[0].instances.default\n : undefined;\n};\nexport const setCurrentVnodeKey = function (router, key) {\n const current = getCurrentVM(router);\n if (current && current._vnode) {\n current._vnode.parent.key = key;\n }\n};\nexport const replaceFirstKeyAndCache = function (vm, key) {\n if (!isDef(vm) || !isDef(vm.cache) || !isDef(vm.keys)) {\n return;\n }\n const keys = vm.keys;\n const cache = vm.cache;\n if (keys.length === 1) {\n const vnode = cache[keys[0]];\n delete cache[keys[0]];\n keys.splice(0, 1);\n keys.push(key);\n cache[key] = vnode;\n }\n};\nexport const getFirstComponentChild = function (children) {\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length; i++) {\n const c = children[i];\n if (isDef(c) && (isDef(c.componentOptions) || isAsyncPlaceholder(c))) {\n return c;\n }\n }\n }\n};\nconst isAsyncPlaceholder = function (node) {\n return node.isComment && node.asyncFactory;\n};\n\nexport const replaceState = function (mode, router, id) {\n let path =\n window.location.pathname + (mode === \"hash\" ? window.location.hash : \"\");\n let query = getQuery(router.history.current.query);\n if (mode !== \"hash\") { //fixed issue#18: in hash mode issue\n path = path + query;\n }\n let state = isDef(history.state) ? history.state : {};\n state[\"id\"] = id;\n // optimize file:// URL\n const isFilSys = window.location.href.startsWith(\"file://\");\n history.replaceState(state, \"\", isFilSys ? null : path);\n};\n\nexport const inBrowser = typeof window !== 'undefined';","export default class HistoryStack {\n constructor() {\n this.historyStackMap = [];\n }\n push(vm, index) {\n // const cur = stackPointer();\n const stack = this.historyStackMap[index];\n if (Array.isArray(stack)) {\n !stack.includes(vm) && stack.push(vm);\n this.historyStackMap[index] = stack.filter((item) => !item._isDestroyed);\n } else {\n const vms = [];\n vms.push(vm);\n this.historyStackMap[index] = vms;\n }\n }\n pop() {\n const last = this.historyStackMap.pop();\n Array.isArray(last) &&\n last.forEach(\n (vm) => vm && vm.$keepAliveDestroy && vm.$keepAliveDestroy()\n );\n }\n removeGreater(index) {\n while (this.historyStackMap.length >= index) {\n this.pop();\n }\n }\n clear() {\n this.historyStackMap = [];\n }\n}\n","import {\n getCurrentVM,\n isDef,\n getStateId,\n resolvePushedVm,\n genKey,\n replaceFirstKeyAndCache,\n getFirstComponentChild,\n isPlaceHolderVm,\n setCurrentVnodeKey,\n replaceState,\n} from \"./utils\";\nimport HistoryStack from \"./historyStack\";\n\nexport default class VueRouterKeepAliveHelper{\n constructor({ Vue, router, replaceStay }) {\n this.Vue = Vue;\n this.router = router;\n this.router._stack = 0;\n this.mode = router.mode; // hash or history\n this.historyShouldChange = false;\n this.isReplace = false;\n this.replacePrePath = undefined;\n this.preStateId = 0;\n this.pre = null;\n this.replaceStay = replaceStay || [];\n this.hacked = false;\n this.historyStack = new HistoryStack();\n this.init();\n }\n init() {\n this.routerHooks();\n this.hackRouter();\n }\n /**\n * use afterEach hook to set state.key and add the reference of vm to the historyStack\n */\n routerHooks() {\n const router = this.router;\n router.afterEach((to, from) => {\n this.historyShouldChange = true;\n // get the vm instance after render\n this.Vue.nextTick(() => {\n const current = this.currentVm;\n const pendingToPushVm = resolvePushedVm(current);\n if (this.pre === null) {\n this.onInitial(pendingToPushVm);\n } else if (this.isReplace) {\n this.onReplace(pendingToPushVm);\n } else if (this.isPush) {\n this.onPush(pendingToPushVm);\n } else {\n this.onBack(pendingToPushVm);\n }\n this.pre = current;\n this.preStateId = this.stackPointer;\n if (!isPlaceHolderVm(pendingToPushVm)) {\n setCurrentVnodeKey(router, genKey(this.stackPointer, router));\n if (!this.hacked && current) {\n this.hackKeepAliveRender(current.$vnode.parent.componentInstance);\n }\n this.historyShouldChange = false;\n }\n });\n });\n }\n /**\n * @description hack router go , replace and push functions to tell replace from back and push\n */\n hackRouter() {\n const router = this.router;\n const rtmp = router.replace;\n const rtmpf = (location, onComplete, onAbort) => {\n this.isReplace = true;\n this.replacePrePath = router.history.current.path;\n rtmp.call(router, location, onComplete, (e) => {\n this.isReplace = false;\n this.replacePrePath = undefined;\n isDef(onAbort) && onAbort(e);\n });\n };\n router.replace = function (ocation, onComplete, onAbort) {\n rtmpf(ocation, onComplete, onAbort);\n };\n\n const gstmp = router.go;\n const gstmpf = (number) => {\n this.isReplace = false;\n return gstmp.call(router, number);\n };\n router.go = function (num) {\n return gstmpf(num);\n };\n const pstmp = router.push;\n const pstmpf = (location, onComplete, onAbort) => {\n this.isReplace = false;\n if (!onComplete && !onAbort && typeof Promise !== \"undefined\") {\n return pstmp.call(router, location, onComplete, onAbort);\n } else {\n pstmp.call(router, location, onComplete, onAbort);\n }\n };\n router.push = function (location, onComplete, onAbort) {\n return pstmpf(location, onComplete, onAbort);\n };\n }\n /**\n * @description hack the render function of keep-alive component, modify the key of vnode for cache\n * @param {*} vm keep-alive component instance\n */\n hackKeepAliveRender(vm) {\n // modify the first keep alive key and cache\n replaceFirstKeyAndCache(vm, genKey(this.stackPointer, this.router));\n\n const tmp = vm.$options.render;\n const self = this;\n const router = this.router;\n vm.$options.render = function () {\n const slot = this.$slots.default;\n const vnode = getFirstComponentChild(slot); // vnode is a keep-alive-component-vnode\n if (self.historyShouldChange) {\n if (vnode && !isDef(vnode.key)) {\n if (self.isReplace) {\n vnode.key = genKey(self.stackPointer, router);\n } else if (self.isPush) {\n vnode.key = genKey(self.stackPointer + 1, router);\n } else {\n vnode.key = genKey(self.stackPointer - 1, router);\n }\n }\n } else {\n // when historyShouldChange is false should rerender only, should not create new vm ,use the same vnode.key issue#7\n vnode.key = genKey(self.stackPointer, router);\n }\n return tmp.apply(this, arguments);\n };\n this.hacked = true;\n }\n /********** callback functions ************/\n onInitial(vm) {\n const currentStateId = getStateId();\n if (isDef(currentStateId)) {\n this.setStackPointer(currentStateId);\n } else {\n this.setState(0);\n }\n this.historyStack.push(vm, this.stackPointer);\n }\n onPush(vm) {\n this.setState(this.increaseStackPointer());\n this.historyStack.push(vm, this.stackPointer);\n }\n onBack(vm) {\n this.historyStack.pop();\n this.decreaseStackPointer();\n this.historyStack.push(vm, this.stackPointer);\n }\n onReplace(vm) {\n const shouldDestroy = !(\n isDef(this.replacePrePath) &&\n this.replaceStay.includes(this.replacePrePath)\n );\n if (shouldDestroy) {\n this.pre.$keepAliveDestroy();\n }\n this.setState(this.stackPointer);\n this.historyStack.push(vm, this.stackPointer);\n this.isReplace = false;\n this.replacePrePath = undefined;\n }\n get currentVm() {\n return getCurrentVM(this.router);\n }\n get isPush() {\n if (!this.isReplace) {\n const stateId = getStateId();\n return !isDef(stateId) || this.preStateId <= stateId;\n }\n return false;\n }\n get stackPointer() {\n return this.router._stack;\n }\n setState(id) {\n this.setStackPointer(id);\n replaceState(this.mode, this.router, id);\n }\n setStackPointer(val) {\n this.router._stack = val;\n }\n increaseStackPointer() {\n return (this.router._stack += 1);\n }\n decreaseStackPointer() {\n return (this.router._stack -= 1);\n }\n}\n","import Helper from \"./helper\";\nimport { extendVue, extendHistory } from \"./extension\";\nimport { inBrowser } from \"./utils\";\n\nlet singleton = undefined;\n// TODO: 1. abstract mode support\nexport default function createHelper(config) {\n if (config.Vue === undefined || config.router === undefined) {\n console.warn(\n \"warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper\"\n );\n return;\n }\n if(singleton) {\n return singleton;\n }\n if (inBrowser) {\n extendHistory(window.history);\n }\n extendVue(config.Vue);\n return (singleton = new Helper(config));\n}","import { isDef } from \"./utils\";\n\nexport function extendHistory(history) {\n const rstmp = history.replaceState;\n history.replaceState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n rstmp.call(history, s, op, path);\n }\n const historyPushState = history.pushState;\n history.pushState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n historyPushState.call(history, s, op, path);\n }\n}\n\nexport function extendVue(Vue) {\n const dtmp = Vue.prototype.$destroy;\n /**\n * @description remove the cache in component before invoke $destroy\n */\n Vue.prototype.$keepAliveDestroy = function () {\n if (this.$vnode && this.$vnode.data.keepAlive) {\n if (\n this.$vnode.parent &&\n this.$vnode.parent.componentInstance &&\n this.$vnode.parent.componentInstance.cache\n ) {\n if (this.$vnode.componentOptions) {\n const key = !isDef(this.$vnode.key)\n ? this.$vnode.componentOptions.Ctor.cid +\n (this.$vnode.componentOptions.tag\n ? `::${this.$vnode.componentOptions.tag}`\n : \"\")\n : this.$vnode.key;\n const cache = this.$vnode.parent.componentInstance.cache;\n const keys = this.$vnode.parent.componentInstance.keys;\n if (cache[key]) {\n if (keys.length) {\n const index = keys.indexOf(key);\n if (index > -1) {\n keys.splice(index, 1);\n }\n }\n delete cache[key];\n }\n }\n }\n }\n dtmp.apply(this, arguments);\n };\n}\n"],"names":["isDef","v","PLACEHOLDER_VM","__placeholder","getStateId","state","getCurrentState","id","undefined","history","genKey","num","router","Number","current","path","getCurrentVM","matched","length","instances","default","getFirstComponentChild","children","Array","isArray","i","c","componentOptions","isAsyncPlaceholder","node","isComment","asyncFactory","replaceState","mode","window","location","pathname","hash","query","params","Object","keys","map","key","encodeURIComponent","join","getQuery","isFilSys","href","startsWith","inBrowser","HistoryStack","historyStackMap","push","vm","index","stack","this","includes","filter","item","_isDestroyed","vms","pop","last","forEach","$keepAliveDestroy","removeGreater","clear","VueRouterKeepAliveHelper","Vue","replaceStay","_stack","historyShouldChange","isReplace","replacePrePath","preStateId","pre","hacked","historyStack","init","routerHooks","hackRouter","afterEach","to","from","_this","nextTick","currentVm","pendingToPushVm","$vnode","data","keepAlive","resolvePushedVm","onInitial","onReplace","isPush","onPush","onBack","stackPointer","_vnode","parent","setCurrentVnodeKey","hackKeepAliveRender","componentInstance","rtmp","replace","ocation","onComplete","onAbort","_this2","call","e","rtmpf","gstmp","go","number","pstmp","Promise","pstmpf","cache","vnode","splice","replaceFirstKeyAndCache","tmp","$options","render","self","slot","$slots","apply","arguments","currentStateId","setStackPointer","setState","increaseStackPointer","decreaseStackPointer","val","stateId","singleton","config","dtmp","rstmp","op","old","assign","s","historyPushState","pushState","extendHistory","prototype","$destroy","Ctor","cid","tag","indexOf","Helper","console","warn"],"mappings":"mZAAO,IAAMA,EAAQ,SAAUC,UACtBA,MAAAA,GAEHC,EAAiB,CACrBC,eAAe,GAUJC,EAAa,eAClBC,EAAQC,WACPN,EAAMK,GAASA,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SACrBjG,KAAKiC,OAAO/D,iBAAiB+H,IAClC,IAEFlC,EAAQ/D,KAAKiC,OAAOW,OAAOG,kBAAkBgB,MAC7C/E,EAAOgB,KAAKiC,OAAOW,OAAOG,kBAAkB/D,QAC9C+E,EAAM7E,GAAM,IACVF,EAAKvB,OAAQ,KACTqC,EAAQd,EAAKkH,QAAQhH,GACvBY,GAAS,GACXd,EAAKiF,OAAOnE,EAAO,UAGhBiE,EAAM7E,IAKrBkG,EAAKX,MAAMzE,KAAM0E,YD9BXQ,EAAY,IAAIiB,EAAOhB,IAZ7BiB,QAAQC,KACN"} \ No newline at end of file +{"version":3,"file":"index.js","sources":["../src/utils.js","../src/historyStack.js","../src/helper.js","../src/index.js","../src/extension.js"],"sourcesContent":["export const isDef = function (v) {\n return v !== undefined && v !== null;\n};\nconst PLACEHOLDER_VM = {\n __placeholder: true,\n};\nexport const resolvePushedVm = (currentVm) => {\n return (isDef(currentVm) && currentVm.$vnode.data.keepAlive) ? currentVm : PLACEHOLDER_VM;\n};\nexport const isPlaceHolderVm = (vm) => vm && !!vm.__placeholder;\n\nexport const getStateId = function () {\n const state = getCurrentState();\n return isDef(state) ? state.id : undefined;\n};\n\nexport const getQuery = function (params) {\n let query = \"\";\n query = Object.keys(params)\n .map(\n (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`\n )\n .join(\"&\");\n if (query.length > 0) {\n query = `?${query}`;\n }\n return query;\n};\nconst getCurrentState = function () {\n return history.state;\n};\n\nexport const genKey = function (num, router) {\n return `keep-alive-vnode-key${Number(num)}${router.history.current.path}`;\n};\nexport const getCurrentVM = function (router) {\n return router.history.current.matched.length > 0\n ? router.history.current.matched[0].instances.default\n : undefined;\n};\nexport const setCurrentVnodeKey = function (router, key) {\n const current = getCurrentVM(router);\n if (current && current._vnode) {\n current._vnode.parent.key = key;\n }\n};\nexport const replaceFirstKeyAndCache = function (vm, key) {\n if (!isDef(vm) || !isDef(vm.cache) || !isDef(vm.keys)) {\n return;\n }\n const keys = vm.keys;\n const cache = vm.cache;\n if (keys.length === 1) {\n const vnode = cache[keys[0]];\n delete cache[keys[0]];\n keys.splice(0, 1);\n keys.push(key);\n cache[key] = vnode;\n }\n};\nexport const getFirstComponentChild = function (children) {\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length; i++) {\n const c = children[i];\n if (isDef(c) && (isDef(c.componentOptions) || isAsyncPlaceholder(c))) {\n return c;\n }\n }\n }\n};\nconst isAsyncPlaceholder = function (node) {\n return node.isComment && node.asyncFactory;\n};\n\nexport const replaceState = function (mode, router, id) {\n const { pathname, search, hash } = window.location;\n let path = `${pathname}${search}${hash}`;\n let state = isDef(history.state) ? history.state : {};\n state[\"id\"] = id;\n // optimize file:// URL\n const isFilSys = window.location.href.startsWith(\"file://\");\n history.replaceState(state, \"\", isFilSys ? null : path);\n};\n\nexport const inBrowser = typeof window !== \"undefined\";\n","export default class HistoryStack {\n constructor() {\n this.historyStackMap = [];\n }\n push(vm, index) {\n // const cur = stackPointer();\n const stack = this.historyStackMap[index];\n if (Array.isArray(stack)) {\n !stack.includes(vm) && stack.push(vm);\n this.historyStackMap[index] = stack.filter((item) => !item._isDestroyed);\n } else {\n const vms = [];\n vms.push(vm);\n this.historyStackMap[index] = vms;\n }\n }\n pop() {\n const last = this.historyStackMap.pop();\n Array.isArray(last) &&\n last.forEach(\n (vm) => vm && vm.$keepAliveDestroy && vm.$keepAliveDestroy()\n );\n }\n removeGreater(index) {\n while (this.historyStackMap.length >= index) {\n this.pop();\n }\n }\n clear() {\n this.historyStackMap = [];\n }\n}\n","import {\n getCurrentVM,\n isDef,\n getStateId,\n resolvePushedVm,\n genKey,\n replaceFirstKeyAndCache,\n getFirstComponentChild,\n isPlaceHolderVm,\n setCurrentVnodeKey,\n replaceState,\n} from \"./utils\";\nimport HistoryStack from \"./historyStack\";\n\nexport default class VueRouterKeepAliveHelper{\n constructor({ Vue, router, replaceStay }) {\n this.Vue = Vue;\n this.router = router;\n this.router._stack = 0;\n this.mode = router.mode; // hash or history\n this.historyShouldChange = false;\n this.isReplace = false;\n this.replacePrePath = undefined;\n this.preStateId = 0;\n this.pre = null;\n this.replaceStay = replaceStay || [];\n this.hacked = false;\n this.historyStack = new HistoryStack();\n this.init();\n }\n init() {\n this.routerHooks();\n this.hackRouter();\n }\n /**\n * use afterEach hook to set state.key and add the reference of vm to the historyStack\n */\n routerHooks() {\n const router = this.router;\n router.afterEach((to, from) => {\n this.historyShouldChange = true;\n // get the vm instance after render\n this.Vue.nextTick(() => {\n const current = this.currentVm;\n const pendingToPushVm = resolvePushedVm(current);\n if (this.pre === null) {\n this.onInitial(pendingToPushVm);\n } else if (this.isReplace) {\n this.onReplace(pendingToPushVm);\n } else if (this.isPush) {\n this.onPush(pendingToPushVm);\n } else {\n this.onBack(pendingToPushVm);\n }\n this.pre = current;\n this.preStateId = this.stackPointer;\n if (!isPlaceHolderVm(pendingToPushVm)) {\n setCurrentVnodeKey(router, genKey(this.stackPointer, router));\n if (!this.hacked && current) {\n this.hackKeepAliveRender(current.$vnode.parent.componentInstance);\n }\n this.historyShouldChange = false;\n }\n });\n });\n }\n /**\n * @description hack router go , replace and push functions to tell replace from back and push\n */\n hackRouter() {\n const router = this.router;\n const rtmp = router.replace;\n const rtmpf = (location, onComplete, onAbort) => {\n this.isReplace = true;\n this.replacePrePath = router.history.current.path;\n rtmp.call(router, location, onComplete, (e) => {\n this.isReplace = false;\n this.replacePrePath = undefined;\n isDef(onAbort) && onAbort(e);\n });\n };\n router.replace = function (ocation, onComplete, onAbort) {\n rtmpf(ocation, onComplete, onAbort);\n };\n\n const gstmp = router.go;\n const gstmpf = (number) => {\n this.isReplace = false;\n return gstmp.call(router, number);\n };\n router.go = function (num) {\n return gstmpf(num);\n };\n const pstmp = router.push;\n const pstmpf = (location, onComplete, onAbort) => {\n this.isReplace = false;\n if (!onComplete && !onAbort && typeof Promise !== \"undefined\") {\n return pstmp.call(router, location, onComplete, onAbort);\n } else {\n pstmp.call(router, location, onComplete, onAbort);\n }\n };\n router.push = function (location, onComplete, onAbort) {\n return pstmpf(location, onComplete, onAbort);\n };\n }\n /**\n * @description hack the render function of keep-alive component, modify the key of vnode for cache\n * @param {*} vm keep-alive component instance\n */\n hackKeepAliveRender(vm) {\n // modify the first keep alive key and cache\n replaceFirstKeyAndCache(vm, genKey(this.stackPointer, this.router));\n\n const tmp = vm.$options.render;\n const self = this;\n const router = this.router;\n vm.$options.render = function () {\n const slot = this.$slots.default;\n const vnode = getFirstComponentChild(slot); // vnode is a keep-alive-component-vnode\n if (self.historyShouldChange) {\n if (vnode && !isDef(vnode.key)) {\n if (self.isReplace) {\n vnode.key = genKey(self.stackPointer, router);\n } else if (self.isPush) {\n vnode.key = genKey(self.stackPointer + 1, router);\n } else {\n vnode.key = genKey(self.stackPointer - 1, router);\n }\n }\n } else {\n // when historyShouldChange is false should rerender only, should not create new vm ,use the same vnode.key issue#7\n vnode.key = genKey(self.stackPointer, router);\n }\n return tmp.apply(this, arguments);\n };\n this.hacked = true;\n }\n /********** callback functions ************/\n onInitial(vm) {\n const currentStateId = getStateId();\n if (isDef(currentStateId)) {\n this.setStackPointer(currentStateId);\n } else {\n this.setState(0);\n }\n this.historyStack.push(vm, this.stackPointer);\n }\n onPush(vm) {\n this.setState(this.increaseStackPointer());\n this.historyStack.push(vm, this.stackPointer);\n }\n onBack(vm) {\n this.historyStack.pop();\n this.decreaseStackPointer();\n this.historyStack.push(vm, this.stackPointer);\n }\n onReplace(vm) {\n const shouldDestroy = !(\n isDef(this.replacePrePath) &&\n this.replaceStay.includes(this.replacePrePath)\n );\n if (shouldDestroy) {\n this.pre.$keepAliveDestroy();\n }\n this.setState(this.stackPointer);\n this.historyStack.push(vm, this.stackPointer);\n this.isReplace = false;\n this.replacePrePath = undefined;\n }\n get currentVm() {\n return getCurrentVM(this.router);\n }\n get isPush() {\n if (!this.isReplace) {\n const stateId = getStateId();\n return !isDef(stateId) || this.preStateId <= stateId;\n }\n return false;\n }\n get stackPointer() {\n return this.router._stack;\n }\n setState(id) {\n this.setStackPointer(id);\n replaceState(this.mode, this.router, id);\n }\n setStackPointer(val) {\n this.router._stack = val;\n }\n increaseStackPointer() {\n return (this.router._stack += 1);\n }\n decreaseStackPointer() {\n return (this.router._stack -= 1);\n }\n}\n","import Helper from \"./helper\";\nimport { extendVue, extendHistory } from \"./extension\";\nimport { inBrowser } from \"./utils\";\n\nlet singleton = undefined;\n// TODO: 1. abstract mode support\nexport default function createHelper(config) {\n if (config.Vue === undefined || config.router === undefined) {\n console.warn(\n \"warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper\"\n );\n return;\n }\n if(singleton) {\n return singleton;\n }\n if (inBrowser) {\n extendHistory(window.history);\n }\n extendVue(config.Vue);\n return (singleton = new Helper(config));\n}","import { isDef } from \"./utils\";\n\nexport function extendHistory(history) {\n const rstmp = history.replaceState;\n history.replaceState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n rstmp.call(history, s, op, path);\n }\n const historyPushState = history.pushState;\n history.pushState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n historyPushState.call(history, s, op, path);\n }\n}\n\nexport function extendVue(Vue) {\n const dtmp = Vue.prototype.$destroy;\n /**\n * @description remove the cache in component before invoke $destroy\n */\n Vue.prototype.$keepAliveDestroy = function () {\n if (this.$vnode && this.$vnode.data.keepAlive) {\n if (\n this.$vnode.parent &&\n this.$vnode.parent.componentInstance &&\n this.$vnode.parent.componentInstance.cache\n ) {\n if (this.$vnode.componentOptions) {\n const key = !isDef(this.$vnode.key)\n ? this.$vnode.componentOptions.Ctor.cid +\n (this.$vnode.componentOptions.tag\n ? `::${this.$vnode.componentOptions.tag}`\n : \"\")\n : this.$vnode.key;\n const cache = this.$vnode.parent.componentInstance.cache;\n const keys = this.$vnode.parent.componentInstance.keys;\n if (cache[key]) {\n if (keys.length) {\n const index = keys.indexOf(key);\n if (index > -1) {\n keys.splice(index, 1);\n }\n }\n delete cache[key];\n }\n }\n }\n }\n dtmp.apply(this, arguments);\n };\n}\n"],"names":["isDef","v","PLACEHOLDER_VM","__placeholder","getStateId","state","getCurrentState","id","undefined","history","genKey","num","router","Number","current","path","getCurrentVM","matched","length","instances","default","getFirstComponentChild","children","Array","isArray","i","c","componentOptions","isAsyncPlaceholder","node","isComment","asyncFactory","inBrowser","window","HistoryStack","historyStackMap","push","vm","index","stack","this","includes","filter","item","_isDestroyed","vms","pop","last","forEach","$keepAliveDestroy","removeGreater","clear","VueRouterKeepAliveHelper","Vue","replaceStay","_stack","mode","historyShouldChange","isReplace","replacePrePath","preStateId","pre","hacked","historyStack","init","routerHooks","hackRouter","afterEach","to","from","_this","nextTick","currentVm","pendingToPushVm","$vnode","data","keepAlive","onInitial","onReplace","isPush","onPush","onBack","stackPointer","key","_vnode","parent","setCurrentVnodeKey","hackKeepAliveRender","componentInstance","rtmp","replace","ocation","onComplete","onAbort","location","_this2","call","e","rtmpf","gstmp","go","number","pstmp","Promise","pstmpf","cache","keys","vnode","splice","replaceFirstKeyAndCache","tmp","$options","render","self","slot","$slots","apply","arguments","currentStateId","setStackPointer","setState","increaseStackPointer","decreaseStackPointer","pathname","search","hash","isFilSys","href","startsWith","replaceState","val","stateId","singleton","config","dtmp","rstmp","op","old","Object","assign","s","historyPushState","pushState","extendHistory","prototype","$destroy","Ctor","cid","tag","indexOf","Helper","console","warn"],"mappings":"mZAAO,IAAMA,EAAQ,SAAUC,UACtBA,MAAAA,GAEHC,EAAiB,CACrBC,eAAe,GAOJC,EAAa,eAClBC,EAAQC,WACPN,EAAMK,GAASA,EAAME,QAAKC,GAe7BF,EAAkB,kBACfG,QAAQJ,OAGJK,EAAS,SAAUC,EAAKC,gCACLC,OAAOF,GAAOC,EAAOH,QAAQK,QAAQC,MAExDC,EAAe,SAAUJ,UAC7BA,EAAOH,QAAQK,QAAQG,QAAQC,OAAS,EAC3CN,EAAOH,QAAQK,QAAQG,QAAQ,GAAGE,UAAUC,aAC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J,EAAIzH,QAEvB8H,EAAmBpI,EAAQqI,UACjCrI,EAAQqI,UAAY,SAAUzI,EAAOmI,EAAIzH,OACjC0H,EAAMC,OAAOC,OAAO,GAAIlI,EAAQJ,OAChCuI,EAAIF,OAAOC,OAAOF,EAAKpI,GAC7BwI,EAAiB7C,KAAKvF,EAASmI,EAAGJ,EAAIzH,IDItCgI,CAAc9G,OAAOxB,SCAC4C,EDEdgF,EAAOhF,ICDXiF,EAAOjF,EAAI2F,UAAUC,SAI3B5F,EAAI2F,UAAU/F,kBAAoB,cAC5BT,KAAKkC,QAAUlC,KAAKkC,OAAOC,KAAKC,WAEhCpC,KAAKkC,OAAOW,QACZ7C,KAAKkC,OAAOW,OAAOG,mBACnBhD,KAAKkC,OAAOW,OAAOG,kBAAkBiB,OAEjCjE,KAAKkC,OAAO/C,iBAAkB,KAC1BwD,EAAOnF,EAAMwC,KAAKkC,OAAOS,KAK3B3C,KAAKkC,OAAOS,IAJZ3C,KAAKkC,OAAO/C,iBAAiBuH,KAAKC,KACjC3G,KAAKkC,OAAO/C,iBAAiByH,SACrB5G,KAAKkC,OAAO/C,iBAAiByH,IAClC,IAEF3C,EAAQjE,KAAKkC,OAAOW,OAAOG,kBAAkBiB,MAC7CC,EAAOlE,KAAKkC,OAAOW,OAAOG,kBAAkBkB,QAC9CD,EAAMtB,GAAM,IACVuB,EAAKxF,OAAQ,KACToB,EAAQoE,EAAK2C,QAAQlE,GACvB7C,GAAS,GACXoE,EAAKE,OAAOtE,EAAO,UAGhBmE,EAAMtB,IAKrBmD,EAAKlB,MAAM5E,KAAM6E,YD9BXe,EAAY,IAAIkB,EAAOjB,IAZ7BkB,QAAQC,KACN"} \ No newline at end of file From 6af92a0492b0243553026259ee7e8e4a7a92e478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=91=AB=E5=87=AF?= Date: Tue, 28 Sep 2021 14:30:24 +0800 Subject: [PATCH 3/6] fix: #23 this.pre is undefined cause error --- src/helper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helper.js b/src/helper.js index d200c91..f145731 100644 --- a/src/helper.js +++ b/src/helper.js @@ -161,7 +161,7 @@ export default class VueRouterKeepAliveHelper{ this.replaceStay.includes(this.replacePrePath) ); if (shouldDestroy) { - this.pre.$keepAliveDestroy(); + this.pre?.$keepAliveDestroy(); } this.setState(this.stackPointer); this.historyStack.push(vm, this.stackPointer); From 8dc653a50a6d0dd965aba0fc4120764a2c474c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=91=AB=E5=87=AF?= Date: Tue, 28 Sep 2021 14:34:24 +0800 Subject: [PATCH 4/6] chore:build for test fix issue 23 --- dist/helper.esm.js | 2 +- dist/helper.esm.js.map | 2 +- dist/index.js | 2 +- dist/index.js.map | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/helper.esm.js b/dist/helper.esm.js index dcd21cd..1f32776 100644 --- a/dist/helper.esm.js +++ b/dist/helper.esm.js @@ -1,2 +1,2 @@ -function t(t,e){for(var n=0;n0?t.history.current.matched[0].instances.default:void 0},s=function(t){if(Array.isArray(t))for(var n=0;n=t;)this.pop()},e.clear=function(){this.historyStackMap=[]},t}(),p=function(){function i(t){var e=t.Vue,n=t.router,r=t.replaceStay;this.Vue=e,this.router=n,this.router._stack=0,this.mode=n.mode,this.historyShouldChange=!1,this.isReplace=!1,this.replacePrePath=void 0,this.preStateId=0,this.pre=null,this.replaceStay=r||[],this.hacked=!1,this.historyStack=new u,this.init()}var c,h,p,l=i.prototype;return l.init=function(){this.routerHooks(),this.hackRouter()},l.routerHooks=function(){var t=this,r=this.router;r.afterEach((function(i,s){t.historyShouldChange=!0,t.Vue.nextTick((function(){var i,s,c=t.currentVm,h=e(i=c)&&i.$vnode.data.keepAlive?i:n;null===t.pre?t.onInitial(h):t.isReplace?t.onReplace(h):t.isPush?t.onPush(h):t.onBack(h),t.pre=c,t.preStateId=t.stackPointer,(s=h)&&s.__placeholder||(!function(t,e){var n=a(t);n&&n._vnode&&(n._vnode.parent.key=e)}(r,o(t.stackPointer,r)),!t.hacked&&c&&t.hackKeepAliveRender(c.$vnode.parent.componentInstance),t.historyShouldChange=!1)}))}))},l.hackRouter=function(){var t=this,n=this.router,r=n.replace;n.replace=function(i,o,a){!function(i,o,a){t.isReplace=!0,t.replacePrePath=n.history.current.path,r.call(n,i,o,(function(n){t.isReplace=!1,t.replacePrePath=void 0,e(a)&&a(n)}))}(i,o,a)};var i=n.go;n.go=function(e){return r=e,t.isReplace=!1,i.call(n,r);var r};var o=n.push;n.push=function(e,r,i){return function(e,r,i){if(t.isReplace=!1,!r&&!i&&"undefined"!=typeof Promise)return o.call(n,e,r,i);o.call(n,e,r,i)}(e,r,i)}},l.hackKeepAliveRender=function(t){!function(t,n){if(e(t)&&e(t.cache)&&e(t.keys)){var r=t.keys,i=t.cache;if(1===r.length){var o=i[r[0]];delete i[r[0]],r.splice(0,1),r.push(n),i[n]=o}}}(t,o(this.stackPointer,this.router));var n=t.$options.render,r=this,i=this.router;t.$options.render=function(){var t=this.$slots.default,a=s(t);return r.historyShouldChange?a&&!e(a.key)&&(r.isReplace?a.key=o(r.stackPointer,i):r.isPush?a.key=o(r.stackPointer+1,i):a.key=o(r.stackPointer-1,i)):a.key=o(r.stackPointer,i),n.apply(this,arguments)},this.hacked=!0},l.onInitial=function(t){var n=r();e(n)?this.setStackPointer(n):this.setState(0),this.historyStack.push(t,this.stackPointer)},l.onPush=function(t){this.setState(this.increaseStackPointer()),this.historyStack.push(t,this.stackPointer)},l.onBack=function(t){this.historyStack.pop(),this.decreaseStackPointer(),this.historyStack.push(t,this.stackPointer)},l.onReplace=function(t){!(e(this.replacePrePath)&&this.replaceStay.includes(this.replacePrePath))&&this.pre.$keepAliveDestroy(),this.setState(this.stackPointer),this.historyStack.push(t,this.stackPointer),this.isReplace=!1,this.replacePrePath=void 0},l.setState=function(t){this.setStackPointer(t),function(t,n,r){var i=window.location,o=""+i.pathname+i.search+i.hash,a=e(history.state)?history.state:{};a.id=r;var s=window.location.href.startsWith("file://");history.replaceState(a,"",s?null:o)}(this.mode,this.router,t)},l.setStackPointer=function(t){this.router._stack=t},l.increaseStackPointer=function(){return this.router._stack+=1},l.decreaseStackPointer=function(){return this.router._stack-=1},c=i,(h=[{key:"currentVm",get:function(){return a(this.router)}},{key:"isPush",get:function(){if(!this.isReplace){var t=r();return!e(t)||this.preStateId<=t}return!1}},{key:"stackPointer",get:function(){return this.router._stack}}])&&t(c.prototype,h),p&&t(c,p),i}();var l=void 0;function f(t){var n,r;if(void 0!==t.Vue&&void 0!==t.router)return l||(h&&function(t){var e=t.replaceState;t.replaceState=function(n,r,i){var o=Object.assign({},t.state),a=Object.assign(o,n);e.call(t,a,r,i)};var n=t.pushState;t.pushState=function(e,r,i){var o=Object.assign({},t.state),a=Object.assign(o,e);n.call(t,a,r,i)}}(window.history),n=t.Vue,r=n.prototype.$destroy,n.prototype.$keepAliveDestroy=function(){if(this.$vnode&&this.$vnode.data.keepAlive&&this.$vnode.parent&&this.$vnode.parent.componentInstance&&this.$vnode.parent.componentInstance.cache&&this.$vnode.componentOptions){var t=e(this.$vnode.key)?this.$vnode.key:this.$vnode.componentOptions.Ctor.cid+(this.$vnode.componentOptions.tag?"::"+this.$vnode.componentOptions.tag:""),n=this.$vnode.parent.componentInstance.cache,i=this.$vnode.parent.componentInstance.keys;if(n[t]){if(i.length){var o=i.indexOf(t);o>-1&&i.splice(o,1)}delete n[t]}}r.apply(this,arguments)},l=new p(t));console.warn("warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper")}export default f; +function t(t,e){for(var n=0;n0?t.history.current.matched[0].instances.default:void 0},s=function(t){if(Array.isArray(t))for(var n=0;n=t;)this.pop()},e.clear=function(){this.historyStackMap=[]},t}(),p=function(){function i(t){var e=t.Vue,n=t.router,r=t.replaceStay;this.Vue=e,this.router=n,this.router._stack=0,this.mode=n.mode,this.historyShouldChange=!1,this.isReplace=!1,this.replacePrePath=void 0,this.preStateId=0,this.pre=null,this.replaceStay=r||[],this.hacked=!1,this.historyStack=new u,this.init()}var c,h,p,l=i.prototype;return l.init=function(){this.routerHooks(),this.hackRouter()},l.routerHooks=function(){var t=this,r=this.router;r.afterEach((function(i,s){t.historyShouldChange=!0,t.Vue.nextTick((function(){var i,s,c=t.currentVm,h=e(i=c)&&i.$vnode.data.keepAlive?i:n;null===t.pre?t.onInitial(h):t.isReplace?t.onReplace(h):t.isPush?t.onPush(h):t.onBack(h),t.pre=c,t.preStateId=t.stackPointer,(s=h)&&s.__placeholder||(!function(t,e){var n=a(t);n&&n._vnode&&(n._vnode.parent.key=e)}(r,o(t.stackPointer,r)),!t.hacked&&c&&t.hackKeepAliveRender(c.$vnode.parent.componentInstance),t.historyShouldChange=!1)}))}))},l.hackRouter=function(){var t=this,n=this.router,r=n.replace;n.replace=function(i,o,a){!function(i,o,a){t.isReplace=!0,t.replacePrePath=n.history.current.path,r.call(n,i,o,(function(n){t.isReplace=!1,t.replacePrePath=void 0,e(a)&&a(n)}))}(i,o,a)};var i=n.go;n.go=function(e){return r=e,t.isReplace=!1,i.call(n,r);var r};var o=n.push;n.push=function(e,r,i){return function(e,r,i){if(t.isReplace=!1,!r&&!i&&"undefined"!=typeof Promise)return o.call(n,e,r,i);o.call(n,e,r,i)}(e,r,i)}},l.hackKeepAliveRender=function(t){!function(t,n){if(e(t)&&e(t.cache)&&e(t.keys)){var r=t.keys,i=t.cache;if(1===r.length){var o=i[r[0]];delete i[r[0]],r.splice(0,1),r.push(n),i[n]=o}}}(t,o(this.stackPointer,this.router));var n=t.$options.render,r=this,i=this.router;t.$options.render=function(){var t=this.$slots.default,a=s(t);return r.historyShouldChange?a&&!e(a.key)&&(r.isReplace?a.key=o(r.stackPointer,i):r.isPush?a.key=o(r.stackPointer+1,i):a.key=o(r.stackPointer-1,i)):a.key=o(r.stackPointer,i),n.apply(this,arguments)},this.hacked=!0},l.onInitial=function(t){var n=r();e(n)?this.setStackPointer(n):this.setState(0),this.historyStack.push(t,this.stackPointer)},l.onPush=function(t){this.setState(this.increaseStackPointer()),this.historyStack.push(t,this.stackPointer)},l.onBack=function(t){this.historyStack.pop(),this.decreaseStackPointer(),this.historyStack.push(t,this.stackPointer)},l.onReplace=function(t){var n;e(this.replacePrePath)&&this.replaceStay.includes(this.replacePrePath)||(null==(n=this.pre)||n.$keepAliveDestroy());this.setState(this.stackPointer),this.historyStack.push(t,this.stackPointer),this.isReplace=!1,this.replacePrePath=void 0},l.setState=function(t){this.setStackPointer(t),function(t,n,r){var i=window.location,o=""+i.pathname+i.search+i.hash,a=e(history.state)?history.state:{};a.id=r;var s=window.location.href.startsWith("file://");history.replaceState(a,"",s?null:o)}(this.mode,this.router,t)},l.setStackPointer=function(t){this.router._stack=t},l.increaseStackPointer=function(){return this.router._stack+=1},l.decreaseStackPointer=function(){return this.router._stack-=1},c=i,(h=[{key:"currentVm",get:function(){return a(this.router)}},{key:"isPush",get:function(){if(!this.isReplace){var t=r();return!e(t)||this.preStateId<=t}return!1}},{key:"stackPointer",get:function(){return this.router._stack}}])&&t(c.prototype,h),p&&t(c,p),i}();var l=void 0;function f(t){var n,r;if(void 0!==t.Vue&&void 0!==t.router)return l||(h&&function(t){var e=t.replaceState;t.replaceState=function(n,r,i){var o=Object.assign({},t.state),a=Object.assign(o,n);e.call(t,a,r,i)};var n=t.pushState;t.pushState=function(e,r,i){var o=Object.assign({},t.state),a=Object.assign(o,e);n.call(t,a,r,i)}}(window.history),n=t.Vue,r=n.prototype.$destroy,n.prototype.$keepAliveDestroy=function(){if(this.$vnode&&this.$vnode.data.keepAlive&&this.$vnode.parent&&this.$vnode.parent.componentInstance&&this.$vnode.parent.componentInstance.cache&&this.$vnode.componentOptions){var t=e(this.$vnode.key)?this.$vnode.key:this.$vnode.componentOptions.Ctor.cid+(this.$vnode.componentOptions.tag?"::"+this.$vnode.componentOptions.tag:""),n=this.$vnode.parent.componentInstance.cache,i=this.$vnode.parent.componentInstance.keys;if(n[t]){if(i.length){var o=i.indexOf(t);o>-1&&i.splice(o,1)}delete n[t]}}r.apply(this,arguments)},l=new p(t));console.warn("warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper")}export default f; //# sourceMappingURL=helper.esm.js.map diff --git a/dist/helper.esm.js.map b/dist/helper.esm.js.map index 9362073..6a03c08 100644 --- a/dist/helper.esm.js.map +++ b/dist/helper.esm.js.map @@ -1 +1 @@ -{"version":3,"file":"helper.esm.js","sources":["../src/utils.js","../src/historyStack.js","../src/helper.js","../src/index.js","../src/extension.js"],"sourcesContent":["export const isDef = function (v) {\n return v !== undefined && v !== null;\n};\nconst PLACEHOLDER_VM = {\n __placeholder: true,\n};\nexport const resolvePushedVm = (currentVm) => {\n return (isDef(currentVm) && currentVm.$vnode.data.keepAlive) ? currentVm : PLACEHOLDER_VM;\n};\nexport const isPlaceHolderVm = (vm) => vm && !!vm.__placeholder;\n\nexport const getStateId = function () {\n const state = getCurrentState();\n return isDef(state) ? state.id : undefined;\n};\n\nexport const getQuery = function (params) {\n let query = \"\";\n query = Object.keys(params)\n .map(\n (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`\n )\n .join(\"&\");\n if (query.length > 0) {\n query = `?${query}`;\n }\n return query;\n};\nconst getCurrentState = function () {\n return history.state;\n};\n\nexport const genKey = function (num, router) {\n return `keep-alive-vnode-key${Number(num)}${router.history.current.path}`;\n};\nexport const getCurrentVM = function (router) {\n return router.history.current.matched.length > 0\n ? router.history.current.matched[0].instances.default\n : undefined;\n};\nexport const setCurrentVnodeKey = function (router, key) {\n const current = getCurrentVM(router);\n if (current && current._vnode) {\n current._vnode.parent.key = key;\n }\n};\nexport const replaceFirstKeyAndCache = function (vm, key) {\n if (!isDef(vm) || !isDef(vm.cache) || !isDef(vm.keys)) {\n return;\n }\n const keys = vm.keys;\n const cache = vm.cache;\n if (keys.length === 1) {\n const vnode = cache[keys[0]];\n delete cache[keys[0]];\n keys.splice(0, 1);\n keys.push(key);\n cache[key] = vnode;\n }\n};\nexport const getFirstComponentChild = function (children) {\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length; i++) {\n const c = children[i];\n if (isDef(c) && (isDef(c.componentOptions) || isAsyncPlaceholder(c))) {\n return c;\n }\n }\n }\n};\nconst isAsyncPlaceholder = function (node) {\n return node.isComment && node.asyncFactory;\n};\n\nexport const replaceState = function (mode, router, id) {\n const { pathname, search, hash } = window.location;\n let path = `${pathname}${search}${hash}`;\n let state = isDef(history.state) ? history.state : {};\n state[\"id\"] = id;\n // optimize file:// URL\n const isFilSys = window.location.href.startsWith(\"file://\");\n history.replaceState(state, \"\", isFilSys ? null : path);\n};\n\nexport const inBrowser = typeof window !== \"undefined\";\n","export default class HistoryStack {\n constructor() {\n this.historyStackMap = [];\n }\n push(vm, index) {\n // const cur = stackPointer();\n const stack = this.historyStackMap[index];\n if (Array.isArray(stack)) {\n !stack.includes(vm) && stack.push(vm);\n this.historyStackMap[index] = stack.filter((item) => !item._isDestroyed);\n } else {\n const vms = [];\n vms.push(vm);\n this.historyStackMap[index] = vms;\n }\n }\n pop() {\n const last = this.historyStackMap.pop();\n Array.isArray(last) &&\n last.forEach(\n (vm) => vm && vm.$keepAliveDestroy && vm.$keepAliveDestroy()\n );\n }\n removeGreater(index) {\n while (this.historyStackMap.length >= index) {\n this.pop();\n }\n }\n clear() {\n this.historyStackMap = [];\n }\n}\n","import {\n getCurrentVM,\n isDef,\n getStateId,\n resolvePushedVm,\n genKey,\n replaceFirstKeyAndCache,\n getFirstComponentChild,\n isPlaceHolderVm,\n setCurrentVnodeKey,\n replaceState,\n} from \"./utils\";\nimport HistoryStack from \"./historyStack\";\n\nexport default class VueRouterKeepAliveHelper{\n constructor({ Vue, router, replaceStay }) {\n this.Vue = Vue;\n this.router = router;\n this.router._stack = 0;\n this.mode = router.mode; // hash or history\n this.historyShouldChange = false;\n this.isReplace = false;\n this.replacePrePath = undefined;\n this.preStateId = 0;\n this.pre = null;\n this.replaceStay = replaceStay || [];\n this.hacked = false;\n this.historyStack = new HistoryStack();\n this.init();\n }\n init() {\n this.routerHooks();\n this.hackRouter();\n }\n /**\n * use afterEach hook to set state.key and add the reference of vm to the historyStack\n */\n routerHooks() {\n const router = this.router;\n router.afterEach((to, from) => {\n this.historyShouldChange = true;\n // get the vm instance after render\n this.Vue.nextTick(() => {\n const current = this.currentVm;\n const pendingToPushVm = resolvePushedVm(current);\n if (this.pre === null) {\n this.onInitial(pendingToPushVm);\n } else if (this.isReplace) {\n this.onReplace(pendingToPushVm);\n } else if (this.isPush) {\n this.onPush(pendingToPushVm);\n } else {\n this.onBack(pendingToPushVm);\n }\n this.pre = current;\n this.preStateId = this.stackPointer;\n if (!isPlaceHolderVm(pendingToPushVm)) {\n setCurrentVnodeKey(router, genKey(this.stackPointer, router));\n if (!this.hacked && current) {\n this.hackKeepAliveRender(current.$vnode.parent.componentInstance);\n }\n this.historyShouldChange = false;\n }\n });\n });\n }\n /**\n * @description hack router go , replace and push functions to tell replace from back and push\n */\n hackRouter() {\n const router = this.router;\n const rtmp = router.replace;\n const rtmpf = (location, onComplete, onAbort) => {\n this.isReplace = true;\n this.replacePrePath = router.history.current.path;\n rtmp.call(router, location, onComplete, (e) => {\n this.isReplace = false;\n this.replacePrePath = undefined;\n isDef(onAbort) && onAbort(e);\n });\n };\n router.replace = function (ocation, onComplete, onAbort) {\n rtmpf(ocation, onComplete, onAbort);\n };\n\n const gstmp = router.go;\n const gstmpf = (number) => {\n this.isReplace = false;\n return gstmp.call(router, number);\n };\n router.go = function (num) {\n return gstmpf(num);\n };\n const pstmp = router.push;\n const pstmpf = (location, onComplete, onAbort) => {\n this.isReplace = false;\n if (!onComplete && !onAbort && typeof Promise !== \"undefined\") {\n return pstmp.call(router, location, onComplete, onAbort);\n } else {\n pstmp.call(router, location, onComplete, onAbort);\n }\n };\n router.push = function (location, onComplete, onAbort) {\n return pstmpf(location, onComplete, onAbort);\n };\n }\n /**\n * @description hack the render function of keep-alive component, modify the key of vnode for cache\n * @param {*} vm keep-alive component instance\n */\n hackKeepAliveRender(vm) {\n // modify the first keep alive key and cache\n replaceFirstKeyAndCache(vm, genKey(this.stackPointer, this.router));\n\n const tmp = vm.$options.render;\n const self = this;\n const router = this.router;\n vm.$options.render = function () {\n const slot = this.$slots.default;\n const vnode = getFirstComponentChild(slot); // vnode is a keep-alive-component-vnode\n if (self.historyShouldChange) {\n if (vnode && !isDef(vnode.key)) {\n if (self.isReplace) {\n vnode.key = genKey(self.stackPointer, router);\n } else if (self.isPush) {\n vnode.key = genKey(self.stackPointer + 1, router);\n } else {\n vnode.key = genKey(self.stackPointer - 1, router);\n }\n }\n } else {\n // when historyShouldChange is false should rerender only, should not create new vm ,use the same vnode.key issue#7\n vnode.key = genKey(self.stackPointer, router);\n }\n return tmp.apply(this, arguments);\n };\n this.hacked = true;\n }\n /********** callback functions ************/\n onInitial(vm) {\n const currentStateId = getStateId();\n if (isDef(currentStateId)) {\n this.setStackPointer(currentStateId);\n } else {\n this.setState(0);\n }\n this.historyStack.push(vm, this.stackPointer);\n }\n onPush(vm) {\n this.setState(this.increaseStackPointer());\n this.historyStack.push(vm, this.stackPointer);\n }\n onBack(vm) {\n this.historyStack.pop();\n this.decreaseStackPointer();\n this.historyStack.push(vm, this.stackPointer);\n }\n onReplace(vm) {\n const shouldDestroy = !(\n isDef(this.replacePrePath) &&\n this.replaceStay.includes(this.replacePrePath)\n );\n if (shouldDestroy) {\n this.pre.$keepAliveDestroy();\n }\n this.setState(this.stackPointer);\n this.historyStack.push(vm, this.stackPointer);\n this.isReplace = false;\n this.replacePrePath = undefined;\n }\n get currentVm() {\n return getCurrentVM(this.router);\n }\n get isPush() {\n if (!this.isReplace) {\n const stateId = getStateId();\n return !isDef(stateId) || this.preStateId <= stateId;\n }\n return false;\n }\n get stackPointer() {\n return this.router._stack;\n }\n setState(id) {\n this.setStackPointer(id);\n replaceState(this.mode, this.router, id);\n }\n setStackPointer(val) {\n this.router._stack = val;\n }\n increaseStackPointer() {\n return (this.router._stack += 1);\n }\n decreaseStackPointer() {\n return (this.router._stack -= 1);\n }\n}\n","import Helper from \"./helper\";\nimport { extendVue, extendHistory } from \"./extension\";\nimport { inBrowser } from \"./utils\";\n\nlet singleton = undefined;\n// TODO: 1. abstract mode support\nexport default function createHelper(config) {\n if (config.Vue === undefined || config.router === undefined) {\n console.warn(\n \"warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper\"\n );\n return;\n }\n if(singleton) {\n return singleton;\n }\n if (inBrowser) {\n extendHistory(window.history);\n }\n extendVue(config.Vue);\n return (singleton = new Helper(config));\n}","import { isDef } from \"./utils\";\n\nexport function extendHistory(history) {\n const rstmp = history.replaceState;\n history.replaceState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n rstmp.call(history, s, op, path);\n }\n const historyPushState = history.pushState;\n history.pushState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n historyPushState.call(history, s, op, path);\n }\n}\n\nexport function extendVue(Vue) {\n const dtmp = Vue.prototype.$destroy;\n /**\n * @description remove the cache in component before invoke $destroy\n */\n Vue.prototype.$keepAliveDestroy = function () {\n if (this.$vnode && this.$vnode.data.keepAlive) {\n if (\n this.$vnode.parent &&\n this.$vnode.parent.componentInstance &&\n this.$vnode.parent.componentInstance.cache\n ) {\n if (this.$vnode.componentOptions) {\n const key = !isDef(this.$vnode.key)\n ? this.$vnode.componentOptions.Ctor.cid +\n (this.$vnode.componentOptions.tag\n ? `::${this.$vnode.componentOptions.tag}`\n : \"\")\n : this.$vnode.key;\n const cache = this.$vnode.parent.componentInstance.cache;\n const keys = this.$vnode.parent.componentInstance.keys;\n if (cache[key]) {\n if (keys.length) {\n const index = keys.indexOf(key);\n if (index > -1) {\n keys.splice(index, 1);\n }\n }\n delete cache[key];\n }\n }\n }\n }\n dtmp.apply(this, arguments);\n };\n}\n"],"names":["isDef","v","PLACEHOLDER_VM","__placeholder","getStateId","state","getCurrentState","id","undefined","history","genKey","num","router","Number","current","path","getCurrentVM","matched","length","instances","default","getFirstComponentChild","children","Array","isArray","i","c","componentOptions","isAsyncPlaceholder","node","isComment","asyncFactory","inBrowser","window","HistoryStack","historyStackMap","push","vm","index","stack","this","includes","filter","item","_isDestroyed","vms","pop","last","forEach","$keepAliveDestroy","removeGreater","clear","VueRouterKeepAliveHelper","Vue","replaceStay","_stack","mode","historyShouldChange","isReplace","replacePrePath","preStateId","pre","hacked","historyStack","init","routerHooks","hackRouter","afterEach","to","from","_this","nextTick","currentVm","pendingToPushVm","$vnode","data","keepAlive","onInitial","onReplace","isPush","onPush","onBack","stackPointer","key","_vnode","parent","setCurrentVnodeKey","hackKeepAliveRender","componentInstance","rtmp","replace","ocation","onComplete","onAbort","location","_this2","call","e","rtmpf","gstmp","go","number","pstmp","Promise","pstmpf","cache","keys","vnode","splice","replaceFirstKeyAndCache","tmp","$options","render","self","slot","$slots","apply","arguments","currentStateId","setStackPointer","setState","increaseStackPointer","decreaseStackPointer","pathname","search","hash","isFilSys","href","startsWith","replaceState","val","stateId","singleton","createHelper","config","dtmp","rstmp","op","old","Object","assign","s","historyPushState","pushState","extendHistory","prototype","$destroy","Ctor","cid","tag","indexOf","Helper","console","warn"],"mappings":"sKAAO,IAAMA,EAAQ,SAAUC,UACtBA,MAAAA,GAEHC,EAAiB,CACrBC,eAAe,GAOJC,EAAa,eAClBC,EAAQC,WACPN,EAAMK,GAASA,EAAME,QAAKC,GAe7BF,EAAkB,kBACfG,QAAQJ,OAGJK,EAAS,SAAUC,EAAKC,gCACLC,OAAOF,GAAOC,EAAOH,QAAQK,QAAQC,MAExDC,EAAe,SAAUJ,UAC7BA,EAAOH,QAAQK,QAAQG,QAAQC,OAAS,EAC3CN,EAAOH,QAAQK,QAAQG,QAAQ,GAAGE,UAAUC,aAC5CZ,GAsBOa,EAAyB,SAAUC,MAC1CC,MAAMC,QAAQF,OACX,IAAIG,EAAI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AAU1I,EAAOoI,EAAI1H,OACjC2H,EAAMC,OAAOC,OAAO,GAAInI,EAAQJ,OAChCwI,EAAIF,OAAOC,OAAOF,EAAKrI,GAC7ByI,EAAiB9C,KAAKvF,EAASoI,EAAGJ,EAAI1H,IDItCiI,CAAc/G,OAAOxB,SCAC4C,EDEdiF,EAAOjF,ICDXkF,EAAOlF,EAAI4F,UAAUC,SAI3B7F,EAAI4F,UAAUhG,kBAAoB,cAC5BT,KAAKkC,QAAUlC,KAAKkC,OAAOC,KAAKC,WAEhCpC,KAAKkC,OAAOW,QACZ7C,KAAKkC,OAAOW,OAAOG,mBACnBhD,KAAKkC,OAAOW,OAAOG,kBAAkBiB,OAEjCjE,KAAKkC,OAAO/C,iBAAkB,KAC1BwD,EAAOnF,EAAMwC,KAAKkC,OAAOS,KAK3B3C,KAAKkC,OAAOS,IAJZ3C,KAAKkC,OAAO/C,iBAAiBwH,KAAKC,KACjC5G,KAAKkC,OAAO/C,iBAAiB0H,SACrB7G,KAAKkC,OAAO/C,iBAAiB0H,IAClC,IAEF5C,EAAQjE,KAAKkC,OAAOW,OAAOG,kBAAkBiB,MAC7CC,EAAOlE,KAAKkC,OAAOW,OAAOG,kBAAkBkB,QAC9CD,EAAMtB,GAAM,IACVuB,EAAKxF,OAAQ,KACToB,EAAQoE,EAAK4C,QAAQnE,GACvB7C,GAAS,GACXoE,EAAKE,OAAOtE,EAAO,UAGhBmE,EAAMtB,IAKrBoD,EAAKnB,MAAM5E,KAAM6E,YD9BXe,EAAY,IAAImB,EAAOjB,IAZ7BkB,QAAQC,KACN"} \ No newline at end of file +{"version":3,"file":"helper.esm.js","sources":["../src/utils.js","../src/historyStack.js","../src/helper.js","../src/index.js","../src/extension.js"],"sourcesContent":["export const isDef = function (v) {\n return v !== undefined && v !== null;\n};\nconst PLACEHOLDER_VM = {\n __placeholder: true,\n};\nexport const resolvePushedVm = (currentVm) => {\n return (isDef(currentVm) && currentVm.$vnode.data.keepAlive) ? currentVm : PLACEHOLDER_VM;\n};\nexport const isPlaceHolderVm = (vm) => vm && !!vm.__placeholder;\n\nexport const getStateId = function () {\n const state = getCurrentState();\n return isDef(state) ? state.id : undefined;\n};\n\nexport const getQuery = function (params) {\n let query = \"\";\n query = Object.keys(params)\n .map(\n (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`\n )\n .join(\"&\");\n if (query.length > 0) {\n query = `?${query}`;\n }\n return query;\n};\nconst getCurrentState = function () {\n return history.state;\n};\n\nexport const genKey = function (num, router) {\n return `keep-alive-vnode-key${Number(num)}${router.history.current.path}`;\n};\nexport const getCurrentVM = function (router) {\n return router.history.current.matched.length > 0\n ? router.history.current.matched[0].instances.default\n : undefined;\n};\nexport const setCurrentVnodeKey = function (router, key) {\n const current = getCurrentVM(router);\n if (current && current._vnode) {\n current._vnode.parent.key = key;\n }\n};\nexport const replaceFirstKeyAndCache = function (vm, key) {\n if (!isDef(vm) || !isDef(vm.cache) || !isDef(vm.keys)) {\n return;\n }\n const keys = vm.keys;\n const cache = vm.cache;\n if (keys.length === 1) {\n const vnode = cache[keys[0]];\n delete cache[keys[0]];\n keys.splice(0, 1);\n keys.push(key);\n cache[key] = vnode;\n }\n};\nexport const getFirstComponentChild = function (children) {\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length; i++) {\n const c = children[i];\n if (isDef(c) && (isDef(c.componentOptions) || isAsyncPlaceholder(c))) {\n return c;\n }\n }\n }\n};\nconst isAsyncPlaceholder = function (node) {\n return node.isComment && node.asyncFactory;\n};\n\nexport const replaceState = function (mode, router, id) {\n const { pathname, search, hash } = window.location;\n let path = `${pathname}${search}${hash}`;\n let state = isDef(history.state) ? history.state : {};\n state[\"id\"] = id;\n // optimize file:// URL\n const isFilSys = window.location.href.startsWith(\"file://\");\n history.replaceState(state, \"\", isFilSys ? null : path);\n};\n\nexport const inBrowser = typeof window !== \"undefined\";\n","export default class HistoryStack {\n constructor() {\n this.historyStackMap = [];\n }\n push(vm, index) {\n // const cur = stackPointer();\n const stack = this.historyStackMap[index];\n if (Array.isArray(stack)) {\n !stack.includes(vm) && stack.push(vm);\n this.historyStackMap[index] = stack.filter((item) => !item._isDestroyed);\n } else {\n const vms = [];\n vms.push(vm);\n this.historyStackMap[index] = vms;\n }\n }\n pop() {\n const last = this.historyStackMap.pop();\n Array.isArray(last) &&\n last.forEach(\n (vm) => vm && vm.$keepAliveDestroy && vm.$keepAliveDestroy()\n );\n }\n removeGreater(index) {\n while (this.historyStackMap.length >= index) {\n this.pop();\n }\n }\n clear() {\n this.historyStackMap = [];\n }\n}\n","import {\n getCurrentVM,\n isDef,\n getStateId,\n resolvePushedVm,\n genKey,\n replaceFirstKeyAndCache,\n getFirstComponentChild,\n isPlaceHolderVm,\n setCurrentVnodeKey,\n replaceState,\n} from \"./utils\";\nimport HistoryStack from \"./historyStack\";\n\nexport default class VueRouterKeepAliveHelper{\n constructor({ Vue, router, replaceStay }) {\n this.Vue = Vue;\n this.router = router;\n this.router._stack = 0;\n this.mode = router.mode; // hash or history\n this.historyShouldChange = false;\n this.isReplace = false;\n this.replacePrePath = undefined;\n this.preStateId = 0;\n this.pre = null;\n this.replaceStay = replaceStay || [];\n this.hacked = false;\n this.historyStack = new HistoryStack();\n this.init();\n }\n init() {\n this.routerHooks();\n this.hackRouter();\n }\n /**\n * use afterEach hook to set state.key and add the reference of vm to the historyStack\n */\n routerHooks() {\n const router = this.router;\n router.afterEach((to, from) => {\n this.historyShouldChange = true;\n // get the vm instance after render\n this.Vue.nextTick(() => {\n const current = this.currentVm;\n const pendingToPushVm = resolvePushedVm(current);\n if (this.pre === null) {\n this.onInitial(pendingToPushVm);\n } else if (this.isReplace) {\n this.onReplace(pendingToPushVm);\n } else if (this.isPush) {\n this.onPush(pendingToPushVm);\n } else {\n this.onBack(pendingToPushVm);\n }\n this.pre = current;\n this.preStateId = this.stackPointer;\n if (!isPlaceHolderVm(pendingToPushVm)) {\n setCurrentVnodeKey(router, genKey(this.stackPointer, router));\n if (!this.hacked && current) {\n this.hackKeepAliveRender(current.$vnode.parent.componentInstance);\n }\n this.historyShouldChange = false;\n }\n });\n });\n }\n /**\n * @description hack router go , replace and push functions to tell replace from back and push\n */\n hackRouter() {\n const router = this.router;\n const rtmp = router.replace;\n const rtmpf = (location, onComplete, onAbort) => {\n this.isReplace = true;\n this.replacePrePath = router.history.current.path;\n rtmp.call(router, location, onComplete, (e) => {\n this.isReplace = false;\n this.replacePrePath = undefined;\n isDef(onAbort) && onAbort(e);\n });\n };\n router.replace = function (ocation, onComplete, onAbort) {\n rtmpf(ocation, onComplete, onAbort);\n };\n\n const gstmp = router.go;\n const gstmpf = (number) => {\n this.isReplace = false;\n return gstmp.call(router, number);\n };\n router.go = function (num) {\n return gstmpf(num);\n };\n const pstmp = router.push;\n const pstmpf = (location, onComplete, onAbort) => {\n this.isReplace = false;\n if (!onComplete && !onAbort && typeof Promise !== \"undefined\") {\n return pstmp.call(router, location, onComplete, onAbort);\n } else {\n pstmp.call(router, location, onComplete, onAbort);\n }\n };\n router.push = function (location, onComplete, onAbort) {\n return pstmpf(location, onComplete, onAbort);\n };\n }\n /**\n * @description hack the render function of keep-alive component, modify the key of vnode for cache\n * @param {*} vm keep-alive component instance\n */\n hackKeepAliveRender(vm) {\n // modify the first keep alive key and cache\n replaceFirstKeyAndCache(vm, genKey(this.stackPointer, this.router));\n\n const tmp = vm.$options.render;\n const self = this;\n const router = this.router;\n vm.$options.render = function () {\n const slot = this.$slots.default;\n const vnode = getFirstComponentChild(slot); // vnode is a keep-alive-component-vnode\n if (self.historyShouldChange) {\n if (vnode && !isDef(vnode.key)) {\n if (self.isReplace) {\n vnode.key = genKey(self.stackPointer, router);\n } else if (self.isPush) {\n vnode.key = genKey(self.stackPointer + 1, router);\n } else {\n vnode.key = genKey(self.stackPointer - 1, router);\n }\n }\n } else {\n // when historyShouldChange is false should rerender only, should not create new vm ,use the same vnode.key issue#7\n vnode.key = genKey(self.stackPointer, router);\n }\n return tmp.apply(this, arguments);\n };\n this.hacked = true;\n }\n /********** callback functions ************/\n onInitial(vm) {\n const currentStateId = getStateId();\n if (isDef(currentStateId)) {\n this.setStackPointer(currentStateId);\n } else {\n this.setState(0);\n }\n this.historyStack.push(vm, this.stackPointer);\n }\n onPush(vm) {\n this.setState(this.increaseStackPointer());\n this.historyStack.push(vm, this.stackPointer);\n }\n onBack(vm) {\n this.historyStack.pop();\n this.decreaseStackPointer();\n this.historyStack.push(vm, this.stackPointer);\n }\n onReplace(vm) {\n const shouldDestroy = !(\n isDef(this.replacePrePath) &&\n this.replaceStay.includes(this.replacePrePath)\n );\n if (shouldDestroy) {\n this.pre?.$keepAliveDestroy();\n }\n this.setState(this.stackPointer);\n this.historyStack.push(vm, this.stackPointer);\n this.isReplace = false;\n this.replacePrePath = undefined;\n }\n get currentVm() {\n return getCurrentVM(this.router);\n }\n get isPush() {\n if (!this.isReplace) {\n const stateId = getStateId();\n return !isDef(stateId) || this.preStateId <= stateId;\n }\n return false;\n }\n get stackPointer() {\n return this.router._stack;\n }\n setState(id) {\n this.setStackPointer(id);\n replaceState(this.mode, this.router, id);\n }\n setStackPointer(val) {\n this.router._stack = val;\n }\n increaseStackPointer() {\n return (this.router._stack += 1);\n }\n decreaseStackPointer() {\n return (this.router._stack -= 1);\n }\n}\n","import Helper from \"./helper\";\nimport { extendVue, extendHistory } from \"./extension\";\nimport { inBrowser } from \"./utils\";\n\nlet singleton = undefined;\n// TODO: 1. abstract mode support\nexport default function createHelper(config) {\n if (config.Vue === undefined || config.router === undefined) {\n console.warn(\n \"warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper\"\n );\n return;\n }\n if(singleton) {\n return singleton;\n }\n if (inBrowser) {\n extendHistory(window.history);\n }\n extendVue(config.Vue);\n return (singleton = new Helper(config));\n}","import { isDef } from \"./utils\";\n\nexport function extendHistory(history) {\n const rstmp = history.replaceState;\n history.replaceState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n rstmp.call(history, s, op, path);\n }\n const historyPushState = history.pushState;\n history.pushState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n historyPushState.call(history, s, op, path);\n }\n}\n\nexport function extendVue(Vue) {\n const dtmp = Vue.prototype.$destroy;\n /**\n * @description remove the cache in component before invoke $destroy\n */\n Vue.prototype.$keepAliveDestroy = function () {\n if (this.$vnode && this.$vnode.data.keepAlive) {\n if (\n this.$vnode.parent &&\n this.$vnode.parent.componentInstance &&\n this.$vnode.parent.componentInstance.cache\n ) {\n if (this.$vnode.componentOptions) {\n const key = !isDef(this.$vnode.key)\n ? this.$vnode.componentOptions.Ctor.cid +\n (this.$vnode.componentOptions.tag\n ? `::${this.$vnode.componentOptions.tag}`\n : \"\")\n : this.$vnode.key;\n const cache = this.$vnode.parent.componentInstance.cache;\n const keys = this.$vnode.parent.componentInstance.keys;\n if (cache[key]) {\n if (keys.length) {\n const index = keys.indexOf(key);\n if (index > -1) {\n keys.splice(index, 1);\n }\n }\n delete cache[key];\n }\n }\n }\n }\n dtmp.apply(this, arguments);\n };\n}\n"],"names":["isDef","v","PLACEHOLDER_VM","__placeholder","getStateId","state","getCurrentState","id","undefined","history","genKey","num","router","Number","current","path","getCurrentVM","matched","length","instances","default","getFirstComponentChild","children","Array","isArray","i","c","componentOptions","isAsyncPlaceholder","node","isComment","asyncFactory","inBrowser","window","HistoryStack","historyStackMap","push","vm","index","stack","this","includes","filter","item","_isDestroyed","vms","pop","last","forEach","$keepAliveDestroy","removeGreater","clear","VueRouterKeepAliveHelper","Vue","replaceStay","_stack","mode","historyShouldChange","isReplace","replacePrePath","preStateId","pre","hacked","historyStack","init","routerHooks","hackRouter","afterEach","to","from","_this","nextTick","currentVm","pendingToPushVm","$vnode","data","keepAlive","onInitial","onReplace","isPush","onPush","onBack","stackPointer","key","_vnode","parent","setCurrentVnodeKey","hackKeepAliveRender","componentInstance","rtmp","replace","ocation","onComplete","onAbort","location","_this2","call","e","rtmpf","gstmp","go","number","pstmp","Promise","pstmpf","cache","keys","vnode","splice","replaceFirstKeyAndCache","tmp","$options","render","self","slot","$slots","apply","arguments","currentStateId","setStackPointer","setState","increaseStackPointer","decreaseStackPointer","pathname","search","hash","isFilSys","href","startsWith","replaceState","val","stateId","singleton","createHelper","config","dtmp","rstmp","op","old","Object","assign","s","historyPushState","pushState","extendHistory","prototype","$destroy","Ctor","cid","tag","indexOf","Helper","console","warn"],"mappings":"sKAAO,IAAMA,EAAQ,SAAUC,UACtBA,MAAAA,GAEHC,EAAiB,CACrBC,eAAe,GAOJC,EAAa,eAClBC,EAAQC,WACPN,EAAMK,GAASA,EAAME,QAAKC,GAe7BF,EAAkB,kBACfG,QAAQJ,OAGJK,EAAS,SAAUC,EAAKC,gCACLC,OAAOF,GAAOC,EAAOH,QAAQK,QAAQC,MAExDC,EAAe,SAAUJ,UAC7BA,EAAOH,QAAQK,QAAQG,QAAQC,OAAS,EAC3CN,EAAOH,QAAQK,QAAQG,QAAQ,GAAGE,UAAUC,aAC5CZ,GAsBOa,EAAyB,SAAUC,MAC1CC,MAAMC,QAAQF,OACX,IAAIG,EAAI,EAAGA,EAAIH,EAASJ,OAAQO,IAAK,KAClCC,EAAIJ,EAASG,MACfzB,EAAM0B,KAAO1B,EAAM0B,EAAEC,mBAAqBC,EAAmBF,WACxDA,IAKTE,EAAqB,SAAUC,UAC5BA,EAAKC,WAAaD,EAAKE,cAanBC,EAA8B,oBAAXC,OCpFXC,+BAEZC,gBAAkB,8BAEzBC,KAAA,SAAKC,EAAIC,OAEDC,EAAQC,KAAKL,gBAAgBG,MAC/Bf,MAAMC,QAAQe,IACfA,EAAME,SAASJ,IAAOE,EAAMH,KAAKC,QAC7BF,gBAAgBG,GAASC,EAAMG,QAAO,SAACC,UAAUA,EAAKC,oBACtD,KACCC,EAAM,GACZA,EAAIT,KAAKC,QACJF,gBAAgBG,GAASO,MAGlCC,IAAA,eACQC,EAAOP,KAAKL,gBAAgBW,MAClCvB,MAAMC,QAAQuB,IACZA,EAA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OAAO/C,iBAAiBwH,KAAKC,KACjC5G,KAAKkC,OAAO/C,iBAAiB0H,SACrB7G,KAAKkC,OAAO/C,iBAAiB0H,IAClC,IAEF5C,EAAQjE,KAAKkC,OAAOW,OAAOG,kBAAkBiB,MAC7CC,EAAOlE,KAAKkC,OAAOW,OAAOG,kBAAkBkB,QAC9CD,EAAMtB,GAAM,IACVuB,EAAKxF,OAAQ,KACToB,EAAQoE,EAAK4C,QAAQnE,GACvB7C,GAAS,GACXoE,EAAKE,OAAOtE,EAAO,UAGhBmE,EAAMtB,IAKrBoD,EAAKnB,MAAM5E,KAAM6E,YD9BXe,EAAY,IAAImB,EAAOjB,IAZ7BkB,QAAQC,KACN"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 90fa39f..fe8f83d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,2 +1,2 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).createHelper=e()}(this,(function(){"use strict";function t(t,e){for(var n=0;n0?t.history.current.matched[0].instances.default:void 0},a=function(t){if(Array.isArray(t))for(var n=0;n=t;)this.pop()},e.clear=function(){this.historyStackMap=[]},t}(),p=function(){function r(t){var e=t.Vue,n=t.router,i=t.replaceStay;this.Vue=e,this.router=n,this.router._stack=0,this.mode=n.mode,this.historyShouldChange=!1,this.isReplace=!1,this.replacePrePath=void 0,this.preStateId=0,this.pre=null,this.replaceStay=i||[],this.hacked=!1,this.historyStack=new u,this.init()}var c,h,p,l=r.prototype;return l.init=function(){this.routerHooks(),this.hackRouter()},l.routerHooks=function(){var t=this,i=this.router;i.afterEach((function(r,a){t.historyShouldChange=!0,t.Vue.nextTick((function(){var r,a,c=t.currentVm,h=e(r=c)&&r.$vnode.data.keepAlive?r:n;null===t.pre?t.onInitial(h):t.isReplace?t.onReplace(h):t.isPush?t.onPush(h):t.onBack(h),t.pre=c,t.preStateId=t.stackPointer,(a=h)&&a.__placeholder||(!function(t,e){var n=s(t);n&&n._vnode&&(n._vnode.parent.key=e)}(i,o(t.stackPointer,i)),!t.hacked&&c&&t.hackKeepAliveRender(c.$vnode.parent.componentInstance),t.historyShouldChange=!1)}))}))},l.hackRouter=function(){var t=this,n=this.router,i=n.replace;n.replace=function(r,o,s){!function(r,o,s){t.isReplace=!0,t.replacePrePath=n.history.current.path,i.call(n,r,o,(function(n){t.isReplace=!1,t.replacePrePath=void 0,e(s)&&s(n)}))}(r,o,s)};var r=n.go;n.go=function(e){return i=e,t.isReplace=!1,r.call(n,i);var i};var o=n.push;n.push=function(e,i,r){return function(e,i,r){if(t.isReplace=!1,!i&&!r&&"undefined"!=typeof Promise)return o.call(n,e,i,r);o.call(n,e,i,r)}(e,i,r)}},l.hackKeepAliveRender=function(t){!function(t,n){if(e(t)&&e(t.cache)&&e(t.keys)){var i=t.keys,r=t.cache;if(1===i.length){var o=r[i[0]];delete r[i[0]],i.splice(0,1),i.push(n),r[n]=o}}}(t,o(this.stackPointer,this.router));var n=t.$options.render,i=this,r=this.router;t.$options.render=function(){var t=this.$slots.default,s=a(t);return i.historyShouldChange?s&&!e(s.key)&&(i.isReplace?s.key=o(i.stackPointer,r):i.isPush?s.key=o(i.stackPointer+1,r):s.key=o(i.stackPointer-1,r)):s.key=o(i.stackPointer,r),n.apply(this,arguments)},this.hacked=!0},l.onInitial=function(t){var n=i();e(n)?this.setStackPointer(n):this.setState(0),this.historyStack.push(t,this.stackPointer)},l.onPush=function(t){this.setState(this.increaseStackPointer()),this.historyStack.push(t,this.stackPointer)},l.onBack=function(t){this.historyStack.pop(),this.decreaseStackPointer(),this.historyStack.push(t,this.stackPointer)},l.onReplace=function(t){!(e(this.replacePrePath)&&this.replaceStay.includes(this.replacePrePath))&&this.pre.$keepAliveDestroy(),this.setState(this.stackPointer),this.historyStack.push(t,this.stackPointer),this.isReplace=!1,this.replacePrePath=void 0},l.setState=function(t){this.setStackPointer(t),function(t,n,i){var r=window.location,o=""+r.pathname+r.search+r.hash,s=e(history.state)?history.state:{};s.id=i;var a=window.location.href.startsWith("file://");history.replaceState(s,"",a?null:o)}(this.mode,this.router,t)},l.setStackPointer=function(t){this.router._stack=t},l.increaseStackPointer=function(){return this.router._stack+=1},l.decreaseStackPointer=function(){return this.router._stack-=1},c=r,(h=[{key:"currentVm",get:function(){return s(this.router)}},{key:"isPush",get:function(){if(!this.isReplace){var t=i();return!e(t)||this.preStateId<=t}return!1}},{key:"stackPointer",get:function(){return this.router._stack}}])&&t(c.prototype,h),p&&t(c,p),r}();var l=void 0;return function(t){var n,i;if(void 0!==t.Vue&&void 0!==t.router)return l||(h&&function(t){var e=t.replaceState;t.replaceState=function(n,i,r){var o=Object.assign({},t.state),s=Object.assign(o,n);e.call(t,s,i,r)};var n=t.pushState;t.pushState=function(e,i,r){var o=Object.assign({},t.state),s=Object.assign(o,e);n.call(t,s,i,r)}}(window.history),n=t.Vue,i=n.prototype.$destroy,n.prototype.$keepAliveDestroy=function(){if(this.$vnode&&this.$vnode.data.keepAlive&&this.$vnode.parent&&this.$vnode.parent.componentInstance&&this.$vnode.parent.componentInstance.cache&&this.$vnode.componentOptions){var t=e(this.$vnode.key)?this.$vnode.key:this.$vnode.componentOptions.Ctor.cid+(this.$vnode.componentOptions.tag?"::"+this.$vnode.componentOptions.tag:""),n=this.$vnode.parent.componentInstance.cache,r=this.$vnode.parent.componentInstance.keys;if(n[t]){if(r.length){var o=r.indexOf(t);o>-1&&r.splice(o,1)}delete n[t]}}i.apply(this,arguments)},l=new p(t));console.warn("warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper")}})); +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).createHelper=e()}(this,(function(){"use strict";function t(t,e){for(var n=0;n0?t.history.current.matched[0].instances.default:void 0},a=function(t){if(Array.isArray(t))for(var n=0;n=t;)this.pop()},e.clear=function(){this.historyStackMap=[]},t}(),p=function(){function r(t){var e=t.Vue,n=t.router,i=t.replaceStay;this.Vue=e,this.router=n,this.router._stack=0,this.mode=n.mode,this.historyShouldChange=!1,this.isReplace=!1,this.replacePrePath=void 0,this.preStateId=0,this.pre=null,this.replaceStay=i||[],this.hacked=!1,this.historyStack=new u,this.init()}var c,h,p,l=r.prototype;return l.init=function(){this.routerHooks(),this.hackRouter()},l.routerHooks=function(){var t=this,i=this.router;i.afterEach((function(r,a){t.historyShouldChange=!0,t.Vue.nextTick((function(){var r,a,c=t.currentVm,h=e(r=c)&&r.$vnode.data.keepAlive?r:n;null===t.pre?t.onInitial(h):t.isReplace?t.onReplace(h):t.isPush?t.onPush(h):t.onBack(h),t.pre=c,t.preStateId=t.stackPointer,(a=h)&&a.__placeholder||(!function(t,e){var n=s(t);n&&n._vnode&&(n._vnode.parent.key=e)}(i,o(t.stackPointer,i)),!t.hacked&&c&&t.hackKeepAliveRender(c.$vnode.parent.componentInstance),t.historyShouldChange=!1)}))}))},l.hackRouter=function(){var t=this,n=this.router,i=n.replace;n.replace=function(r,o,s){!function(r,o,s){t.isReplace=!0,t.replacePrePath=n.history.current.path,i.call(n,r,o,(function(n){t.isReplace=!1,t.replacePrePath=void 0,e(s)&&s(n)}))}(r,o,s)};var r=n.go;n.go=function(e){return i=e,t.isReplace=!1,r.call(n,i);var i};var o=n.push;n.push=function(e,i,r){return function(e,i,r){if(t.isReplace=!1,!i&&!r&&"undefined"!=typeof Promise)return o.call(n,e,i,r);o.call(n,e,i,r)}(e,i,r)}},l.hackKeepAliveRender=function(t){!function(t,n){if(e(t)&&e(t.cache)&&e(t.keys)){var i=t.keys,r=t.cache;if(1===i.length){var o=r[i[0]];delete r[i[0]],i.splice(0,1),i.push(n),r[n]=o}}}(t,o(this.stackPointer,this.router));var n=t.$options.render,i=this,r=this.router;t.$options.render=function(){var t=this.$slots.default,s=a(t);return i.historyShouldChange?s&&!e(s.key)&&(i.isReplace?s.key=o(i.stackPointer,r):i.isPush?s.key=o(i.stackPointer+1,r):s.key=o(i.stackPointer-1,r)):s.key=o(i.stackPointer,r),n.apply(this,arguments)},this.hacked=!0},l.onInitial=function(t){var n=i();e(n)?this.setStackPointer(n):this.setState(0),this.historyStack.push(t,this.stackPointer)},l.onPush=function(t){this.setState(this.increaseStackPointer()),this.historyStack.push(t,this.stackPointer)},l.onBack=function(t){this.historyStack.pop(),this.decreaseStackPointer(),this.historyStack.push(t,this.stackPointer)},l.onReplace=function(t){var n;e(this.replacePrePath)&&this.replaceStay.includes(this.replacePrePath)||(null==(n=this.pre)||n.$keepAliveDestroy());this.setState(this.stackPointer),this.historyStack.push(t,this.stackPointer),this.isReplace=!1,this.replacePrePath=void 0},l.setState=function(t){this.setStackPointer(t),function(t,n,i){var r=window.location,o=""+r.pathname+r.search+r.hash,s=e(history.state)?history.state:{};s.id=i;var a=window.location.href.startsWith("file://");history.replaceState(s,"",a?null:o)}(this.mode,this.router,t)},l.setStackPointer=function(t){this.router._stack=t},l.increaseStackPointer=function(){return this.router._stack+=1},l.decreaseStackPointer=function(){return this.router._stack-=1},c=r,(h=[{key:"currentVm",get:function(){return s(this.router)}},{key:"isPush",get:function(){if(!this.isReplace){var t=i();return!e(t)||this.preStateId<=t}return!1}},{key:"stackPointer",get:function(){return this.router._stack}}])&&t(c.prototype,h),p&&t(c,p),r}();var l=void 0;return function(t){var n,i;if(void 0!==t.Vue&&void 0!==t.router)return l||(h&&function(t){var e=t.replaceState;t.replaceState=function(n,i,r){var o=Object.assign({},t.state),s=Object.assign(o,n);e.call(t,s,i,r)};var n=t.pushState;t.pushState=function(e,i,r){var o=Object.assign({},t.state),s=Object.assign(o,e);n.call(t,s,i,r)}}(window.history),n=t.Vue,i=n.prototype.$destroy,n.prototype.$keepAliveDestroy=function(){if(this.$vnode&&this.$vnode.data.keepAlive&&this.$vnode.parent&&this.$vnode.parent.componentInstance&&this.$vnode.parent.componentInstance.cache&&this.$vnode.componentOptions){var t=e(this.$vnode.key)?this.$vnode.key:this.$vnode.componentOptions.Ctor.cid+(this.$vnode.componentOptions.tag?"::"+this.$vnode.componentOptions.tag:""),n=this.$vnode.parent.componentInstance.cache,r=this.$vnode.parent.componentInstance.keys;if(n[t]){if(r.length){var o=r.indexOf(t);o>-1&&r.splice(o,1)}delete n[t]}}i.apply(this,arguments)},l=new p(t));console.warn("warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper")}})); //# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map index b1b9340..57627cc 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sources":["../src/utils.js","../src/historyStack.js","../src/helper.js","../src/index.js","../src/extension.js"],"sourcesContent":["export const isDef = function (v) {\n return v !== undefined && v !== null;\n};\nconst PLACEHOLDER_VM = {\n __placeholder: true,\n};\nexport const resolvePushedVm = (currentVm) => {\n return (isDef(currentVm) && currentVm.$vnode.data.keepAlive) ? currentVm : PLACEHOLDER_VM;\n};\nexport const isPlaceHolderVm = (vm) => vm && !!vm.__placeholder;\n\nexport const getStateId = function () {\n const state = getCurrentState();\n return isDef(state) ? state.id : undefined;\n};\n\nexport const getQuery = function (params) {\n let query = \"\";\n query = Object.keys(params)\n .map(\n (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`\n )\n .join(\"&\");\n if (query.length > 0) {\n query = `?${query}`;\n }\n return query;\n};\nconst getCurrentState = function () {\n return history.state;\n};\n\nexport const genKey = function (num, router) {\n return `keep-alive-vnode-key${Number(num)}${router.history.current.path}`;\n};\nexport const getCurrentVM = function (router) {\n return router.history.current.matched.length > 0\n ? router.history.current.matched[0].instances.default\n : undefined;\n};\nexport const setCurrentVnodeKey = function (router, key) {\n const current = getCurrentVM(router);\n if (current && current._vnode) {\n current._vnode.parent.key = key;\n }\n};\nexport const replaceFirstKeyAndCache = function (vm, key) {\n if (!isDef(vm) || !isDef(vm.cache) || !isDef(vm.keys)) {\n return;\n }\n const keys = vm.keys;\n const cache = vm.cache;\n if (keys.length === 1) {\n const vnode = cache[keys[0]];\n delete cache[keys[0]];\n keys.splice(0, 1);\n keys.push(key);\n cache[key] = vnode;\n }\n};\nexport const getFirstComponentChild = function (children) {\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length; i++) {\n const c = children[i];\n if (isDef(c) && (isDef(c.componentOptions) || isAsyncPlaceholder(c))) {\n return c;\n }\n }\n }\n};\nconst isAsyncPlaceholder = function (node) {\n return node.isComment && node.asyncFactory;\n};\n\nexport const replaceState = function (mode, router, id) {\n const { pathname, search, hash } = window.location;\n let path = `${pathname}${search}${hash}`;\n let state = isDef(history.state) ? history.state : {};\n state[\"id\"] = id;\n // optimize file:// URL\n const isFilSys = window.location.href.startsWith(\"file://\");\n history.replaceState(state, \"\", isFilSys ? null : path);\n};\n\nexport const inBrowser = typeof window !== \"undefined\";\n","export default class HistoryStack {\n constructor() {\n this.historyStackMap = [];\n }\n push(vm, index) {\n // const cur = stackPointer();\n const stack = this.historyStackMap[index];\n if (Array.isArray(stack)) {\n !stack.includes(vm) && stack.push(vm);\n this.historyStackMap[index] = stack.filter((item) => !item._isDestroyed);\n } else {\n const vms = [];\n vms.push(vm);\n this.historyStackMap[index] = vms;\n }\n }\n pop() {\n const last = this.historyStackMap.pop();\n Array.isArray(last) &&\n last.forEach(\n (vm) => vm && vm.$keepAliveDestroy && vm.$keepAliveDestroy()\n );\n }\n removeGreater(index) {\n while (this.historyStackMap.length >= index) {\n this.pop();\n }\n }\n clear() {\n this.historyStackMap = [];\n }\n}\n","import {\n getCurrentVM,\n isDef,\n getStateId,\n resolvePushedVm,\n genKey,\n replaceFirstKeyAndCache,\n getFirstComponentChild,\n isPlaceHolderVm,\n setCurrentVnodeKey,\n replaceState,\n} from \"./utils\";\nimport HistoryStack from \"./historyStack\";\n\nexport default class VueRouterKeepAliveHelper{\n constructor({ Vue, router, replaceStay }) {\n this.Vue = Vue;\n this.router = router;\n this.router._stack = 0;\n this.mode = router.mode; // hash or history\n this.historyShouldChange = false;\n this.isReplace = false;\n this.replacePrePath = undefined;\n this.preStateId = 0;\n this.pre = null;\n this.replaceStay = replaceStay || [];\n this.hacked = false;\n this.historyStack = new HistoryStack();\n this.init();\n }\n init() {\n this.routerHooks();\n this.hackRouter();\n }\n /**\n * use afterEach hook to set state.key and add the reference of vm to the historyStack\n */\n routerHooks() {\n const router = this.router;\n router.afterEach((to, from) => {\n this.historyShouldChange = true;\n // get the vm instance after render\n this.Vue.nextTick(() => {\n const current = this.currentVm;\n const pendingToPushVm = resolvePushedVm(current);\n if (this.pre === null) {\n this.onInitial(pendingToPushVm);\n } else if (this.isReplace) {\n this.onReplace(pendingToPushVm);\n } else if (this.isPush) {\n this.onPush(pendingToPushVm);\n } else {\n this.onBack(pendingToPushVm);\n }\n this.pre = current;\n this.preStateId = this.stackPointer;\n if (!isPlaceHolderVm(pendingToPushVm)) {\n setCurrentVnodeKey(router, genKey(this.stackPointer, router));\n if (!this.hacked && current) {\n this.hackKeepAliveRender(current.$vnode.parent.componentInstance);\n }\n this.historyShouldChange = false;\n }\n });\n });\n }\n /**\n * @description hack router go , replace and push functions to tell replace from back and push\n */\n hackRouter() {\n const router = this.router;\n const rtmp = router.replace;\n const rtmpf = (location, onComplete, onAbort) => {\n this.isReplace = true;\n this.replacePrePath = router.history.current.path;\n rtmp.call(router, location, onComplete, (e) => {\n this.isReplace = false;\n this.replacePrePath = undefined;\n isDef(onAbort) && onAbort(e);\n });\n };\n router.replace = function (ocation, onComplete, onAbort) {\n rtmpf(ocation, onComplete, onAbort);\n };\n\n const gstmp = router.go;\n const gstmpf = (number) => {\n this.isReplace = false;\n return gstmp.call(router, number);\n };\n router.go = function (num) {\n return gstmpf(num);\n };\n const pstmp = router.push;\n const pstmpf = (location, onComplete, onAbort) => {\n this.isReplace = false;\n if (!onComplete && !onAbort && typeof Promise !== \"undefined\") {\n return pstmp.call(router, location, onComplete, onAbort);\n } else {\n pstmp.call(router, location, onComplete, onAbort);\n }\n };\n router.push = function (location, onComplete, onAbort) {\n return pstmpf(location, onComplete, onAbort);\n };\n }\n /**\n * @description hack the render function of keep-alive component, modify the key of vnode for cache\n * @param {*} vm keep-alive component instance\n */\n hackKeepAliveRender(vm) {\n // modify the first keep alive key and cache\n replaceFirstKeyAndCache(vm, genKey(this.stackPointer, this.router));\n\n const tmp = vm.$options.render;\n const self = this;\n const router = this.router;\n vm.$options.render = function () {\n const slot = this.$slots.default;\n const vnode = getFirstComponentChild(slot); // vnode is a keep-alive-component-vnode\n if (self.historyShouldChange) {\n if (vnode && !isDef(vnode.key)) {\n if (self.isReplace) {\n vnode.key = genKey(self.stackPointer, router);\n } else if (self.isPush) {\n vnode.key = genKey(self.stackPointer + 1, router);\n } else {\n vnode.key = genKey(self.stackPointer - 1, router);\n }\n }\n } else {\n // when historyShouldChange is false should rerender only, should not create new vm ,use the same vnode.key issue#7\n vnode.key = genKey(self.stackPointer, router);\n }\n return tmp.apply(this, arguments);\n };\n this.hacked = true;\n }\n /********** callback functions ************/\n onInitial(vm) {\n const currentStateId = getStateId();\n if (isDef(currentStateId)) {\n this.setStackPointer(currentStateId);\n } else {\n this.setState(0);\n }\n this.historyStack.push(vm, this.stackPointer);\n }\n onPush(vm) {\n this.setState(this.increaseStackPointer());\n this.historyStack.push(vm, this.stackPointer);\n }\n onBack(vm) {\n this.historyStack.pop();\n this.decreaseStackPointer();\n this.historyStack.push(vm, this.stackPointer);\n }\n onReplace(vm) {\n const shouldDestroy = !(\n isDef(this.replacePrePath) &&\n this.replaceStay.includes(this.replacePrePath)\n );\n if (shouldDestroy) {\n this.pre.$keepAliveDestroy();\n }\n this.setState(this.stackPointer);\n this.historyStack.push(vm, this.stackPointer);\n this.isReplace = false;\n this.replacePrePath = undefined;\n }\n get currentVm() {\n return getCurrentVM(this.router);\n }\n get isPush() {\n if (!this.isReplace) {\n const stateId = getStateId();\n return !isDef(stateId) || this.preStateId <= stateId;\n }\n return false;\n }\n get stackPointer() {\n return this.router._stack;\n }\n setState(id) {\n this.setStackPointer(id);\n replaceState(this.mode, this.router, id);\n }\n setStackPointer(val) {\n this.router._stack = val;\n }\n increaseStackPointer() {\n return (this.router._stack += 1);\n }\n decreaseStackPointer() {\n return (this.router._stack -= 1);\n }\n}\n","import Helper from \"./helper\";\nimport { extendVue, extendHistory } from \"./extension\";\nimport { inBrowser } from \"./utils\";\n\nlet singleton = undefined;\n// TODO: 1. abstract mode support\nexport default function createHelper(config) {\n if (config.Vue === undefined || config.router === undefined) {\n console.warn(\n \"warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper\"\n );\n return;\n }\n if(singleton) {\n return singleton;\n }\n if (inBrowser) {\n extendHistory(window.history);\n }\n extendVue(config.Vue);\n return (singleton = new Helper(config));\n}","import { isDef } from \"./utils\";\n\nexport function extendHistory(history) {\n const rstmp = history.replaceState;\n history.replaceState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n rstmp.call(history, s, op, path);\n }\n const historyPushState = history.pushState;\n history.pushState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n historyPushState.call(history, s, op, path);\n }\n}\n\nexport function extendVue(Vue) {\n const dtmp = Vue.prototype.$destroy;\n /**\n * @description remove the cache in component before invoke $destroy\n */\n Vue.prototype.$keepAliveDestroy = function () {\n if (this.$vnode && this.$vnode.data.keepAlive) {\n if (\n this.$vnode.parent &&\n this.$vnode.parent.componentInstance &&\n this.$vnode.parent.componentInstance.cache\n ) {\n if (this.$vnode.componentOptions) {\n const key = !isDef(this.$vnode.key)\n ? this.$vnode.componentOptions.Ctor.cid +\n (this.$vnode.componentOptions.tag\n ? `::${this.$vnode.componentOptions.tag}`\n : \"\")\n : this.$vnode.key;\n const cache = this.$vnode.parent.componentInstance.cache;\n const keys = this.$vnode.parent.componentInstance.keys;\n if (cache[key]) {\n if (keys.length) {\n const index = keys.indexOf(key);\n if (index > -1) {\n keys.splice(index, 1);\n }\n }\n delete cache[key];\n }\n }\n }\n }\n dtmp.apply(this, arguments);\n };\n}\n"],"names":["isDef","v","PLACEHOLDER_VM","__placeholder","getStateId","state","getCurrentState","id","undefined","history","genKey","num","router","Number","current","path","getCurrentVM","matched","length","instances","default","getFirstComponentChild","children","Array","isArray","i","c","componentOptions","isAsyncPlaceholder","node","isComment","asyncFactory","inBrowser","window","HistoryStack","historyStackMap","push","vm","index","stack","this","includes","filter","item","_isDestroyed","vms","pop","last","forEach","$keepAliveDestroy","removeGreater","clear","VueRouterKeepAliveHelper","Vue","replaceStay","_stack","mode","historyShouldChange","isReplace","replacePrePath","preStateId","pre","hacked","historyStack","init","routerHooks","hackRouter","afterEach","to","from","_this","nextTick","currentVm","pendingToPushVm","$vnode","data","keepAlive","onInitial","onReplace","isPush","onPush","onBack","stackPointer","key","_vnode","parent","setCurrentVnodeKey","hackKeepAliveRender","componentInstance","rtmp","replace","ocation","onComplete","onAbort","location","_this2","call","e","rtmpf","gstmp","go","number","pstmp","Promise","pstmpf","cache","keys","vnode","splice","replaceFirstKeyAndCache","tmp","$options","render","self","slot","$slots","apply","arguments","currentStateId","setStackPointer","setState","increaseStackPointer","decreaseStackPointer","pathname","search","hash","isFilSys","href","startsWith","replaceState","val","stateId","singleton","config","dtmp","rstmp","op","old","Object","assign","s","historyPushState","pushState","extendHistory","prototype","$destroy","Ctor","cid","tag","indexOf","Helper","console","warn"],"mappings":"mZAAO,IAAMA,EAAQ,SAAUC,UACtBA,MAAAA,GAEHC,EAAiB,CACrBC,eAAe,GAOJC,EAAa,eAClBC,EAAQC,WACPN,EAAMK,GAASA,EAAME,QAAKC,GAe7BF,EAAkB,kBACfG,QAAQJ,OAGJK,EAAS,SAAUC,EAAKC,gCACLC,OAAOF,GAAOC,EAAOH,QAAQK,QAAQC,MAExDC,EAAe,SAAUJ,UAC7BA,EAAOH,QAAQK,QAAQG,QAAQC,OAAS,EAC3CN,EAAOH,QAAQK,QAAQG,QAAQ,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AKvF,EAASmI,EAAGJ,EAAIzH,QAEvB8H,EAAmBpI,EAAQqI,UACjCrI,EAAQqI,UAAY,SAAUzI,EAAOmI,EAAIzH,OACjC0H,EAAMC,OAAOC,OAAO,GAAIlI,EAAQJ,OAChCuI,EAAIF,OAAOC,OAAOF,EAAKpI,GAC7BwI,EAAiB7C,KAAKvF,EAASmI,EAAGJ,EAAIzH,IDItCgI,CAAc9G,OAAOxB,SCAC4C,EDEdgF,EAAOhF,ICDXiF,EAAOjF,EAAI2F,UAAUC,SAI3B5F,EAAI2F,UAAU/F,kBAAoB,cAC5BT,KAAKkC,QAAUlC,KAAKkC,OAAOC,KAAKC,WAEhCpC,KAAKkC,OAAOW,QACZ7C,KAAKkC,OAAOW,OAAOG,mBACnBhD,KAAKkC,OAAOW,OAAOG,kBAAkBiB,OAEjCjE,KAAKkC,OAAO/C,iBAAkB,KAC1BwD,EAAOnF,EAAMwC,KAAKkC,OAAOS,KAK3B3C,KAAKkC,OAAOS,IAJZ3C,KAAKkC,OAAO/C,iBAAiBuH,KAAKC,KACjC3G,KAAKkC,OAAO/C,iBAAiByH,SACrB5G,KAAKkC,OAAO/C,iBAAiByH,IAClC,IAEF3C,EAAQjE,KAAKkC,OAAOW,OAAOG,kBAAkBiB,MAC7CC,EAAOlE,KAAKkC,OAAOW,OAAOG,kBAAkBkB,QAC9CD,EAAMtB,GAAM,IACVuB,EAAKxF,OAAQ,KACToB,EAAQoE,EAAK2C,QAAQlE,GACvB7C,GAAS,GACXoE,EAAKE,OAAOtE,EAAO,UAGhBmE,EAAMtB,IAKrBmD,EAAKlB,MAAM5E,KAAM6E,YD9BXe,EAAY,IAAIkB,EAAOjB,IAZ7BkB,QAAQC,KACN"} \ No newline at end of file +{"version":3,"file":"index.js","sources":["../src/utils.js","../src/historyStack.js","../src/helper.js","../src/index.js","../src/extension.js"],"sourcesContent":["export const isDef = function (v) {\n return v !== undefined && v !== null;\n};\nconst PLACEHOLDER_VM = {\n __placeholder: true,\n};\nexport const resolvePushedVm = (currentVm) => {\n return (isDef(currentVm) && currentVm.$vnode.data.keepAlive) ? currentVm : PLACEHOLDER_VM;\n};\nexport const isPlaceHolderVm = (vm) => vm && !!vm.__placeholder;\n\nexport const getStateId = function () {\n const state = getCurrentState();\n return isDef(state) ? state.id : undefined;\n};\n\nexport const getQuery = function (params) {\n let query = \"\";\n query = Object.keys(params)\n .map(\n (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`\n )\n .join(\"&\");\n if (query.length > 0) {\n query = `?${query}`;\n }\n return query;\n};\nconst getCurrentState = function () {\n return history.state;\n};\n\nexport const genKey = function (num, router) {\n return `keep-alive-vnode-key${Number(num)}${router.history.current.path}`;\n};\nexport const getCurrentVM = function (router) {\n return router.history.current.matched.length > 0\n ? router.history.current.matched[0].instances.default\n : undefined;\n};\nexport const setCurrentVnodeKey = function (router, key) {\n const current = getCurrentVM(router);\n if (current && current._vnode) {\n current._vnode.parent.key = key;\n }\n};\nexport const replaceFirstKeyAndCache = function (vm, key) {\n if (!isDef(vm) || !isDef(vm.cache) || !isDef(vm.keys)) {\n return;\n }\n const keys = vm.keys;\n const cache = vm.cache;\n if (keys.length === 1) {\n const vnode = cache[keys[0]];\n delete cache[keys[0]];\n keys.splice(0, 1);\n keys.push(key);\n cache[key] = vnode;\n }\n};\nexport const getFirstComponentChild = function (children) {\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length; i++) {\n const c = children[i];\n if (isDef(c) && (isDef(c.componentOptions) || isAsyncPlaceholder(c))) {\n return c;\n }\n }\n }\n};\nconst isAsyncPlaceholder = function (node) {\n return node.isComment && node.asyncFactory;\n};\n\nexport const replaceState = function (mode, router, id) {\n const { pathname, search, hash } = window.location;\n let path = `${pathname}${search}${hash}`;\n let state = isDef(history.state) ? history.state : {};\n state[\"id\"] = id;\n // optimize file:// URL\n const isFilSys = window.location.href.startsWith(\"file://\");\n history.replaceState(state, \"\", isFilSys ? null : path);\n};\n\nexport const inBrowser = typeof window !== \"undefined\";\n","export default class HistoryStack {\n constructor() {\n this.historyStackMap = [];\n }\n push(vm, index) {\n // const cur = stackPointer();\n const stack = this.historyStackMap[index];\n if (Array.isArray(stack)) {\n !stack.includes(vm) && stack.push(vm);\n this.historyStackMap[index] = stack.filter((item) => !item._isDestroyed);\n } else {\n const vms = [];\n vms.push(vm);\n this.historyStackMap[index] = vms;\n }\n }\n pop() {\n const last = this.historyStackMap.pop();\n Array.isArray(last) &&\n last.forEach(\n (vm) => vm && vm.$keepAliveDestroy && vm.$keepAliveDestroy()\n );\n }\n removeGreater(index) {\n while (this.historyStackMap.length >= index) {\n this.pop();\n }\n }\n clear() {\n this.historyStackMap = [];\n }\n}\n","import {\n getCurrentVM,\n isDef,\n getStateId,\n resolvePushedVm,\n genKey,\n replaceFirstKeyAndCache,\n getFirstComponentChild,\n isPlaceHolderVm,\n setCurrentVnodeKey,\n replaceState,\n} from \"./utils\";\nimport HistoryStack from \"./historyStack\";\n\nexport default class VueRouterKeepAliveHelper{\n constructor({ Vue, router, replaceStay }) {\n this.Vue = Vue;\n this.router = router;\n this.router._stack = 0;\n this.mode = router.mode; // hash or history\n this.historyShouldChange = false;\n this.isReplace = false;\n this.replacePrePath = undefined;\n this.preStateId = 0;\n this.pre = null;\n this.replaceStay = replaceStay || [];\n this.hacked = false;\n this.historyStack = new HistoryStack();\n this.init();\n }\n init() {\n this.routerHooks();\n this.hackRouter();\n }\n /**\n * use afterEach hook to set state.key and add the reference of vm to the historyStack\n */\n routerHooks() {\n const router = this.router;\n router.afterEach((to, from) => {\n this.historyShouldChange = true;\n // get the vm instance after render\n this.Vue.nextTick(() => {\n const current = this.currentVm;\n const pendingToPushVm = resolvePushedVm(current);\n if (this.pre === null) {\n this.onInitial(pendingToPushVm);\n } else if (this.isReplace) {\n this.onReplace(pendingToPushVm);\n } else if (this.isPush) {\n this.onPush(pendingToPushVm);\n } else {\n this.onBack(pendingToPushVm);\n }\n this.pre = current;\n this.preStateId = this.stackPointer;\n if (!isPlaceHolderVm(pendingToPushVm)) {\n setCurrentVnodeKey(router, genKey(this.stackPointer, router));\n if (!this.hacked && current) {\n this.hackKeepAliveRender(current.$vnode.parent.componentInstance);\n }\n this.historyShouldChange = false;\n }\n });\n });\n }\n /**\n * @description hack router go , replace and push functions to tell replace from back and push\n */\n hackRouter() {\n const router = this.router;\n const rtmp = router.replace;\n const rtmpf = (location, onComplete, onAbort) => {\n this.isReplace = true;\n this.replacePrePath = router.history.current.path;\n rtmp.call(router, location, onComplete, (e) => {\n this.isReplace = false;\n this.replacePrePath = undefined;\n isDef(onAbort) && onAbort(e);\n });\n };\n router.replace = function (ocation, onComplete, onAbort) {\n rtmpf(ocation, onComplete, onAbort);\n };\n\n const gstmp = router.go;\n const gstmpf = (number) => {\n this.isReplace = false;\n return gstmp.call(router, number);\n };\n router.go = function (num) {\n return gstmpf(num);\n };\n const pstmp = router.push;\n const pstmpf = (location, onComplete, onAbort) => {\n this.isReplace = false;\n if (!onComplete && !onAbort && typeof Promise !== \"undefined\") {\n return pstmp.call(router, location, onComplete, onAbort);\n } else {\n pstmp.call(router, location, onComplete, onAbort);\n }\n };\n router.push = function (location, onComplete, onAbort) {\n return pstmpf(location, onComplete, onAbort);\n };\n }\n /**\n * @description hack the render function of keep-alive component, modify the key of vnode for cache\n * @param {*} vm keep-alive component instance\n */\n hackKeepAliveRender(vm) {\n // modify the first keep alive key and cache\n replaceFirstKeyAndCache(vm, genKey(this.stackPointer, this.router));\n\n const tmp = vm.$options.render;\n const self = this;\n const router = this.router;\n vm.$options.render = function () {\n const slot = this.$slots.default;\n const vnode = getFirstComponentChild(slot); // vnode is a keep-alive-component-vnode\n if (self.historyShouldChange) {\n if (vnode && !isDef(vnode.key)) {\n if (self.isReplace) {\n vnode.key = genKey(self.stackPointer, router);\n } else if (self.isPush) {\n vnode.key = genKey(self.stackPointer + 1, router);\n } else {\n vnode.key = genKey(self.stackPointer - 1, router);\n }\n }\n } else {\n // when historyShouldChange is false should rerender only, should not create new vm ,use the same vnode.key issue#7\n vnode.key = genKey(self.stackPointer, router);\n }\n return tmp.apply(this, arguments);\n };\n this.hacked = true;\n }\n /********** callback functions ************/\n onInitial(vm) {\n const currentStateId = getStateId();\n if (isDef(currentStateId)) {\n this.setStackPointer(currentStateId);\n } else {\n this.setState(0);\n }\n this.historyStack.push(vm, this.stackPointer);\n }\n onPush(vm) {\n this.setState(this.increaseStackPointer());\n this.historyStack.push(vm, this.stackPointer);\n }\n onBack(vm) {\n this.historyStack.pop();\n this.decreaseStackPointer();\n this.historyStack.push(vm, this.stackPointer);\n }\n onReplace(vm) {\n const shouldDestroy = !(\n isDef(this.replacePrePath) &&\n this.replaceStay.includes(this.replacePrePath)\n );\n if (shouldDestroy) {\n this.pre?.$keepAliveDestroy();\n }\n this.setState(this.stackPointer);\n this.historyStack.push(vm, this.stackPointer);\n this.isReplace = false;\n this.replacePrePath = undefined;\n }\n get currentVm() {\n return getCurrentVM(this.router);\n }\n get isPush() {\n if (!this.isReplace) {\n const stateId = getStateId();\n return !isDef(stateId) || this.preStateId <= stateId;\n }\n return false;\n }\n get stackPointer() {\n return this.router._stack;\n }\n setState(id) {\n this.setStackPointer(id);\n replaceState(this.mode, this.router, id);\n }\n setStackPointer(val) {\n this.router._stack = val;\n }\n increaseStackPointer() {\n return (this.router._stack += 1);\n }\n decreaseStackPointer() {\n return (this.router._stack -= 1);\n }\n}\n","import Helper from \"./helper\";\nimport { extendVue, extendHistory } from \"./extension\";\nimport { inBrowser } from \"./utils\";\n\nlet singleton = undefined;\n// TODO: 1. abstract mode support\nexport default function createHelper(config) {\n if (config.Vue === undefined || config.router === undefined) {\n console.warn(\n \"warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper\"\n );\n return;\n }\n if(singleton) {\n return singleton;\n }\n if (inBrowser) {\n extendHistory(window.history);\n }\n extendVue(config.Vue);\n return (singleton = new Helper(config));\n}","import { isDef } from \"./utils\";\n\nexport function extendHistory(history) {\n const rstmp = history.replaceState;\n history.replaceState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n rstmp.call(history, s, op, path);\n }\n const historyPushState = history.pushState;\n history.pushState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n historyPushState.call(history, s, op, path);\n }\n}\n\nexport function extendVue(Vue) {\n const dtmp = Vue.prototype.$destroy;\n /**\n * @description remove the cache in component before invoke $destroy\n */\n Vue.prototype.$keepAliveDestroy = function () {\n if (this.$vnode && this.$vnode.data.keepAlive) {\n if (\n this.$vnode.parent &&\n this.$vnode.parent.componentInstance &&\n this.$vnode.parent.componentInstance.cache\n ) {\n if (this.$vnode.componentOptions) {\n const key = !isDef(this.$vnode.key)\n ? this.$vnode.componentOptions.Ctor.cid +\n (this.$vnode.componentOptions.tag\n ? `::${this.$vnode.componentOptions.tag}`\n : \"\")\n : this.$vnode.key;\n const cache = this.$vnode.parent.componentInstance.cache;\n const keys = this.$vnode.parent.componentInstance.keys;\n if (cache[key]) {\n if (keys.length) {\n const index = keys.indexOf(key);\n if (index > -1) {\n keys.splice(index, 1);\n }\n }\n delete cache[key];\n }\n }\n }\n }\n dtmp.apply(this, arguments);\n };\n}\n"],"names":["isDef","v","PLACEHOLDER_VM","__placeholder","getStateId","state","getCurrentState","id","undefined","history","genKey","num","router","Number","current","path","getCurrentVM","matched","length","instances","default","getFirstComponentChild","children","Array","isArray","i","c","componentOptions","isAsyncPlaceholder","node","isComment","asyncFactory","inBrowser","window","HistoryStack","historyStackMap","push","vm","index","stack","this","includes","filter","item","_isDestroyed","vms","pop","last","forEach","$keepAliveDestroy","removeGreater","clear","VueRouterKeepAliveHelper","Vue","replaceStay","_stack","mode","historyShouldChange","isReplace","replacePrePath","preStateId","pre","hacked","historyStack","init","routerHooks","hackRouter","afterEach","to","from","_this","nextTick","currentVm","pendingToPushVm","$vnode","data","keepAlive","onInitial","onReplace","isPush","onPush","onBack","stackPointer","key","_vnode","parent","setCurrentVnodeKey","hackKeepAliveRender","componentInstance","rtmp","replace","ocation","onComplete","onAbort","location","_this2","call","e","rtmpf","gstmp","go","number","pstmp","Promise","pstmpf","cache","keys","vnode","splice","replaceFirstKeyAndCache","tmp","$options","render","self","slot","$slots","apply","arguments","currentStateId","setStackPointer","setState","increaseStackPointer","decreaseStackPointer","pathname","search","hash","isFilSys","href","startsWith","replaceState","val","stateId","singleton","config","dtmp","rstmp","op","old","Object","assign","s","historyPushState","pushState","extendHistory","prototype","$destroy","Ctor","cid","tag","indexOf","Helper","console","warn"],"mappings":"mZAAO,IAAMA,EAAQ,SAAUC,UACtBA,MAAAA,GAEHC,EAAiB,CACrBC,eAAe,GAOJC,EAAa,eAClBC,EAAQC,WACPN,EAAMK,GAASA,EAAME,QAAKC,GAe7BF,EAAkB,kBACfG,QAAQJ,OAGJK,EAAS,SAAUC,EAAKC,gCACLC,OAAOF,GAAOC,EAAOH,QAAQK,QAAQC,MAExDC,EAAe,SAAUJ,UAC7BA,EAAOH,QAAQK,QAAQG,QAAQC,OAAS,EAC3CN,EAAOH,QAAQK,QAAQG,QAAQ,GAAGE,UAAUC,aAC5CZ,GAsBOa,EAAyB,SAAUC,MAC1CC,MAAMC,QAAQF,OACX,IAAIG,EAAI,EAAGA,EAAIH,EAASJ,OAAQO,IAAK,KAClCC,EAAIJ,EAASG,MACfzB,EAAM0B,KAAO1B,EAAM0B,EAAEC,mBAAqBC,EAAmBF,WACxDA,IAKTE,EAAqB,SAAUC,UAC5BA,EAAKC,WAAaD,EAAKE,cAanBC,EAA8B,oBAAXC,OCpFXC,+BAEZC,gBAAkB,8BAEzBC,KAAA,SAAKC,EAAIC,OAEDC,EAAQC,KAAKL,gBAAgBG,MAC/Bf,MAAMC,QAAQe,IACfA,EAAME,SAASJ,IAAOE,EAAMH,KAAKC,QAC7BF,gBAAgBG,GAASC,EAAMG,QAAO,SAACC,UAAUA,EAAKC,oBACtD,KACCC,EAAM,GACZA,EAAIT,KAAKC,QACJF,gBAAgBG,GAASO,MAGlCC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EAAMwC,KAAKkC,OAAOS,KAK3B3C,KAAKkC,OAAOS,IAJZ3C,KAAKkC,OAAO/C,iBAAiBuH,KAAKC,KACjC3G,KAAKkC,OAAO/C,iBAAiByH,SACrB5G,KAAKkC,OAAO/C,iBAAiByH,IAClC,IAEF3C,EAAQjE,KAAKkC,OAAOW,OAAOG,kBAAkBiB,MAC7CC,EAAOlE,KAAKkC,OAAOW,OAAOG,kBAAkBkB,QAC9CD,EAAMtB,GAAM,IACVuB,EAAKxF,OAAQ,KACToB,EAAQoE,EAAK2C,QAAQlE,GACvB7C,GAAS,GACXoE,EAAKE,OAAOtE,EAAO,UAGhBmE,EAAMtB,IAKrBmD,EAAKlB,MAAM5E,KAAM6E,YD9BXe,EAAY,IAAIkB,EAAOjB,IAZ7BkB,QAAQC,KACN"} \ No newline at end of file From f1beedebf2b184f0635c7e593ecb1d13a7f19af5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=91=AB=E5=87=AF?= Date: Wed, 29 Sep 2021 11:04:16 +0800 Subject: [PATCH 5/6] fix: #23 this.pre is undefined cause error --- src/helper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helper.js b/src/helper.js index f145731..12a1c94 100644 --- a/src/helper.js +++ b/src/helper.js @@ -161,7 +161,7 @@ export default class VueRouterKeepAliveHelper{ this.replaceStay.includes(this.replacePrePath) ); if (shouldDestroy) { - this.pre?.$keepAliveDestroy(); + this.pre?.$keepAliveDestroy?.(); } this.setState(this.stackPointer); this.historyStack.push(vm, this.stackPointer); From 1586a484b7954cb7d28963a630b4b53c37e621a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=91=AB=E5=87=AF?= Date: Wed, 29 Sep 2021 11:04:47 +0800 Subject: [PATCH 6/6] chore:build for test fix issue 23 --- dist/helper.esm.js | 2 +- dist/helper.esm.js.map | 2 +- dist/index.js | 2 +- dist/index.js.map | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/helper.esm.js b/dist/helper.esm.js index 1f32776..163e559 100644 --- a/dist/helper.esm.js +++ b/dist/helper.esm.js @@ -1,2 +1,2 @@ -function t(t,e){for(var n=0;n0?t.history.current.matched[0].instances.default:void 0},s=function(t){if(Array.isArray(t))for(var n=0;n=t;)this.pop()},e.clear=function(){this.historyStackMap=[]},t}(),p=function(){function i(t){var e=t.Vue,n=t.router,r=t.replaceStay;this.Vue=e,this.router=n,this.router._stack=0,this.mode=n.mode,this.historyShouldChange=!1,this.isReplace=!1,this.replacePrePath=void 0,this.preStateId=0,this.pre=null,this.replaceStay=r||[],this.hacked=!1,this.historyStack=new u,this.init()}var c,h,p,l=i.prototype;return l.init=function(){this.routerHooks(),this.hackRouter()},l.routerHooks=function(){var t=this,r=this.router;r.afterEach((function(i,s){t.historyShouldChange=!0,t.Vue.nextTick((function(){var i,s,c=t.currentVm,h=e(i=c)&&i.$vnode.data.keepAlive?i:n;null===t.pre?t.onInitial(h):t.isReplace?t.onReplace(h):t.isPush?t.onPush(h):t.onBack(h),t.pre=c,t.preStateId=t.stackPointer,(s=h)&&s.__placeholder||(!function(t,e){var n=a(t);n&&n._vnode&&(n._vnode.parent.key=e)}(r,o(t.stackPointer,r)),!t.hacked&&c&&t.hackKeepAliveRender(c.$vnode.parent.componentInstance),t.historyShouldChange=!1)}))}))},l.hackRouter=function(){var t=this,n=this.router,r=n.replace;n.replace=function(i,o,a){!function(i,o,a){t.isReplace=!0,t.replacePrePath=n.history.current.path,r.call(n,i,o,(function(n){t.isReplace=!1,t.replacePrePath=void 0,e(a)&&a(n)}))}(i,o,a)};var i=n.go;n.go=function(e){return r=e,t.isReplace=!1,i.call(n,r);var r};var o=n.push;n.push=function(e,r,i){return function(e,r,i){if(t.isReplace=!1,!r&&!i&&"undefined"!=typeof Promise)return o.call(n,e,r,i);o.call(n,e,r,i)}(e,r,i)}},l.hackKeepAliveRender=function(t){!function(t,n){if(e(t)&&e(t.cache)&&e(t.keys)){var r=t.keys,i=t.cache;if(1===r.length){var o=i[r[0]];delete i[r[0]],r.splice(0,1),r.push(n),i[n]=o}}}(t,o(this.stackPointer,this.router));var n=t.$options.render,r=this,i=this.router;t.$options.render=function(){var t=this.$slots.default,a=s(t);return r.historyShouldChange?a&&!e(a.key)&&(r.isReplace?a.key=o(r.stackPointer,i):r.isPush?a.key=o(r.stackPointer+1,i):a.key=o(r.stackPointer-1,i)):a.key=o(r.stackPointer,i),n.apply(this,arguments)},this.hacked=!0},l.onInitial=function(t){var n=r();e(n)?this.setStackPointer(n):this.setState(0),this.historyStack.push(t,this.stackPointer)},l.onPush=function(t){this.setState(this.increaseStackPointer()),this.historyStack.push(t,this.stackPointer)},l.onBack=function(t){this.historyStack.pop(),this.decreaseStackPointer(),this.historyStack.push(t,this.stackPointer)},l.onReplace=function(t){var n;e(this.replacePrePath)&&this.replaceStay.includes(this.replacePrePath)||(null==(n=this.pre)||n.$keepAliveDestroy());this.setState(this.stackPointer),this.historyStack.push(t,this.stackPointer),this.isReplace=!1,this.replacePrePath=void 0},l.setState=function(t){this.setStackPointer(t),function(t,n,r){var i=window.location,o=""+i.pathname+i.search+i.hash,a=e(history.state)?history.state:{};a.id=r;var s=window.location.href.startsWith("file://");history.replaceState(a,"",s?null:o)}(this.mode,this.router,t)},l.setStackPointer=function(t){this.router._stack=t},l.increaseStackPointer=function(){return this.router._stack+=1},l.decreaseStackPointer=function(){return this.router._stack-=1},c=i,(h=[{key:"currentVm",get:function(){return a(this.router)}},{key:"isPush",get:function(){if(!this.isReplace){var t=r();return!e(t)||this.preStateId<=t}return!1}},{key:"stackPointer",get:function(){return this.router._stack}}])&&t(c.prototype,h),p&&t(c,p),i}();var l=void 0;function f(t){var n,r;if(void 0!==t.Vue&&void 0!==t.router)return l||(h&&function(t){var e=t.replaceState;t.replaceState=function(n,r,i){var o=Object.assign({},t.state),a=Object.assign(o,n);e.call(t,a,r,i)};var n=t.pushState;t.pushState=function(e,r,i){var o=Object.assign({},t.state),a=Object.assign(o,e);n.call(t,a,r,i)}}(window.history),n=t.Vue,r=n.prototype.$destroy,n.prototype.$keepAliveDestroy=function(){if(this.$vnode&&this.$vnode.data.keepAlive&&this.$vnode.parent&&this.$vnode.parent.componentInstance&&this.$vnode.parent.componentInstance.cache&&this.$vnode.componentOptions){var t=e(this.$vnode.key)?this.$vnode.key:this.$vnode.componentOptions.Ctor.cid+(this.$vnode.componentOptions.tag?"::"+this.$vnode.componentOptions.tag:""),n=this.$vnode.parent.componentInstance.cache,i=this.$vnode.parent.componentInstance.keys;if(n[t]){if(i.length){var o=i.indexOf(t);o>-1&&i.splice(o,1)}delete n[t]}}r.apply(this,arguments)},l=new p(t));console.warn("warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper")}export default f; +function t(t,e){for(var n=0;n0?t.history.current.matched[0].instances.default:void 0},s=function(t){if(Array.isArray(t))for(var n=0;n=t;)this.pop()},e.clear=function(){this.historyStackMap=[]},t}(),p=function(){function i(t){var e=t.Vue,n=t.router,r=t.replaceStay;this.Vue=e,this.router=n,this.router._stack=0,this.mode=n.mode,this.historyShouldChange=!1,this.isReplace=!1,this.replacePrePath=void 0,this.preStateId=0,this.pre=null,this.replaceStay=r||[],this.hacked=!1,this.historyStack=new u,this.init()}var c,h,p,l=i.prototype;return l.init=function(){this.routerHooks(),this.hackRouter()},l.routerHooks=function(){var t=this,r=this.router;r.afterEach((function(i,s){t.historyShouldChange=!0,t.Vue.nextTick((function(){var i,s,c=t.currentVm,h=e(i=c)&&i.$vnode.data.keepAlive?i:n;null===t.pre?t.onInitial(h):t.isReplace?t.onReplace(h):t.isPush?t.onPush(h):t.onBack(h),t.pre=c,t.preStateId=t.stackPointer,(s=h)&&s.__placeholder||(!function(t,e){var n=a(t);n&&n._vnode&&(n._vnode.parent.key=e)}(r,o(t.stackPointer,r)),!t.hacked&&c&&t.hackKeepAliveRender(c.$vnode.parent.componentInstance),t.historyShouldChange=!1)}))}))},l.hackRouter=function(){var t=this,n=this.router,r=n.replace;n.replace=function(i,o,a){!function(i,o,a){t.isReplace=!0,t.replacePrePath=n.history.current.path,r.call(n,i,o,(function(n){t.isReplace=!1,t.replacePrePath=void 0,e(a)&&a(n)}))}(i,o,a)};var i=n.go;n.go=function(e){return r=e,t.isReplace=!1,i.call(n,r);var r};var o=n.push;n.push=function(e,r,i){return function(e,r,i){if(t.isReplace=!1,!r&&!i&&"undefined"!=typeof Promise)return o.call(n,e,r,i);o.call(n,e,r,i)}(e,r,i)}},l.hackKeepAliveRender=function(t){!function(t,n){if(e(t)&&e(t.cache)&&e(t.keys)){var r=t.keys,i=t.cache;if(1===r.length){var o=i[r[0]];delete i[r[0]],r.splice(0,1),r.push(n),i[n]=o}}}(t,o(this.stackPointer,this.router));var n=t.$options.render,r=this,i=this.router;t.$options.render=function(){var t=this.$slots.default,a=s(t);return r.historyShouldChange?a&&!e(a.key)&&(r.isReplace?a.key=o(r.stackPointer,i):r.isPush?a.key=o(r.stackPointer+1,i):a.key=o(r.stackPointer-1,i)):a.key=o(r.stackPointer,i),n.apply(this,arguments)},this.hacked=!0},l.onInitial=function(t){var n=r();e(n)?this.setStackPointer(n):this.setState(0),this.historyStack.push(t,this.stackPointer)},l.onPush=function(t){this.setState(this.increaseStackPointer()),this.historyStack.push(t,this.stackPointer)},l.onBack=function(t){this.historyStack.pop(),this.decreaseStackPointer(),this.historyStack.push(t,this.stackPointer)},l.onReplace=function(t){var n;e(this.replacePrePath)&&this.replaceStay.includes(this.replacePrePath)||(null==(n=this.pre)||null==n.$keepAliveDestroy||n.$keepAliveDestroy());this.setState(this.stackPointer),this.historyStack.push(t,this.stackPointer),this.isReplace=!1,this.replacePrePath=void 0},l.setState=function(t){this.setStackPointer(t),function(t,n,r){var i=window.location,o=""+i.pathname+i.search+i.hash,a=e(history.state)?history.state:{};a.id=r;var s=window.location.href.startsWith("file://");history.replaceState(a,"",s?null:o)}(this.mode,this.router,t)},l.setStackPointer=function(t){this.router._stack=t},l.increaseStackPointer=function(){return this.router._stack+=1},l.decreaseStackPointer=function(){return this.router._stack-=1},c=i,(h=[{key:"currentVm",get:function(){return a(this.router)}},{key:"isPush",get:function(){if(!this.isReplace){var t=r();return!e(t)||this.preStateId<=t}return!1}},{key:"stackPointer",get:function(){return this.router._stack}}])&&t(c.prototype,h),p&&t(c,p),i}();var l=void 0;function f(t){var n,r;if(void 0!==t.Vue&&void 0!==t.router)return l||(h&&function(t){var e=t.replaceState;t.replaceState=function(n,r,i){var o=Object.assign({},t.state),a=Object.assign(o,n);e.call(t,a,r,i)};var n=t.pushState;t.pushState=function(e,r,i){var o=Object.assign({},t.state),a=Object.assign(o,e);n.call(t,a,r,i)}}(window.history),n=t.Vue,r=n.prototype.$destroy,n.prototype.$keepAliveDestroy=function(){if(this.$vnode&&this.$vnode.data.keepAlive&&this.$vnode.parent&&this.$vnode.parent.componentInstance&&this.$vnode.parent.componentInstance.cache&&this.$vnode.componentOptions){var t=e(this.$vnode.key)?this.$vnode.key:this.$vnode.componentOptions.Ctor.cid+(this.$vnode.componentOptions.tag?"::"+this.$vnode.componentOptions.tag:""),n=this.$vnode.parent.componentInstance.cache,i=this.$vnode.parent.componentInstance.keys;if(n[t]){if(i.length){var o=i.indexOf(t);o>-1&&i.splice(o,1)}delete n[t]}}r.apply(this,arguments)},l=new p(t));console.warn("warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper")}export default f; //# sourceMappingURL=helper.esm.js.map diff --git a/dist/helper.esm.js.map b/dist/helper.esm.js.map index 6a03c08..f7243b2 100644 --- a/dist/helper.esm.js.map +++ b/dist/helper.esm.js.map @@ -1 +1 @@ -{"version":3,"file":"helper.esm.js","sources":["../src/utils.js","../src/historyStack.js","../src/helper.js","../src/index.js","../src/extension.js"],"sourcesContent":["export const isDef = function (v) {\n return v !== undefined && v !== null;\n};\nconst PLACEHOLDER_VM = {\n __placeholder: true,\n};\nexport const resolvePushedVm = (currentVm) => {\n return (isDef(currentVm) && currentVm.$vnode.data.keepAlive) ? currentVm : PLACEHOLDER_VM;\n};\nexport const isPlaceHolderVm = (vm) => vm && !!vm.__placeholder;\n\nexport const getStateId = function () {\n const state = getCurrentState();\n return isDef(state) ? state.id : undefined;\n};\n\nexport const getQuery = function (params) {\n let query = \"\";\n query = Object.keys(params)\n .map(\n (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`\n )\n .join(\"&\");\n if (query.length > 0) {\n query = `?${query}`;\n }\n return query;\n};\nconst getCurrentState = function () {\n return history.state;\n};\n\nexport const genKey = function (num, router) {\n return `keep-alive-vnode-key${Number(num)}${router.history.current.path}`;\n};\nexport const getCurrentVM = function (router) {\n return router.history.current.matched.length > 0\n ? router.history.current.matched[0].instances.default\n : undefined;\n};\nexport const setCurrentVnodeKey = function (router, key) {\n const current = getCurrentVM(router);\n if (current && current._vnode) {\n current._vnode.parent.key = key;\n }\n};\nexport const replaceFirstKeyAndCache = function (vm, key) {\n if (!isDef(vm) || !isDef(vm.cache) || !isDef(vm.keys)) {\n return;\n }\n const keys = vm.keys;\n const cache = vm.cache;\n if (keys.length === 1) {\n const vnode = cache[keys[0]];\n delete cache[keys[0]];\n keys.splice(0, 1);\n keys.push(key);\n cache[key] = vnode;\n }\n};\nexport const getFirstComponentChild = function (children) {\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length; i++) {\n const c = children[i];\n if (isDef(c) && (isDef(c.componentOptions) || isAsyncPlaceholder(c))) {\n return c;\n }\n }\n }\n};\nconst isAsyncPlaceholder = function (node) {\n return node.isComment && node.asyncFactory;\n};\n\nexport const replaceState = function (mode, router, id) {\n const { pathname, search, hash } = window.location;\n let path = `${pathname}${search}${hash}`;\n let state = isDef(history.state) ? history.state : {};\n state[\"id\"] = id;\n // optimize file:// URL\n const isFilSys = window.location.href.startsWith(\"file://\");\n history.replaceState(state, \"\", isFilSys ? null : path);\n};\n\nexport const inBrowser = typeof window !== \"undefined\";\n","export default class HistoryStack {\n constructor() {\n this.historyStackMap = [];\n }\n push(vm, index) {\n // const cur = stackPointer();\n const stack = this.historyStackMap[index];\n if (Array.isArray(stack)) {\n !stack.includes(vm) && stack.push(vm);\n this.historyStackMap[index] = stack.filter((item) => !item._isDestroyed);\n } else {\n const vms = [];\n vms.push(vm);\n this.historyStackMap[index] = vms;\n }\n }\n pop() {\n const last = this.historyStackMap.pop();\n Array.isArray(last) &&\n last.forEach(\n (vm) => vm && vm.$keepAliveDestroy && vm.$keepAliveDestroy()\n );\n }\n removeGreater(index) {\n while (this.historyStackMap.length >= index) {\n this.pop();\n }\n }\n clear() {\n this.historyStackMap = [];\n }\n}\n","import {\n getCurrentVM,\n isDef,\n getStateId,\n resolvePushedVm,\n genKey,\n replaceFirstKeyAndCache,\n getFirstComponentChild,\n isPlaceHolderVm,\n setCurrentVnodeKey,\n replaceState,\n} from \"./utils\";\nimport HistoryStack from \"./historyStack\";\n\nexport default class VueRouterKeepAliveHelper{\n constructor({ Vue, router, replaceStay }) {\n this.Vue = Vue;\n this.router = router;\n this.router._stack = 0;\n this.mode = router.mode; // hash or history\n this.historyShouldChange = false;\n this.isReplace = false;\n this.replacePrePath = undefined;\n this.preStateId = 0;\n this.pre = null;\n this.replaceStay = replaceStay || [];\n this.hacked = false;\n this.historyStack = new HistoryStack();\n this.init();\n }\n init() {\n this.routerHooks();\n this.hackRouter();\n }\n /**\n * use afterEach hook to set state.key and add the reference of vm to the historyStack\n */\n routerHooks() {\n const router = this.router;\n router.afterEach((to, from) => {\n this.historyShouldChange = true;\n // get the vm instance after render\n this.Vue.nextTick(() => {\n const current = this.currentVm;\n const pendingToPushVm = resolvePushedVm(current);\n if (this.pre === null) {\n this.onInitial(pendingToPushVm);\n } else if (this.isReplace) {\n this.onReplace(pendingToPushVm);\n } else if (this.isPush) {\n this.onPush(pendingToPushVm);\n } else {\n this.onBack(pendingToPushVm);\n }\n this.pre = current;\n this.preStateId = this.stackPointer;\n if (!isPlaceHolderVm(pendingToPushVm)) {\n setCurrentVnodeKey(router, genKey(this.stackPointer, router));\n if (!this.hacked && current) {\n this.hackKeepAliveRender(current.$vnode.parent.componentInstance);\n }\n this.historyShouldChange = false;\n }\n });\n });\n }\n /**\n * @description hack router go , replace and push functions to tell replace from back and push\n */\n hackRouter() {\n const router = this.router;\n const rtmp = router.replace;\n const rtmpf = (location, onComplete, onAbort) => {\n this.isReplace = true;\n this.replacePrePath = router.history.current.path;\n rtmp.call(router, location, onComplete, (e) => {\n this.isReplace = false;\n this.replacePrePath = undefined;\n isDef(onAbort) && onAbort(e);\n });\n };\n router.replace = function (ocation, onComplete, onAbort) {\n rtmpf(ocation, onComplete, onAbort);\n };\n\n const gstmp = router.go;\n const gstmpf = (number) => {\n this.isReplace = false;\n return gstmp.call(router, number);\n };\n router.go = function (num) {\n return gstmpf(num);\n };\n const pstmp = router.push;\n const pstmpf = (location, onComplete, onAbort) => {\n this.isReplace = false;\n if (!onComplete && !onAbort && typeof Promise !== \"undefined\") {\n return pstmp.call(router, location, onComplete, onAbort);\n } else {\n pstmp.call(router, location, onComplete, onAbort);\n }\n };\n router.push = function (location, onComplete, onAbort) {\n return pstmpf(location, onComplete, onAbort);\n };\n }\n /**\n * @description hack the render function of keep-alive component, modify the key of vnode for cache\n * @param {*} vm keep-alive component instance\n */\n hackKeepAliveRender(vm) {\n // modify the first keep alive key and cache\n replaceFirstKeyAndCache(vm, genKey(this.stackPointer, this.router));\n\n const tmp = vm.$options.render;\n const self = this;\n const router = this.router;\n vm.$options.render = function () {\n const slot = this.$slots.default;\n const vnode = getFirstComponentChild(slot); // vnode is a keep-alive-component-vnode\n if (self.historyShouldChange) {\n if (vnode && !isDef(vnode.key)) {\n if (self.isReplace) {\n vnode.key = genKey(self.stackPointer, router);\n } else if (self.isPush) {\n vnode.key = genKey(self.stackPointer + 1, router);\n } else {\n vnode.key = genKey(self.stackPointer - 1, router);\n }\n }\n } else {\n // when historyShouldChange is false should rerender only, should not create new vm ,use the same vnode.key issue#7\n vnode.key = genKey(self.stackPointer, router);\n }\n return tmp.apply(this, arguments);\n };\n this.hacked = true;\n }\n /********** callback functions ************/\n onInitial(vm) {\n const currentStateId = getStateId();\n if (isDef(currentStateId)) {\n this.setStackPointer(currentStateId);\n } else {\n this.setState(0);\n }\n this.historyStack.push(vm, this.stackPointer);\n }\n onPush(vm) {\n this.setState(this.increaseStackPointer());\n this.historyStack.push(vm, this.stackPointer);\n }\n onBack(vm) {\n this.historyStack.pop();\n this.decreaseStackPointer();\n this.historyStack.push(vm, this.stackPointer);\n }\n onReplace(vm) {\n const shouldDestroy = !(\n isDef(this.replacePrePath) &&\n this.replaceStay.includes(this.replacePrePath)\n );\n if (shouldDestroy) {\n this.pre?.$keepAliveDestroy();\n }\n this.setState(this.stackPointer);\n this.historyStack.push(vm, this.stackPointer);\n this.isReplace = false;\n this.replacePrePath = undefined;\n }\n get currentVm() {\n return getCurrentVM(this.router);\n }\n get isPush() {\n if (!this.isReplace) {\n const stateId = getStateId();\n return !isDef(stateId) || this.preStateId <= stateId;\n }\n return false;\n }\n get stackPointer() {\n return this.router._stack;\n }\n setState(id) {\n this.setStackPointer(id);\n replaceState(this.mode, this.router, id);\n }\n setStackPointer(val) {\n this.router._stack = val;\n }\n increaseStackPointer() {\n return (this.router._stack += 1);\n }\n decreaseStackPointer() {\n return (this.router._stack -= 1);\n }\n}\n","import Helper from \"./helper\";\nimport { extendVue, extendHistory } from \"./extension\";\nimport { inBrowser } from \"./utils\";\n\nlet singleton = undefined;\n// TODO: 1. abstract mode support\nexport default function createHelper(config) {\n if (config.Vue === undefined || config.router === undefined) {\n console.warn(\n \"warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper\"\n );\n return;\n }\n if(singleton) {\n return singleton;\n }\n if (inBrowser) {\n extendHistory(window.history);\n }\n extendVue(config.Vue);\n return (singleton = new Helper(config));\n}","import { isDef } from \"./utils\";\n\nexport function extendHistory(history) {\n const rstmp = history.replaceState;\n history.replaceState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n rstmp.call(history, s, op, path);\n }\n const historyPushState = history.pushState;\n history.pushState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n historyPushState.call(history, s, op, path);\n }\n}\n\nexport function extendVue(Vue) {\n const dtmp = Vue.prototype.$destroy;\n /**\n * @description remove the cache in component before invoke $destroy\n */\n Vue.prototype.$keepAliveDestroy = function () {\n if (this.$vnode && this.$vnode.data.keepAlive) {\n if (\n this.$vnode.parent &&\n this.$vnode.parent.componentInstance &&\n this.$vnode.parent.componentInstance.cache\n ) {\n if (this.$vnode.componentOptions) {\n const key = !isDef(this.$vnode.key)\n ? this.$vnode.componentOptions.Ctor.cid +\n (this.$vnode.componentOptions.tag\n ? `::${this.$vnode.componentOptions.tag}`\n : \"\")\n : this.$vnode.key;\n const cache = this.$vnode.parent.componentInstance.cache;\n const keys = this.$vnode.parent.componentInstance.keys;\n if (cache[key]) {\n if (keys.length) {\n const index = keys.indexOf(key);\n if (index > -1) {\n keys.splice(index, 1);\n }\n }\n delete cache[key];\n }\n }\n }\n }\n dtmp.apply(this, arguments);\n };\n}\n"],"names":["isDef","v","PLACEHOLDER_VM","__placeholder","getStateId","state","getCurrentState","id","undefined","history","genKey","num","router","Number","current","path","getCurrentVM","matched","length","instances","default","getFirstComponentChild","children","Array","isArray","i","c","componentOptions","isAsyncPlaceholder","node","isComment","asyncFactory","inBrowser","window","HistoryStack","historyStackMap","push","vm","index","stack","this","includes","filter","item","_isDestroyed","vms","pop","last","forEach","$keepAliveDestroy","removeGreater","clear","VueRouterKeepAliveHelper","Vue","replaceStay","_stack","mode","historyShouldChange","isReplace","replacePrePath","preStateId","pre","hacked","historyStack","init","routerHooks","hackRouter","afterEach","to","from","_this","nextTick","currentVm","pendingToPushVm","$vnode","data","keepAlive","onInitial","onReplace","isPush","onPush","onBack","stackPointer","key","_vnode","parent","setCurrentVnodeKey","hackKeepAliveRender","componentInstance","rtmp","replace","ocation","onComplete","onAbort","location","_this2","call","e","rtmpf","gstmp","go","number","pstmp","Promise","pstmpf","cache","keys","vnode","splice","replaceFirstKeyAndCache","tmp","$options","render","self","slot","$slots","apply","arguments","currentStateId","setStackPointer","setState","increaseStackPointer","decreaseStackPointer","pathname","search","hash","isFilSys","href","startsWith","replaceState","val","stateId","singleton","createHelper","config","dtmp","rstmp","op","old","Object","assign","s","historyPushState","pushState","extendHistory","prototype","$destroy","Ctor","cid","tag","indexOf","Helper","console","warn"],"mappings":"sKAAO,IAAMA,EAAQ,SAAUC,UACtBA,MAAAA,GAEHC,EAAiB,CACrBC,eAAe,GAOJC,EAAa,eAClBC,EAAQC,WACPN,EAAMK,GAASA,EAAME,QAAKC,GAe7BF,EAAkB,kBACfG,QAAQJ,OAGJK,EAAS,SAAUC,EAAKC,gCACLC,OAAOF,GAAOC,EAAOH,QAAQK,QAAQC,MAExDC,EAAe,SAAUJ,UAC7BA,EAAOH,QAAQK,QAAQG,QAAQC,OAAS,EAC3CN,EAAOH,QAAQK,QAAQG,QAAQ,GAAGE,UAAUC,aAC5CZ,GAsBOa,EAAyB,SAAUC,MAC1CC,MAAMC,QAAQF,OACX,IAAIG,EAAI,EAAGA,EAAIH,EAASJ,OAAQO,IAAK,KAClCC,EAAIJ,EAASG,MACfzB,EAAM0B,KAAO1B,EAAM0B,EAAEC,mBAAqBC,EAAmBF,WACxDA,IAKTE,EAAqB,SAAUC,UAC5BA,EAAKC,WAAaD,EAAKE,cAanBC,EAA8B,oBAAXC,OCpFXC,+BAEZC,gBAAkB,8BAEzBC,KAAA,SAAKC,EAAIC,OAEDC,EAAQC,KAAKL,gBAAgBG,MAC/Bf,MAAMC,QAAQe,IACfA,EAAME,SAASJ,IAAOE,EAAMH,KAAKC,QAC7BF,gBAAgBG,GAASC,EAAMG,QAAO,SAACC,UAAUA,EAAKC,oBACtD,KACCC,EAAM,GACZA,EAAIT,KAAKC,QACJF,gBAAgBG,GAASO,MAGlCC,IAAA,eACQC,EAAOP,KAAKL,gBAAg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3C,KAAKkC,OAAOS,IAJZ3C,KAAKkC,OAAO/C,iBAAiBwH,KAAKC,KACjC5G,KAAKkC,OAAO/C,iBAAiB0H,SACrB7G,KAAKkC,OAAO/C,iBAAiB0H,IAClC,IAEF5C,EAAQjE,KAAKkC,OAAOW,OAAOG,kBAAkBiB,MAC7CC,EAAOlE,KAAKkC,OAAOW,OAAOG,kBAAkBkB,QAC9CD,EAAMtB,GAAM,IACVuB,EAAKxF,OAAQ,KACToB,EAAQoE,EAAK4C,QAAQnE,GACvB7C,GAAS,GACXoE,EAAKE,OAAOtE,EAAO,UAGhBmE,EAAMtB,IAKrBoD,EAAKnB,MAAM5E,KAAM6E,YD9BXe,EAAY,IAAImB,EAAOjB,IAZ7BkB,QAAQC,KACN"} \ No newline at end of file +{"version":3,"file":"helper.esm.js","sources":["../src/utils.js","../src/historyStack.js","../src/helper.js","../src/index.js","../src/extension.js"],"sourcesContent":["export const isDef = function (v) {\n return v !== undefined && v !== null;\n};\nconst PLACEHOLDER_VM = {\n __placeholder: true,\n};\nexport const resolvePushedVm = (currentVm) => {\n return (isDef(currentVm) && currentVm.$vnode.data.keepAlive) ? currentVm : PLACEHOLDER_VM;\n};\nexport const isPlaceHolderVm = (vm) => vm && !!vm.__placeholder;\n\nexport const getStateId = function () {\n const state = getCurrentState();\n return isDef(state) ? state.id : undefined;\n};\n\nexport const getQuery = function (params) {\n let query = \"\";\n query = Object.keys(params)\n .map(\n (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`\n )\n .join(\"&\");\n if (query.length > 0) {\n query = `?${query}`;\n }\n return query;\n};\nconst getCurrentState = function () {\n return history.state;\n};\n\nexport const genKey = function (num, router) {\n return `keep-alive-vnode-key${Number(num)}${router.history.current.path}`;\n};\nexport const getCurrentVM = function (router) {\n return router.history.current.matched.length > 0\n ? router.history.current.matched[0].instances.default\n : undefined;\n};\nexport const setCurrentVnodeKey = function (router, key) {\n const current = getCurrentVM(router);\n if (current && current._vnode) {\n current._vnode.parent.key = key;\n }\n};\nexport const replaceFirstKeyAndCache = function (vm, key) {\n if (!isDef(vm) || !isDef(vm.cache) || !isDef(vm.keys)) {\n return;\n }\n const keys = vm.keys;\n const cache = vm.cache;\n if (keys.length === 1) {\n const vnode = cache[keys[0]];\n delete cache[keys[0]];\n keys.splice(0, 1);\n keys.push(key);\n cache[key] = vnode;\n }\n};\nexport const getFirstComponentChild = function (children) {\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length; i++) {\n const c = children[i];\n if (isDef(c) && (isDef(c.componentOptions) || isAsyncPlaceholder(c))) {\n return c;\n }\n }\n }\n};\nconst isAsyncPlaceholder = function (node) {\n return node.isComment && node.asyncFactory;\n};\n\nexport const replaceState = function (mode, router, id) {\n const { pathname, search, hash } = window.location;\n let path = `${pathname}${search}${hash}`;\n let state = isDef(history.state) ? history.state : {};\n state[\"id\"] = id;\n // optimize file:// URL\n const isFilSys = window.location.href.startsWith(\"file://\");\n history.replaceState(state, \"\", isFilSys ? null : path);\n};\n\nexport const inBrowser = typeof window !== \"undefined\";\n","export default class HistoryStack {\n constructor() {\n this.historyStackMap = [];\n }\n push(vm, index) {\n // const cur = stackPointer();\n const stack = this.historyStackMap[index];\n if (Array.isArray(stack)) {\n !stack.includes(vm) && stack.push(vm);\n this.historyStackMap[index] = stack.filter((item) => !item._isDestroyed);\n } else {\n const vms = [];\n vms.push(vm);\n this.historyStackMap[index] = vms;\n }\n }\n pop() {\n const last = this.historyStackMap.pop();\n Array.isArray(last) &&\n last.forEach(\n (vm) => vm && vm.$keepAliveDestroy && vm.$keepAliveDestroy()\n );\n }\n removeGreater(index) {\n while (this.historyStackMap.length >= index) {\n this.pop();\n }\n }\n clear() {\n this.historyStackMap = [];\n }\n}\n","import {\n getCurrentVM,\n isDef,\n getStateId,\n resolvePushedVm,\n genKey,\n replaceFirstKeyAndCache,\n getFirstComponentChild,\n isPlaceHolderVm,\n setCurrentVnodeKey,\n replaceState,\n} from \"./utils\";\nimport HistoryStack from \"./historyStack\";\n\nexport default class VueRouterKeepAliveHelper{\n constructor({ Vue, router, replaceStay }) {\n this.Vue = Vue;\n this.router = router;\n this.router._stack = 0;\n this.mode = router.mode; // hash or history\n this.historyShouldChange = false;\n this.isReplace = false;\n this.replacePrePath = undefined;\n this.preStateId = 0;\n this.pre = null;\n this.replaceStay = replaceStay || [];\n this.hacked = false;\n this.historyStack = new HistoryStack();\n this.init();\n }\n init() {\n this.routerHooks();\n this.hackRouter();\n }\n /**\n * use afterEach hook to set state.key and add the reference of vm to the historyStack\n */\n routerHooks() {\n const router = this.router;\n router.afterEach((to, from) => {\n this.historyShouldChange = true;\n // get the vm instance after render\n this.Vue.nextTick(() => {\n const current = this.currentVm;\n const pendingToPushVm = resolvePushedVm(current);\n if (this.pre === null) {\n this.onInitial(pendingToPushVm);\n } else if (this.isReplace) {\n this.onReplace(pendingToPushVm);\n } else if (this.isPush) {\n this.onPush(pendingToPushVm);\n } else {\n this.onBack(pendingToPushVm);\n }\n this.pre = current;\n this.preStateId = this.stackPointer;\n if (!isPlaceHolderVm(pendingToPushVm)) {\n setCurrentVnodeKey(router, genKey(this.stackPointer, router));\n if (!this.hacked && current) {\n this.hackKeepAliveRender(current.$vnode.parent.componentInstance);\n }\n this.historyShouldChange = false;\n }\n });\n });\n }\n /**\n * @description hack router go , replace and push functions to tell replace from back and push\n */\n hackRouter() {\n const router = this.router;\n const rtmp = router.replace;\n const rtmpf = (location, onComplete, onAbort) => {\n this.isReplace = true;\n this.replacePrePath = router.history.current.path;\n rtmp.call(router, location, onComplete, (e) => {\n this.isReplace = false;\n this.replacePrePath = undefined;\n isDef(onAbort) && onAbort(e);\n });\n };\n router.replace = function (ocation, onComplete, onAbort) {\n rtmpf(ocation, onComplete, onAbort);\n };\n\n const gstmp = router.go;\n const gstmpf = (number) => {\n this.isReplace = false;\n return gstmp.call(router, number);\n };\n router.go = function (num) {\n return gstmpf(num);\n };\n const pstmp = router.push;\n const pstmpf = (location, onComplete, onAbort) => {\n this.isReplace = false;\n if (!onComplete && !onAbort && typeof Promise !== \"undefined\") {\n return pstmp.call(router, location, onComplete, onAbort);\n } else {\n pstmp.call(router, location, onComplete, onAbort);\n }\n };\n router.push = function (location, onComplete, onAbort) {\n return pstmpf(location, onComplete, onAbort);\n };\n }\n /**\n * @description hack the render function of keep-alive component, modify the key of vnode for cache\n * @param {*} vm keep-alive component instance\n */\n hackKeepAliveRender(vm) {\n // modify the first keep alive key and cache\n replaceFirstKeyAndCache(vm, genKey(this.stackPointer, this.router));\n\n const tmp = vm.$options.render;\n const self = this;\n const router = this.router;\n vm.$options.render = function () {\n const slot = this.$slots.default;\n const vnode = getFirstComponentChild(slot); // vnode is a keep-alive-component-vnode\n if (self.historyShouldChange) {\n if (vnode && !isDef(vnode.key)) {\n if (self.isReplace) {\n vnode.key = genKey(self.stackPointer, router);\n } else if (self.isPush) {\n vnode.key = genKey(self.stackPointer + 1, router);\n } else {\n vnode.key = genKey(self.stackPointer - 1, router);\n }\n }\n } else {\n // when historyShouldChange is false should rerender only, should not create new vm ,use the same vnode.key issue#7\n vnode.key = genKey(self.stackPointer, router);\n }\n return tmp.apply(this, arguments);\n };\n this.hacked = true;\n }\n /********** callback functions ************/\n onInitial(vm) {\n const currentStateId = getStateId();\n if (isDef(currentStateId)) {\n this.setStackPointer(currentStateId);\n } else {\n this.setState(0);\n }\n this.historyStack.push(vm, this.stackPointer);\n }\n onPush(vm) {\n this.setState(this.increaseStackPointer());\n this.historyStack.push(vm, this.stackPointer);\n }\n onBack(vm) {\n this.historyStack.pop();\n this.decreaseStackPointer();\n this.historyStack.push(vm, this.stackPointer);\n }\n onReplace(vm) {\n const shouldDestroy = !(\n isDef(this.replacePrePath) &&\n this.replaceStay.includes(this.replacePrePath)\n );\n if (shouldDestroy) {\n this.pre?.$keepAliveDestroy?.();\n }\n this.setState(this.stackPointer);\n this.historyStack.push(vm, this.stackPointer);\n this.isReplace = false;\n this.replacePrePath = undefined;\n }\n get currentVm() {\n return getCurrentVM(this.router);\n }\n get isPush() {\n if (!this.isReplace) {\n const stateId = getStateId();\n return !isDef(stateId) || this.preStateId <= stateId;\n }\n return false;\n }\n get stackPointer() {\n return this.router._stack;\n }\n setState(id) {\n this.setStackPointer(id);\n replaceState(this.mode, this.router, id);\n }\n setStackPointer(val) {\n this.router._stack = val;\n }\n increaseStackPointer() {\n return (this.router._stack += 1);\n }\n decreaseStackPointer() {\n return (this.router._stack -= 1);\n }\n}\n","import Helper from \"./helper\";\nimport { extendVue, extendHistory } from \"./extension\";\nimport { inBrowser } from \"./utils\";\n\nlet singleton = undefined;\n// TODO: 1. abstract mode support\nexport default function createHelper(config) {\n if (config.Vue === undefined || config.router === undefined) {\n console.warn(\n \"warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper\"\n );\n return;\n }\n if(singleton) {\n return singleton;\n }\n if (inBrowser) {\n extendHistory(window.history);\n }\n extendVue(config.Vue);\n return (singleton = new Helper(config));\n}","import { isDef } from \"./utils\";\n\nexport function extendHistory(history) {\n const rstmp = history.replaceState;\n history.replaceState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n rstmp.call(history, s, op, path);\n }\n const historyPushState = history.pushState;\n history.pushState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n historyPushState.call(history, s, op, path);\n }\n}\n\nexport function extendVue(Vue) {\n const dtmp = Vue.prototype.$destroy;\n /**\n * @description remove the cache in component before invoke $destroy\n */\n Vue.prototype.$keepAliveDestroy = function () {\n if (this.$vnode && this.$vnode.data.keepAlive) {\n if (\n this.$vnode.parent &&\n this.$vnode.parent.componentInstance &&\n this.$vnode.parent.componentInstance.cache\n ) {\n if (this.$vnode.componentOptions) {\n const key = !isDef(this.$vnode.key)\n ? this.$vnode.componentOptions.Ctor.cid +\n (this.$vnode.componentOptions.tag\n ? `::${this.$vnode.componentOptions.tag}`\n : \"\")\n : this.$vnode.key;\n const cache = this.$vnode.parent.componentInstance.cache;\n const keys = this.$vnode.parent.componentInstance.keys;\n if (cache[key]) {\n if (keys.length) {\n const index = keys.indexOf(key);\n if (index > -1) {\n keys.splice(index, 1);\n }\n }\n delete cache[key];\n }\n }\n }\n }\n dtmp.apply(this, arguments);\n };\n}\n"],"names":["isDef","v","PLACEHOLDER_VM","__placeholder","getStateId","state","getCurrentState","id","undefined","history","genKey","num","router","Number","current","path","getCurrentVM","matched","length","instances","default","getFirstComponentChild","children","Array","isArray","i","c","componentOptions","isAsyncPlaceholder","node","isComment","asyncFactory","inBrowser","window","HistoryStack","historyStackMap","push","vm","index","stack","this","includes","filter","item","_isDestroyed","vms","pop","last","forEach","$keepAliveDestroy","removeGreater","clear","VueRouterKeepAliveHelper","Vue","replaceStay","_stack","mode","historyShouldChange","isReplace","replacePrePath","preStateId","pre","hacked","historyStack","init","routerHooks","hackRouter","afterEach","to","from","_this","nextTick","currentVm","pendingToPushVm","$vnode","data","keepAlive","onInitial","onReplace","isPush","onPush","onBack","stackPointer","key","_vnode","parent","setCurrentVnodeKey","hackKeepAliveRender","componentInstance","rtmp","replace","ocation","onComplete","onAbort","location","_this2","call","e","rtmpf","gstmp","go","number","pstmp","Promise","pstmpf","cache","keys","vnode","splice","replaceFirstKeyAndCache","tmp","$options","render","self","slot","$slots","apply","arguments","currentStateId","setStackPointer","setState","increaseStackPointer","decreaseStackPointer","pathname","search","hash","isFilSys","href","startsWith","replaceState","val","stateId","singleton","createHelper","config","dtmp","rstmp","op","old","Object","assign","s","historyPushState","pushState","extendHistory","prototype","$destroy","Ctor","cid","tag","indexOf","Helper","console","warn"],"mappings":"sKAAO,IAAMA,EAAQ,SAAUC,UACtBA,MAAAA,GAEHC,EAAiB,CACrBC,eAAe,GAOJC,EAAa,eAClBC,EAAQC,WACPN,EAAMK,GAASA,EAAME,QAAKC,GAe7BF,EAAkB,kBACfG,QAAQJ,OAGJK,EAAS,SAAUC,EAAKC,gCACLC,OAAOF,GAAOC,EAAOH,QAAQK,QAAQC,MAExDC,E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.js +++ b/dist/index.js @@ -1,2 +1,2 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).createHelper=e()}(this,(function(){"use strict";function t(t,e){for(var n=0;n0?t.history.current.matched[0].instances.default:void 0},a=function(t){if(Array.isArray(t))for(var n=0;n=t;)this.pop()},e.clear=function(){this.historyStackMap=[]},t}(),p=function(){function r(t){var e=t.Vue,n=t.router,i=t.replaceStay;this.Vue=e,this.router=n,this.router._stack=0,this.mode=n.mode,this.historyShouldChange=!1,this.isReplace=!1,this.replacePrePath=void 0,this.preStateId=0,this.pre=null,this.replaceStay=i||[],this.hacked=!1,this.historyStack=new u,this.init()}var c,h,p,l=r.prototype;return l.init=function(){this.routerHooks(),this.hackRouter()},l.routerHooks=function(){var t=this,i=this.router;i.afterEach((function(r,a){t.historyShouldChange=!0,t.Vue.nextTick((function(){var r,a,c=t.currentVm,h=e(r=c)&&r.$vnode.data.keepAlive?r:n;null===t.pre?t.onInitial(h):t.isReplace?t.onReplace(h):t.isPush?t.onPush(h):t.onBack(h),t.pre=c,t.preStateId=t.stackPointer,(a=h)&&a.__placeholder||(!function(t,e){var n=s(t);n&&n._vnode&&(n._vnode.parent.key=e)}(i,o(t.stackPointer,i)),!t.hacked&&c&&t.hackKeepAliveRender(c.$vnode.parent.componentInstance),t.historyShouldChange=!1)}))}))},l.hackRouter=function(){var t=this,n=this.router,i=n.replace;n.replace=function(r,o,s){!function(r,o,s){t.isReplace=!0,t.replacePrePath=n.history.current.path,i.call(n,r,o,(function(n){t.isReplace=!1,t.replacePrePath=void 0,e(s)&&s(n)}))}(r,o,s)};var r=n.go;n.go=function(e){return i=e,t.isReplace=!1,r.call(n,i);var i};var o=n.push;n.push=function(e,i,r){return function(e,i,r){if(t.isReplace=!1,!i&&!r&&"undefined"!=typeof Promise)return o.call(n,e,i,r);o.call(n,e,i,r)}(e,i,r)}},l.hackKeepAliveRender=function(t){!function(t,n){if(e(t)&&e(t.cache)&&e(t.keys)){var i=t.keys,r=t.cache;if(1===i.length){var o=r[i[0]];delete r[i[0]],i.splice(0,1),i.push(n),r[n]=o}}}(t,o(this.stackPointer,this.router));var n=t.$options.render,i=this,r=this.router;t.$options.render=function(){var t=this.$slots.default,s=a(t);return i.historyShouldChange?s&&!e(s.key)&&(i.isReplace?s.key=o(i.stackPointer,r):i.isPush?s.key=o(i.stackPointer+1,r):s.key=o(i.stackPointer-1,r)):s.key=o(i.stackPointer,r),n.apply(this,arguments)},this.hacked=!0},l.onInitial=function(t){var n=i();e(n)?this.setStackPointer(n):this.setState(0),this.historyStack.push(t,this.stackPointer)},l.onPush=function(t){this.setState(this.increaseStackPointer()),this.historyStack.push(t,this.stackPointer)},l.onBack=function(t){this.historyStack.pop(),this.decreaseStackPointer(),this.historyStack.push(t,this.stackPointer)},l.onReplace=function(t){var n;e(this.replacePrePath)&&this.replaceStay.includes(this.replacePrePath)||(null==(n=this.pre)||n.$keepAliveDestroy());this.setState(this.stackPointer),this.historyStack.push(t,this.stackPointer),this.isReplace=!1,this.replacePrePath=void 0},l.setState=function(t){this.setStackPointer(t),function(t,n,i){var r=window.location,o=""+r.pathname+r.search+r.hash,s=e(history.state)?history.state:{};s.id=i;var a=window.location.href.startsWith("file://");history.replaceState(s,"",a?null:o)}(this.mode,this.router,t)},l.setStackPointer=function(t){this.router._stack=t},l.increaseStackPointer=function(){return this.router._stack+=1},l.decreaseStackPointer=function(){return this.router._stack-=1},c=r,(h=[{key:"currentVm",get:function(){return s(this.router)}},{key:"isPush",get:function(){if(!this.isReplace){var t=i();return!e(t)||this.preStateId<=t}return!1}},{key:"stackPointer",get:function(){return this.router._stack}}])&&t(c.prototype,h),p&&t(c,p),r}();var l=void 0;return function(t){var n,i;if(void 0!==t.Vue&&void 0!==t.router)return l||(h&&function(t){var e=t.replaceState;t.replaceState=function(n,i,r){var o=Object.assign({},t.state),s=Object.assign(o,n);e.call(t,s,i,r)};var n=t.pushState;t.pushState=function(e,i,r){var o=Object.assign({},t.state),s=Object.assign(o,e);n.call(t,s,i,r)}}(window.history),n=t.Vue,i=n.prototype.$destroy,n.prototype.$keepAliveDestroy=function(){if(this.$vnode&&this.$vnode.data.keepAlive&&this.$vnode.parent&&this.$vnode.parent.componentInstance&&this.$vnode.parent.componentInstance.cache&&this.$vnode.componentOptions){var t=e(this.$vnode.key)?this.$vnode.key:this.$vnode.componentOptions.Ctor.cid+(this.$vnode.componentOptions.tag?"::"+this.$vnode.componentOptions.tag:""),n=this.$vnode.parent.componentInstance.cache,r=this.$vnode.parent.componentInstance.keys;if(n[t]){if(r.length){var o=r.indexOf(t);o>-1&&r.splice(o,1)}delete n[t]}}i.apply(this,arguments)},l=new p(t));console.warn("warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper")}})); +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).createHelper=e()}(this,(function(){"use strict";function t(t,e){for(var n=0;n0?t.history.current.matched[0].instances.default:void 0},a=function(t){if(Array.isArray(t))for(var n=0;n=t;)this.pop()},e.clear=function(){this.historyStackMap=[]},t}(),p=function(){function r(t){var e=t.Vue,n=t.router,i=t.replaceStay;this.Vue=e,this.router=n,this.router._stack=0,this.mode=n.mode,this.historyShouldChange=!1,this.isReplace=!1,this.replacePrePath=void 0,this.preStateId=0,this.pre=null,this.replaceStay=i||[],this.hacked=!1,this.historyStack=new u,this.init()}var c,h,p,l=r.prototype;return l.init=function(){this.routerHooks(),this.hackRouter()},l.routerHooks=function(){var t=this,i=this.router;i.afterEach((function(r,a){t.historyShouldChange=!0,t.Vue.nextTick((function(){var r,a,c=t.currentVm,h=e(r=c)&&r.$vnode.data.keepAlive?r:n;null===t.pre?t.onInitial(h):t.isReplace?t.onReplace(h):t.isPush?t.onPush(h):t.onBack(h),t.pre=c,t.preStateId=t.stackPointer,(a=h)&&a.__placeholder||(!function(t,e){var n=s(t);n&&n._vnode&&(n._vnode.parent.key=e)}(i,o(t.stackPointer,i)),!t.hacked&&c&&t.hackKeepAliveRender(c.$vnode.parent.componentInstance),t.historyShouldChange=!1)}))}))},l.hackRouter=function(){var t=this,n=this.router,i=n.replace;n.replace=function(r,o,s){!function(r,o,s){t.isReplace=!0,t.replacePrePath=n.history.current.path,i.call(n,r,o,(function(n){t.isReplace=!1,t.replacePrePath=void 0,e(s)&&s(n)}))}(r,o,s)};var r=n.go;n.go=function(e){return i=e,t.isReplace=!1,r.call(n,i);var i};var o=n.push;n.push=function(e,i,r){return function(e,i,r){if(t.isReplace=!1,!i&&!r&&"undefined"!=typeof Promise)return o.call(n,e,i,r);o.call(n,e,i,r)}(e,i,r)}},l.hackKeepAliveRender=function(t){!function(t,n){if(e(t)&&e(t.cache)&&e(t.keys)){var i=t.keys,r=t.cache;if(1===i.length){var o=r[i[0]];delete r[i[0]],i.splice(0,1),i.push(n),r[n]=o}}}(t,o(this.stackPointer,this.router));var n=t.$options.render,i=this,r=this.router;t.$options.render=function(){var t=this.$slots.default,s=a(t);return i.historyShouldChange?s&&!e(s.key)&&(i.isReplace?s.key=o(i.stackPointer,r):i.isPush?s.key=o(i.stackPointer+1,r):s.key=o(i.stackPointer-1,r)):s.key=o(i.stackPointer,r),n.apply(this,arguments)},this.hacked=!0},l.onInitial=function(t){var n=i();e(n)?this.setStackPointer(n):this.setState(0),this.historyStack.push(t,this.stackPointer)},l.onPush=function(t){this.setState(this.increaseStackPointer()),this.historyStack.push(t,this.stackPointer)},l.onBack=function(t){this.historyStack.pop(),this.decreaseStackPointer(),this.historyStack.push(t,this.stackPointer)},l.onReplace=function(t){var n;e(this.replacePrePath)&&this.replaceStay.includes(this.replacePrePath)||(null==(n=this.pre)||null==n.$keepAliveDestroy||n.$keepAliveDestroy());this.setState(this.stackPointer),this.historyStack.push(t,this.stackPointer),this.isReplace=!1,this.replacePrePath=void 0},l.setState=function(t){this.setStackPointer(t),function(t,n,i){var r=window.location,o=""+r.pathname+r.search+r.hash,s=e(history.state)?history.state:{};s.id=i;var a=window.location.href.startsWith("file://");history.replaceState(s,"",a?null:o)}(this.mode,this.router,t)},l.setStackPointer=function(t){this.router._stack=t},l.increaseStackPointer=function(){return this.router._stack+=1},l.decreaseStackPointer=function(){return this.router._stack-=1},c=r,(h=[{key:"currentVm",get:function(){return s(this.router)}},{key:"isPush",get:function(){if(!this.isReplace){var t=i();return!e(t)||this.preStateId<=t}return!1}},{key:"stackPointer",get:function(){return this.router._stack}}])&&t(c.prototype,h),p&&t(c,p),r}();var l=void 0;return function(t){var n,i;if(void 0!==t.Vue&&void 0!==t.router)return l||(h&&function(t){var e=t.replaceState;t.replaceState=function(n,i,r){var o=Object.assign({},t.state),s=Object.assign(o,n);e.call(t,s,i,r)};var n=t.pushState;t.pushState=function(e,i,r){var o=Object.assign({},t.state),s=Object.assign(o,e);n.call(t,s,i,r)}}(window.history),n=t.Vue,i=n.prototype.$destroy,n.prototype.$keepAliveDestroy=function(){if(this.$vnode&&this.$vnode.data.keepAlive&&this.$vnode.parent&&this.$vnode.parent.componentInstance&&this.$vnode.parent.componentInstance.cache&&this.$vnode.componentOptions){var t=e(this.$vnode.key)?this.$vnode.key:this.$vnode.componentOptions.Ctor.cid+(this.$vnode.componentOptions.tag?"::"+this.$vnode.componentOptions.tag:""),n=this.$vnode.parent.componentInstance.cache,r=this.$vnode.parent.componentInstance.keys;if(n[t]){if(r.length){var o=r.indexOf(t);o>-1&&r.splice(o,1)}delete n[t]}}i.apply(this,arguments)},l=new p(t));console.warn("warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper")}})); //# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map index 57627cc..48cf75b 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sources":["../src/utils.js","../src/historyStack.js","../src/helper.js","../src/index.js","../src/extension.js"],"sourcesContent":["export const isDef = function (v) {\n return v !== undefined && v !== null;\n};\nconst PLACEHOLDER_VM = {\n __placeholder: true,\n};\nexport const resolvePushedVm = (currentVm) => {\n return (isDef(currentVm) && currentVm.$vnode.data.keepAlive) ? currentVm : PLACEHOLDER_VM;\n};\nexport const isPlaceHolderVm = (vm) => vm && !!vm.__placeholder;\n\nexport const getStateId = function () {\n const state = getCurrentState();\n return isDef(state) ? state.id : undefined;\n};\n\nexport const getQuery = function (params) {\n let query = \"\";\n query = Object.keys(params)\n .map(\n (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`\n )\n .join(\"&\");\n if (query.length > 0) {\n query = `?${query}`;\n }\n return query;\n};\nconst getCurrentState = function () {\n return history.state;\n};\n\nexport const genKey = function (num, router) {\n return `keep-alive-vnode-key${Number(num)}${router.history.current.path}`;\n};\nexport const getCurrentVM = function (router) {\n return router.history.current.matched.length > 0\n ? router.history.current.matched[0].instances.default\n : undefined;\n};\nexport const setCurrentVnodeKey = function (router, key) {\n const current = getCurrentVM(router);\n if (current && current._vnode) {\n current._vnode.parent.key = key;\n }\n};\nexport const replaceFirstKeyAndCache = function (vm, key) {\n if (!isDef(vm) || !isDef(vm.cache) || !isDef(vm.keys)) {\n return;\n }\n const keys = vm.keys;\n const cache = vm.cache;\n if (keys.length === 1) {\n const vnode = cache[keys[0]];\n delete cache[keys[0]];\n keys.splice(0, 1);\n keys.push(key);\n cache[key] = vnode;\n }\n};\nexport const getFirstComponentChild = function (children) {\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length; i++) {\n const c = children[i];\n if (isDef(c) && (isDef(c.componentOptions) || isAsyncPlaceholder(c))) {\n return c;\n }\n }\n }\n};\nconst isAsyncPlaceholder = function (node) {\n return node.isComment && node.asyncFactory;\n};\n\nexport const replaceState = function (mode, router, id) {\n const { pathname, search, hash } = window.location;\n let path = `${pathname}${search}${hash}`;\n let state = isDef(history.state) ? history.state : {};\n state[\"id\"] = id;\n // optimize file:// URL\n const isFilSys = window.location.href.startsWith(\"file://\");\n history.replaceState(state, \"\", isFilSys ? null : path);\n};\n\nexport const inBrowser = typeof window !== \"undefined\";\n","export default class HistoryStack {\n constructor() {\n this.historyStackMap = [];\n }\n push(vm, index) {\n // const cur = stackPointer();\n const stack = this.historyStackMap[index];\n if (Array.isArray(stack)) {\n !stack.includes(vm) && stack.push(vm);\n this.historyStackMap[index] = stack.filter((item) => !item._isDestroyed);\n } else {\n const vms = [];\n vms.push(vm);\n this.historyStackMap[index] = vms;\n }\n }\n pop() {\n const last = this.historyStackMap.pop();\n Array.isArray(last) &&\n last.forEach(\n (vm) => vm && vm.$keepAliveDestroy && vm.$keepAliveDestroy()\n );\n }\n removeGreater(index) {\n while (this.historyStackMap.length >= index) {\n this.pop();\n }\n }\n clear() {\n this.historyStackMap = [];\n }\n}\n","import {\n getCurrentVM,\n isDef,\n getStateId,\n resolvePushedVm,\n genKey,\n replaceFirstKeyAndCache,\n getFirstComponentChild,\n isPlaceHolderVm,\n setCurrentVnodeKey,\n replaceState,\n} from \"./utils\";\nimport HistoryStack from \"./historyStack\";\n\nexport default class VueRouterKeepAliveHelper{\n constructor({ Vue, router, replaceStay }) {\n this.Vue = Vue;\n this.router = router;\n this.router._stack = 0;\n this.mode = router.mode; // hash or history\n this.historyShouldChange = false;\n this.isReplace = false;\n this.replacePrePath = undefined;\n this.preStateId = 0;\n this.pre = null;\n this.replaceStay = replaceStay || [];\n this.hacked = false;\n this.historyStack = new HistoryStack();\n this.init();\n }\n init() {\n this.routerHooks();\n this.hackRouter();\n }\n /**\n * use afterEach hook to set state.key and add the reference of vm to the historyStack\n */\n routerHooks() {\n const router = this.router;\n router.afterEach((to, from) => {\n this.historyShouldChange = true;\n // get the vm instance after render\n this.Vue.nextTick(() => {\n const current = this.currentVm;\n const pendingToPushVm = resolvePushedVm(current);\n if (this.pre === null) {\n this.onInitial(pendingToPushVm);\n } else if (this.isReplace) {\n this.onReplace(pendingToPushVm);\n } else if (this.isPush) {\n this.onPush(pendingToPushVm);\n } else {\n this.onBack(pendingToPushVm);\n }\n this.pre = current;\n this.preStateId = this.stackPointer;\n if (!isPlaceHolderVm(pendingToPushVm)) {\n setCurrentVnodeKey(router, genKey(this.stackPointer, router));\n if (!this.hacked && current) {\n this.hackKeepAliveRender(current.$vnode.parent.componentInstance);\n }\n this.historyShouldChange = false;\n }\n });\n });\n }\n /**\n * @description hack router go , replace and push functions to tell replace from back and push\n */\n hackRouter() {\n const router = this.router;\n const rtmp = router.replace;\n const rtmpf = (location, onComplete, onAbort) => {\n this.isReplace = true;\n this.replacePrePath = router.history.current.path;\n rtmp.call(router, location, onComplete, (e) => {\n this.isReplace = false;\n this.replacePrePath = undefined;\n isDef(onAbort) && onAbort(e);\n });\n };\n router.replace = function (ocation, onComplete, onAbort) {\n rtmpf(ocation, onComplete, onAbort);\n };\n\n const gstmp = router.go;\n const gstmpf = (number) => {\n this.isReplace = false;\n return gstmp.call(router, number);\n };\n router.go = function (num) {\n return gstmpf(num);\n };\n const pstmp = router.push;\n const pstmpf = (location, onComplete, onAbort) => {\n this.isReplace = false;\n if (!onComplete && !onAbort && typeof Promise !== \"undefined\") {\n return pstmp.call(router, location, onComplete, onAbort);\n } else {\n pstmp.call(router, location, onComplete, onAbort);\n }\n };\n router.push = function (location, onComplete, onAbort) {\n return pstmpf(location, onComplete, onAbort);\n };\n }\n /**\n * @description hack the render function of keep-alive component, modify the key of vnode for cache\n * @param {*} vm keep-alive component instance\n */\n hackKeepAliveRender(vm) {\n // modify the first keep alive key and cache\n replaceFirstKeyAndCache(vm, genKey(this.stackPointer, this.router));\n\n const tmp = vm.$options.render;\n const self = this;\n const router = this.router;\n vm.$options.render = function () {\n const slot = this.$slots.default;\n const vnode = getFirstComponentChild(slot); // vnode is a keep-alive-component-vnode\n if (self.historyShouldChange) {\n if (vnode && !isDef(vnode.key)) {\n if (self.isReplace) {\n vnode.key = genKey(self.stackPointer, router);\n } else if (self.isPush) {\n vnode.key = genKey(self.stackPointer + 1, router);\n } else {\n vnode.key = genKey(self.stackPointer - 1, router);\n }\n }\n } else {\n // when historyShouldChange is false should rerender only, should not create new vm ,use the same vnode.key issue#7\n vnode.key = genKey(self.stackPointer, router);\n }\n return tmp.apply(this, arguments);\n };\n this.hacked = true;\n }\n /********** callback functions ************/\n onInitial(vm) {\n const currentStateId = getStateId();\n if (isDef(currentStateId)) {\n this.setStackPointer(currentStateId);\n } else {\n this.setState(0);\n }\n this.historyStack.push(vm, this.stackPointer);\n }\n onPush(vm) {\n this.setState(this.increaseStackPointer());\n this.historyStack.push(vm, this.stackPointer);\n }\n onBack(vm) {\n this.historyStack.pop();\n this.decreaseStackPointer();\n this.historyStack.push(vm, this.stackPointer);\n }\n onReplace(vm) {\n const shouldDestroy = !(\n isDef(this.replacePrePath) &&\n this.replaceStay.includes(this.replacePrePath)\n );\n if (shouldDestroy) {\n this.pre?.$keepAliveDestroy();\n }\n this.setState(this.stackPointer);\n this.historyStack.push(vm, this.stackPointer);\n this.isReplace = false;\n this.replacePrePath = undefined;\n }\n get currentVm() {\n return getCurrentVM(this.router);\n }\n get isPush() {\n if (!this.isReplace) {\n const stateId = getStateId();\n return !isDef(stateId) || this.preStateId <= stateId;\n }\n return false;\n }\n get stackPointer() {\n return this.router._stack;\n }\n setState(id) {\n this.setStackPointer(id);\n replaceState(this.mode, this.router, id);\n }\n setStackPointer(val) {\n this.router._stack = val;\n }\n increaseStackPointer() {\n return (this.router._stack += 1);\n }\n decreaseStackPointer() {\n return (this.router._stack -= 1);\n }\n}\n","import Helper from \"./helper\";\nimport { extendVue, extendHistory } from \"./extension\";\nimport { inBrowser } from \"./utils\";\n\nlet singleton = undefined;\n// TODO: 1. abstract mode support\nexport default function createHelper(config) {\n if (config.Vue === undefined || config.router === undefined) {\n console.warn(\n \"warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper\"\n );\n return;\n }\n if(singleton) {\n return singleton;\n }\n if (inBrowser) {\n extendHistory(window.history);\n }\n extendVue(config.Vue);\n return (singleton = new Helper(config));\n}","import { isDef } from \"./utils\";\n\nexport function extendHistory(history) {\n const rstmp = history.replaceState;\n history.replaceState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n rstmp.call(history, s, op, path);\n }\n const historyPushState = history.pushState;\n history.pushState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n historyPushState.call(history, s, op, path);\n }\n}\n\nexport function extendVue(Vue) {\n const dtmp = Vue.prototype.$destroy;\n /**\n * @description remove the cache in component before invoke $destroy\n */\n Vue.prototype.$keepAliveDestroy = function () {\n if (this.$vnode && this.$vnode.data.keepAlive) {\n if (\n this.$vnode.parent &&\n this.$vnode.parent.componentInstance &&\n this.$vnode.parent.componentInstance.cache\n ) {\n if (this.$vnode.componentOptions) {\n const key = !isDef(this.$vnode.key)\n ? this.$vnode.componentOptions.Ctor.cid +\n (this.$vnode.componentOptions.tag\n ? `::${this.$vnode.componentOptions.tag}`\n : \"\")\n : this.$vnode.key;\n const cache = this.$vnode.parent.componentInstance.cache;\n const keys = this.$vnode.parent.componentInstance.keys;\n if (cache[key]) {\n if (keys.length) {\n const index = keys.indexOf(key);\n if (index > -1) {\n keys.splice(index, 1);\n }\n }\n delete cache[key];\n }\n }\n }\n }\n dtmp.apply(this, arguments);\n };\n}\n"],"names":["isDef","v","PLACEHOLDER_VM","__placeholder","getStateId","state","getCurrentState","id","undefined","history","genKey","num","router","Number","current","path","getCurrentVM","matched","length","instances","default","getFirstComponentChild","children","Array","isArray","i","c","componentOptions","isAsyncPlaceholder","node","isComment","asyncFactory","inBrowser","window","HistoryStack","historyStackMap","push","vm","index","stack","this","includes","filter","item","_isDestroyed","vms","pop","last","forEach","$keepAliveDestroy","removeGreater","clear","VueRouterKeepAliveHelper","Vue","replaceStay","_stack","mode","historyShouldChange","isReplace","replacePrePath","preStateId","pre","hacked","historyStack","init","routerHooks","hackRouter","afterEach","to","from","_this","nextTick","currentVm","pendingToPushVm","$vnode","data","keepAlive","onInitial","onReplace","isPush","onPush","onBack","stackPointer","key","_vnode","parent","setCurrentVnodeKey","hackKeepAliveRender","componentInstance","rtmp","replace","ocation","onComplete","onAbort","location","_this2","call","e","rtmpf","gstmp","go","number","pstmp","Promise","pstmpf","cache","keys","vnode","splice","replaceFirstKeyAndCache","tmp","$options","render","self","slot","$slots","apply","arguments","currentStateId","setStackPointer","setState","increaseStackPointer","decreaseStackPointer","pathname","search","hash","isFilSys","href","startsWith","replaceState","val","stateId","singleton","config","dtmp","rstmp","op","old","Object","assign","s","historyPushState","pushState","extendHistory","prototype","$destroy","Ctor","cid","tag","indexOf","Helper","console","warn"],"mappings":"mZAAO,IAAMA,EAAQ,SAAUC,UACtBA,MAAAA,GAEHC,EAAiB,CACrBC,eAAe,GAOJC,EAAa,eAClBC,EAAQC,WACPN,EAAMK,GAASA,EAAME,QAAKC,GAe7BF,EAAkB,kBACfG,QAAQJ,OAGJK,EAAS,SAAUC,EAAKC,gCACLC,OAAOF,GAAOC,EAAOH,QAAQK,QAAQC,MAExDC,EAAe,SAAUJ,UAC7BA,EAAOH,QAAQK,QAAQG,QAAQC,OAAS,EAC3CN,EAAOH,QAAQK,QAAQG,QAAQ,GAAGE,UAAUC,aAC5CZ,GAsBOa,EAAyB,SAAUC,MAC1CC,MAAMC,QAAQF,OACX,IAAIG,EAAI,EAAGA,EAAIH,EAASJ,OAAQO,IAAK,KAClCC,EAAIJ,EAASG,MACfzB,EAAM0B,KAAO1B,EAAM0B,EAAEC,mBAAqBC,EAAmBF,WACxDA,IAKTE,EAAqB,SAAUC,UAC5BA,EAAKC,WAAaD,EAAKE,cAanBC,EAA8B,oBAAXC,OCpFXC,+BAEZC,gBAAkB,8BAEzBC,KAAA,SAAKC,EAAIC,OAEDC,EAAQC,KAAKL,gBAAgBG,MAC/Bf,MAAMC,QAAQe,IACfA,EAAME,SAASJ,IAAOE,EAAMH,KAAKC,QAC7BF,gBAAgBG,GAASC,EAAMG,QA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nBhD,KAAKkC,OAAOW,OAAOG,kBAAkBiB,OAEjCjE,KAAKkC,OAAO/C,iBAAkB,KAC1BwD,EAAOnF,EAAMwC,KAAKkC,OAAOS,KAK3B3C,KAAKkC,OAAOS,IAJZ3C,KAAKkC,OAAO/C,iBAAiBuH,KAAKC,KACjC3G,KAAKkC,OAAO/C,iBAAiByH,SACrB5G,KAAKkC,OAAO/C,iBAAiByH,IAClC,IAEF3C,EAAQjE,KAAKkC,OAAOW,OAAOG,kBAAkBiB,MAC7CC,EAAOlE,KAAKkC,OAAOW,OAAOG,kBAAkBkB,QAC9CD,EAAMtB,GAAM,IACVuB,EAAKxF,OAAQ,KACToB,EAAQoE,EAAK2C,QAAQlE,GACvB7C,GAAS,GACXoE,EAAKE,OAAOtE,EAAO,UAGhBmE,EAAMtB,IAKrBmD,EAAKlB,MAAM5E,KAAM6E,YD9BXe,EAAY,IAAIkB,EAAOjB,IAZ7BkB,QAAQC,KACN"} \ No newline at end of file +{"version":3,"file":"index.js","sources":["../src/utils.js","../src/historyStack.js","../src/helper.js","../src/index.js","../src/extension.js"],"sourcesContent":["export const isDef = function (v) {\n return v !== undefined && v !== null;\n};\nconst PLACEHOLDER_VM = {\n __placeholder: true,\n};\nexport const resolvePushedVm = (currentVm) => {\n return (isDef(currentVm) && currentVm.$vnode.data.keepAlive) ? currentVm : PLACEHOLDER_VM;\n};\nexport const isPlaceHolderVm = (vm) => vm && !!vm.__placeholder;\n\nexport const getStateId = function () {\n const state = getCurrentState();\n return isDef(state) ? state.id : undefined;\n};\n\nexport const getQuery = function (params) {\n let query = \"\";\n query = Object.keys(params)\n .map(\n (key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`\n )\n .join(\"&\");\n if (query.length > 0) {\n query = `?${query}`;\n }\n return query;\n};\nconst getCurrentState = function () {\n return history.state;\n};\n\nexport const genKey = function (num, router) {\n return `keep-alive-vnode-key${Number(num)}${router.history.current.path}`;\n};\nexport const getCurrentVM = function (router) {\n return router.history.current.matched.length > 0\n ? router.history.current.matched[0].instances.default\n : undefined;\n};\nexport const setCurrentVnodeKey = function (router, key) {\n const current = getCurrentVM(router);\n if (current && current._vnode) {\n current._vnode.parent.key = key;\n }\n};\nexport const replaceFirstKeyAndCache = function (vm, key) {\n if (!isDef(vm) || !isDef(vm.cache) || !isDef(vm.keys)) {\n return;\n }\n const keys = vm.keys;\n const cache = vm.cache;\n if (keys.length === 1) {\n const vnode = cache[keys[0]];\n delete cache[keys[0]];\n keys.splice(0, 1);\n keys.push(key);\n cache[key] = vnode;\n }\n};\nexport const getFirstComponentChild = function (children) {\n if (Array.isArray(children)) {\n for (let i = 0; i < children.length; i++) {\n const c = children[i];\n if (isDef(c) && (isDef(c.componentOptions) || isAsyncPlaceholder(c))) {\n return c;\n }\n }\n }\n};\nconst isAsyncPlaceholder = function (node) {\n return node.isComment && node.asyncFactory;\n};\n\nexport const replaceState = function (mode, router, id) {\n const { pathname, search, hash } = window.location;\n let path = `${pathname}${search}${hash}`;\n let state = isDef(history.state) ? history.state : {};\n state[\"id\"] = id;\n // optimize file:// URL\n const isFilSys = window.location.href.startsWith(\"file://\");\n history.replaceState(state, \"\", isFilSys ? null : path);\n};\n\nexport const inBrowser = typeof window !== \"undefined\";\n","export default class HistoryStack {\n constructor() {\n this.historyStackMap = [];\n }\n push(vm, index) {\n // const cur = stackPointer();\n const stack = this.historyStackMap[index];\n if (Array.isArray(stack)) {\n !stack.includes(vm) && stack.push(vm);\n this.historyStackMap[index] = stack.filter((item) => !item._isDestroyed);\n } else {\n const vms = [];\n vms.push(vm);\n this.historyStackMap[index] = vms;\n }\n }\n pop() {\n const last = this.historyStackMap.pop();\n Array.isArray(last) &&\n last.forEach(\n (vm) => vm && vm.$keepAliveDestroy && vm.$keepAliveDestroy()\n );\n }\n removeGreater(index) {\n while (this.historyStackMap.length >= index) {\n this.pop();\n }\n }\n clear() {\n this.historyStackMap = [];\n }\n}\n","import {\n getCurrentVM,\n isDef,\n getStateId,\n resolvePushedVm,\n genKey,\n replaceFirstKeyAndCache,\n getFirstComponentChild,\n isPlaceHolderVm,\n setCurrentVnodeKey,\n replaceState,\n} from \"./utils\";\nimport HistoryStack from \"./historyStack\";\n\nexport default class VueRouterKeepAliveHelper{\n constructor({ Vue, router, replaceStay }) {\n this.Vue = Vue;\n this.router = router;\n this.router._stack = 0;\n this.mode = router.mode; // hash or history\n this.historyShouldChange = false;\n this.isReplace = false;\n this.replacePrePath = undefined;\n this.preStateId = 0;\n this.pre = null;\n this.replaceStay = replaceStay || [];\n this.hacked = false;\n this.historyStack = new HistoryStack();\n this.init();\n }\n init() {\n this.routerHooks();\n this.hackRouter();\n }\n /**\n * use afterEach hook to set state.key and add the reference of vm to the historyStack\n */\n routerHooks() {\n const router = this.router;\n router.afterEach((to, from) => {\n this.historyShouldChange = true;\n // get the vm instance after render\n this.Vue.nextTick(() => {\n const current = this.currentVm;\n const pendingToPushVm = resolvePushedVm(current);\n if (this.pre === null) {\n this.onInitial(pendingToPushVm);\n } else if (this.isReplace) {\n this.onReplace(pendingToPushVm);\n } else if (this.isPush) {\n this.onPush(pendingToPushVm);\n } else {\n this.onBack(pendingToPushVm);\n }\n this.pre = current;\n this.preStateId = this.stackPointer;\n if (!isPlaceHolderVm(pendingToPushVm)) {\n setCurrentVnodeKey(router, genKey(this.stackPointer, router));\n if (!this.hacked && current) {\n this.hackKeepAliveRender(current.$vnode.parent.componentInstance);\n }\n this.historyShouldChange = false;\n }\n });\n });\n }\n /**\n * @description hack router go , replace and push functions to tell replace from back and push\n */\n hackRouter() {\n const router = this.router;\n const rtmp = router.replace;\n const rtmpf = (location, onComplete, onAbort) => {\n this.isReplace = true;\n this.replacePrePath = router.history.current.path;\n rtmp.call(router, location, onComplete, (e) => {\n this.isReplace = false;\n this.replacePrePath = undefined;\n isDef(onAbort) && onAbort(e);\n });\n };\n router.replace = function (ocation, onComplete, onAbort) {\n rtmpf(ocation, onComplete, onAbort);\n };\n\n const gstmp = router.go;\n const gstmpf = (number) => {\n this.isReplace = false;\n return gstmp.call(router, number);\n };\n router.go = function (num) {\n return gstmpf(num);\n };\n const pstmp = router.push;\n const pstmpf = (location, onComplete, onAbort) => {\n this.isReplace = false;\n if (!onComplete && !onAbort && typeof Promise !== \"undefined\") {\n return pstmp.call(router, location, onComplete, onAbort);\n } else {\n pstmp.call(router, location, onComplete, onAbort);\n }\n };\n router.push = function (location, onComplete, onAbort) {\n return pstmpf(location, onComplete, onAbort);\n };\n }\n /**\n * @description hack the render function of keep-alive component, modify the key of vnode for cache\n * @param {*} vm keep-alive component instance\n */\n hackKeepAliveRender(vm) {\n // modify the first keep alive key and cache\n replaceFirstKeyAndCache(vm, genKey(this.stackPointer, this.router));\n\n const tmp = vm.$options.render;\n const self = this;\n const router = this.router;\n vm.$options.render = function () {\n const slot = this.$slots.default;\n const vnode = getFirstComponentChild(slot); // vnode is a keep-alive-component-vnode\n if (self.historyShouldChange) {\n if (vnode && !isDef(vnode.key)) {\n if (self.isReplace) {\n vnode.key = genKey(self.stackPointer, router);\n } else if (self.isPush) {\n vnode.key = genKey(self.stackPointer + 1, router);\n } else {\n vnode.key = genKey(self.stackPointer - 1, router);\n }\n }\n } else {\n // when historyShouldChange is false should rerender only, should not create new vm ,use the same vnode.key issue#7\n vnode.key = genKey(self.stackPointer, router);\n }\n return tmp.apply(this, arguments);\n };\n this.hacked = true;\n }\n /********** callback functions ************/\n onInitial(vm) {\n const currentStateId = getStateId();\n if (isDef(currentStateId)) {\n this.setStackPointer(currentStateId);\n } else {\n this.setState(0);\n }\n this.historyStack.push(vm, this.stackPointer);\n }\n onPush(vm) {\n this.setState(this.increaseStackPointer());\n this.historyStack.push(vm, this.stackPointer);\n }\n onBack(vm) {\n this.historyStack.pop();\n this.decreaseStackPointer();\n this.historyStack.push(vm, this.stackPointer);\n }\n onReplace(vm) {\n const shouldDestroy = !(\n isDef(this.replacePrePath) &&\n this.replaceStay.includes(this.replacePrePath)\n );\n if (shouldDestroy) {\n this.pre?.$keepAliveDestroy?.();\n }\n this.setState(this.stackPointer);\n this.historyStack.push(vm, this.stackPointer);\n this.isReplace = false;\n this.replacePrePath = undefined;\n }\n get currentVm() {\n return getCurrentVM(this.router);\n }\n get isPush() {\n if (!this.isReplace) {\n const stateId = getStateId();\n return !isDef(stateId) || this.preStateId <= stateId;\n }\n return false;\n }\n get stackPointer() {\n return this.router._stack;\n }\n setState(id) {\n this.setStackPointer(id);\n replaceState(this.mode, this.router, id);\n }\n setStackPointer(val) {\n this.router._stack = val;\n }\n increaseStackPointer() {\n return (this.router._stack += 1);\n }\n decreaseStackPointer() {\n return (this.router._stack -= 1);\n }\n}\n","import Helper from \"./helper\";\nimport { extendVue, extendHistory } from \"./extension\";\nimport { inBrowser } from \"./utils\";\n\nlet singleton = undefined;\n// TODO: 1. abstract mode support\nexport default function createHelper(config) {\n if (config.Vue === undefined || config.router === undefined) {\n console.warn(\n \"warning: router helper needs Vue and root router ,see more for guide : https://github.com/Zippowxk/vue-router-keep-alive-helper\"\n );\n return;\n }\n if(singleton) {\n return singleton;\n }\n if (inBrowser) {\n extendHistory(window.history);\n }\n extendVue(config.Vue);\n return (singleton = new Helper(config));\n}","import { isDef } from \"./utils\";\n\nexport function extendHistory(history) {\n const rstmp = history.replaceState;\n history.replaceState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n rstmp.call(history, s, op, path);\n }\n const historyPushState = history.pushState;\n history.pushState = function (state, op, path) {\n const old = Object.assign({}, history.state);\n const s = Object.assign(old, state);\n historyPushState.call(history, s, op, path);\n }\n}\n\nexport function extendVue(Vue) {\n const dtmp = Vue.prototype.$destroy;\n /**\n * @description remove the cache in component before invoke $destroy\n */\n Vue.prototype.$keepAliveDestroy = function () {\n if (this.$vnode && this.$vnode.data.keepAlive) {\n if (\n this.$vnode.parent &&\n this.$vnode.parent.componentInstance &&\n this.$vnode.parent.componentInstance.cache\n ) {\n if (this.$vnode.componentOptions) {\n const key = !isDef(this.$vnode.key)\n ? this.$vnode.componentOptions.Ctor.cid +\n (this.$vnode.componentOptions.tag\n ? `::${this.$vnode.componentOptions.tag}`\n : \"\")\n : this.$vnode.key;\n const cache = this.$vnode.parent.componentInstance.cache;\n const keys = this.$vnode.parent.componentInstance.keys;\n if (cache[key]) {\n if (keys.length) {\n const index = keys.indexOf(key);\n if (index > -1) {\n keys.splice(index, 1);\n }\n }\n delete cache[key];\n }\n }\n }\n }\n dtmp.apply(this, arguments);\n };\n}\n"],"names":["isDef","v","PLACEHOLDER_VM","__placeholder","getStateId","state","getCurrentState","id","undefined","history","genKey","num","router","Number","current","path","getCurrentVM","matched","length","instances","default","getFirstComponentChild","children","Array","isArray","i","c","componentOptions","isAsyncPlaceholder","node","isComment","asyncFactory","inBrowser","window","HistoryStack","historyStackMap","push","vm","index","stack","this","includes","filter","item","_isDestroyed","vms","pop","last","forEach","$keepAliveDestroy","removeGreater","clear","VueRouterKeepAliveHelper","Vue","replaceStay","_stack","mode","historyShouldChange","isReplace","replacePrePath","preStateId","pre","hacked","historyStack","init","routerHooks","hackRouter","afterEach","to","from","_this","nextTick","currentVm","pendingToPushVm","$vnode","data","keepAlive","onInitial","onReplace","isPush","onPush","onBack","stackPointer","key","_vnode","parent","setCurrentVnodeKey","hackKeepAliveRender","componentInstance","rtmp","replace","ocation","onComplete","onAbort","location","_this2","call","e","rtmpf","gstmp","go","number","pstmp","Promise","pstmpf","cache","keys","vnode","splice","replaceFirstKeyAndCache","tmp","$options","render","self","slot","$slots","apply","arguments","currentStateId","setStackPointer","setState","increaseStackPointer","decreaseStackPointer","pathname","search","hash","isFilSys","href","startsWith","replaceState","val","stateId","singleton","config","dtmp","rstmp","op","old","Object","assign","s","historyPushState","pushState","extendHistory","prototype","$destroy","Ctor","cid","tag","indexOf","Helper","console","warn"],"mappings":"mZAAO,IAAMA,EAAQ,SAAUC,UACtBA,MAAAA,GAEHC,EAAiB,CACrBC,eAAe,GAOJC,EAAa,eAClBC,EAAQC,WACPN,EAAMK,GAASA,EAAME,QAAKC,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