vue2-client 1.10.32-alpha.1 → 1.10.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,179 +1,172 @@
1
- import { isDef, isRegExp, remove } from '@vue2-client/utils/util'
2
-
3
- const patternTypes = [String, RegExp, Array]
4
-
5
- function matches (pattern, name) {
6
- if (Array.isArray(pattern)) {
7
- if (pattern.indexOf(name) > -1) {
8
- return true
9
- } else {
10
- for (const item of pattern) {
11
- if (isRegExp(item) && item.test(name)) {
12
- return true
13
- }
14
- }
15
- return false
16
- }
17
- } else if (typeof pattern === 'string') {
18
- return pattern.split(',').indexOf(name) > -1
19
- } else if (isRegExp(pattern)) {
20
- return pattern.test(name)
21
- }
22
- /* istanbul ignore next */
23
- return false
24
- }
25
-
26
- function getComponentName (opts) {
27
- return opts && (opts.Ctor.options.name || opts.tag)
28
- }
29
-
30
- function getComponentKey (vnode) {
31
- const { componentOptions, key } = vnode
32
- return key == null
33
- ? componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '')
34
- : key + `::${componentOptions.Ctor.cid}`
35
- }
36
-
37
- function getFirstComponentChild (children) {
38
- if (Array.isArray(children)) {
39
- for (let i = 0; i < children.length; i++) {
40
- const c = children[i]
41
- if (isDef(c) && (isDef(c.componentOptions) || c.isAsyncPlaceholder)) {
42
- return c
43
- }
44
- }
45
- }
46
- }
47
-
48
- function pruneCache (keepAliveInstance, filter) {
49
- const { cache, keys, _vnode } = keepAliveInstance
50
- for (const key in cache) {
51
- const cachedNode = cache[key]
52
- if (cachedNode) {
53
- const name = getComponentName(cachedNode.componentOptions)
54
- const componentKey = getComponentKey(cachedNode)
55
- if (name && !filter(name, componentKey)) {
56
- pruneCacheEntry(cache, key, keys, _vnode)
57
- }
58
- }
59
- }
60
- }
61
-
62
- function pruneCacheEntry2 (cache, key, keys) {
63
- const cached = cache[key]
64
- if (cached) {
65
- cached.componentInstance.$destroy()
66
- }
67
- cache[key] = null
68
- remove(keys, key)
69
- }
70
-
71
- function pruneCacheEntry (cache, key, keys, current) {
72
- const cached = cache[key]
73
- if (cached && (!current || cached.tag !== current.tag)) {
74
- cached.componentInstance.$destroy()
75
- }
76
- cache[key] = null
77
- remove(keys, key)
78
- }
79
-
80
- export default {
81
- name: 'AKeepAlive',
82
- abstract: true,
83
- model: {
84
- prop: 'clearCaches',
85
- event: 'clear'
86
- },
87
- props: {
88
- include: patternTypes,
89
- exclude: patternTypes,
90
- excludeKeys: patternTypes,
91
- max: [String, Number],
92
- clearCaches: Array
93
- },
94
- watch: {
95
- clearCaches: function (val) {
96
- if (val && val.length > 0) {
97
- const { cache, keys } = this
98
- // 遍历所有缓存的 keys,找到以传入的 key 开头的项进行清除
99
- keys.forEach(cacheKey => {
100
- val.forEach(targetKey => {
101
- // 检查缓存的 key 是否以目标 key 开头
102
- console.warn('clearCaches:', cacheKey, targetKey, cacheKey.includes(`${targetKey}::`) || cacheKey.startsWith(targetKey))
103
- if (cacheKey.includes(`${targetKey}::`) || cacheKey.startsWith(targetKey)) {
104
- pruneCacheEntry2(cache, cacheKey, keys)
105
- }
106
- })
107
- })
108
- this.$emit('clear', [])
109
- }
110
- }
111
- },
112
-
113
- created () {
114
- this.cache = Object.create(null)
115
- this.keys = []
116
- },
117
-
118
- destroyed () {
119
- for (const key in this.cache) {
120
- pruneCacheEntry(this.cache, key, this.keys)
121
- }
122
- },
123
-
124
- mounted () {
125
- this.$watch('include', val => {
126
- pruneCache(this, (name) => matches(val, name))
127
- })
128
- this.$watch('exclude', val => {
129
- pruneCache(this, (name) => !matches(val, name))
130
- })
131
- this.$watch('excludeKeys', val => {
132
- pruneCache(this, (name, key) => !matches(val, key))
133
- })
134
- },
135
-
136
- render () {
137
- const slot = this.$slots.default
138
- const vnode = getFirstComponentChild(slot)
139
- const componentOptions = vnode && vnode.componentOptions
140
- if (componentOptions) {
141
- // check pattern
142
- const name = getComponentName(componentOptions)
143
- const componentKey = getComponentKey(vnode)
144
- const { include, exclude, excludeKeys } = this
145
- if (
146
- // not included
147
- (include && (!name || !matches(include, name))) ||
148
- // excluded
149
- (exclude && name && matches(exclude, name)) ||
150
- (excludeKeys && componentKey && matches(excludeKeys, componentKey))
151
- ) {
152
- return vnode
153
- }
154
-
155
- const { cache, keys } = this
156
- const key = vnode.key == null
157
- // same constructor may get registered as different local components
158
- // so cid alone is not enough (#3269)
159
- ? componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '')
160
- : vnode.key + `::${componentOptions.Ctor.cid}`
161
- if (cache[key]) {
162
- vnode.componentInstance = cache[key].componentInstance
163
- // make current key freshest
164
- remove(keys, key)
165
- keys.push(key)
166
- } else {
167
- cache[key] = vnode
168
- keys.push(key)
169
- // prune oldest entry
170
- if (this.max && keys.length > parseInt(this.max)) {
171
- pruneCacheEntry(cache, keys[0], keys, this._vnode)
172
- }
173
- }
174
-
175
- vnode.data.keepAlive = true
176
- }
177
- return vnode || (slot && slot[0])
178
- }
179
- }
1
+ import { isDef, isRegExp, remove } from '@vue2-client/utils/util'
2
+
3
+ const patternTypes = [String, RegExp, Array]
4
+
5
+ function matches (pattern, name) {
6
+ if (Array.isArray(pattern)) {
7
+ if (pattern.indexOf(name) > -1) {
8
+ return true
9
+ } else {
10
+ for (const item of pattern) {
11
+ if (isRegExp(item) && item.test(name)) {
12
+ return true
13
+ }
14
+ }
15
+ return false
16
+ }
17
+ } else if (typeof pattern === 'string') {
18
+ return pattern.split(',').indexOf(name) > -1
19
+ } else if (isRegExp(pattern)) {
20
+ return pattern.test(name)
21
+ }
22
+ /* istanbul ignore next */
23
+ return false
24
+ }
25
+
26
+ function getComponentName (opts) {
27
+ return opts && (opts.Ctor.options.name || opts.tag)
28
+ }
29
+
30
+ function getComponentKey (vnode) {
31
+ const { componentOptions, key } = vnode
32
+ return key == null
33
+ ? componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '')
34
+ : key + componentOptions.Ctor.cid
35
+ }
36
+
37
+ function getFirstComponentChild (children) {
38
+ if (Array.isArray(children)) {
39
+ for (let i = 0; i < children.length; i++) {
40
+ const c = children[i]
41
+ if (isDef(c) && (isDef(c.componentOptions) || c.isAsyncPlaceholder)) {
42
+ return c
43
+ }
44
+ }
45
+ }
46
+ }
47
+
48
+ function pruneCache (keepAliveInstance, filter) {
49
+ const { cache, keys, _vnode } = keepAliveInstance
50
+ for (const key in cache) {
51
+ const cachedNode = cache[key]
52
+ if (cachedNode) {
53
+ const name = getComponentName(cachedNode.componentOptions)
54
+ const componentKey = getComponentKey(cachedNode)
55
+ if (name && !filter(name, componentKey)) {
56
+ pruneCacheEntry(cache, key, keys, _vnode)
57
+ }
58
+ }
59
+ }
60
+ }
61
+
62
+ function pruneCacheEntry2 (cache, key, keys) {
63
+ const cached = cache[key]
64
+ if (cached) {
65
+ cached.componentInstance.$destroy()
66
+ }
67
+ cache[key] = null
68
+ remove(keys, key)
69
+ }
70
+
71
+ function pruneCacheEntry (cache, key, keys, current) {
72
+ const cached = cache[key]
73
+ if (cached && (!current || cached.tag !== current.tag)) {
74
+ cached.componentInstance.$destroy()
75
+ }
76
+ cache[key] = null
77
+ remove(keys, key)
78
+ }
79
+
80
+ export default {
81
+ name: 'AKeepAlive',
82
+ abstract: true,
83
+ model: {
84
+ prop: 'clearCaches',
85
+ event: 'clear'
86
+ },
87
+ props: {
88
+ include: patternTypes,
89
+ exclude: patternTypes,
90
+ excludeKeys: patternTypes,
91
+ max: [String, Number],
92
+ clearCaches: Array
93
+ },
94
+ watch: {
95
+ clearCaches: function (val) {
96
+ if (val && val.length > 0) {
97
+ const { cache, keys } = this
98
+ val.forEach(key => {
99
+ pruneCacheEntry2(cache, key, keys)
100
+ })
101
+ this.$emit('clear', [])
102
+ }
103
+ }
104
+ },
105
+
106
+ created () {
107
+ this.cache = Object.create(null)
108
+ this.keys = []
109
+ },
110
+
111
+ destroyed () {
112
+ for (const key in this.cache) {
113
+ pruneCacheEntry(this.cache, key, this.keys)
114
+ }
115
+ },
116
+
117
+ mounted () {
118
+ this.$watch('include', val => {
119
+ pruneCache(this, (name) => matches(val, name))
120
+ })
121
+ this.$watch('exclude', val => {
122
+ pruneCache(this, (name) => !matches(val, name))
123
+ })
124
+ this.$watch('excludeKeys', val => {
125
+ pruneCache(this, (name, key) => !matches(val, key))
126
+ })
127
+ },
128
+
129
+ render () {
130
+ const slot = this.$slots.default
131
+ const vnode = getFirstComponentChild(slot)
132
+ const componentOptions = vnode && vnode.componentOptions
133
+ if (componentOptions) {
134
+ // check pattern
135
+ const name = getComponentName(componentOptions)
136
+ const componentKey = getComponentKey(vnode)
137
+ const { include, exclude, excludeKeys } = this
138
+ if (
139
+ // not included
140
+ (include && (!name || !matches(include, name))) ||
141
+ // excluded
142
+ (exclude && name && matches(exclude, name)) ||
143
+ (excludeKeys && componentKey && matches(excludeKeys, componentKey))
144
+ ) {
145
+ return vnode
146
+ }
147
+
148
+ const { cache, keys } = this
149
+ const key = vnode.key == null
150
+ // same constructor may get registered as different local components
151
+ // so cid alone is not enough (#3269)
152
+ ? componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '')
153
+ : vnode.key + componentOptions.Ctor.cid
154
+ if (cache[key]) {
155
+ vnode.componentInstance = cache[key].componentInstance
156
+ // make current key freshest
157
+ remove(keys, key)
158
+ keys.push(key)
159
+ } else {
160
+ cache[key] = vnode
161
+ keys.push(key)
162
+ // prune oldest entry
163
+ if (this.max && keys.length > parseInt(this.max)) {
164
+ pruneCacheEntry(cache, keys[0], keys, this._vnode)
165
+ }
166
+ }
167
+
168
+ vnode.data.keepAlive = true
169
+ }
170
+ return vnode || (slot && slot[0])
171
+ }
172
+ }
@@ -1,78 +1,22 @@
1
- <template>
2
- <page-toggle-transition :disabled="animate.disabled" :animate="animate.name" :direction="animate.direction">
3
- <a-keep-alive :exclude-keys="excludeKeys" v-if="cachePage" v-model="clearCaches">
4
- <router-view v-if="!refreshing" :key="$route.path" />
5
- </a-keep-alive>
6
- <router-view v-else-if="!refreshing" />
7
- </page-toggle-transition>
8
- </template>
9
-
10
- <script>
11
- import PageToggleTransition from '../components/transition/PageToggleTransition'
12
- import { mapState } from 'vuex'
13
- import AKeepAlive from '@vue2-client/components/cache/AKeepAlive'
14
-
15
- export default {
16
- name: 'BlankView',
17
- components: { PageToggleTransition, AKeepAlive },
18
- data () {
19
- return {
20
- clearCaches: [],
21
- excludeKeys: [],
22
- refreshing: false
23
- }
24
- },
25
- computed: {
26
- ...mapState('setting', ['cachePage', 'animate'])
27
- },
28
- created () {
29
- this.loadCacheConfig(this.$router?.options?.routes)
30
- },
31
- mounted () {
32
- if (window.__MICRO_APP_ENVIRONMENT__) {
33
- window.microApp.addDataListener((data) => {
34
- if (data.type === 'refresh') {
35
- this.refresh()
36
- } else if (data.type === 'clearCache' && data.key) {
37
- this.clearCache(data.key)
38
- }
39
- })
40
- }
41
- },
42
- methods: {
43
- // 加载需要排除缓存的路由配置
44
- loadCacheConfig (routes, pCache = true) {
45
- routes.forEach(item => {
46
- const cacheAble = item.meta?.page?.cacheAble ?? pCache ?? true
47
- if (!cacheAble) {
48
- this.excludeKeys.push(new RegExp(`${item.path.replace(/:[^/]*/g, '[^/]*')}(\\?.*)?\\d*$`))
49
- }
50
- if (item.children) {
51
- this.loadCacheConfig(item.children, cacheAble)
52
- }
53
- })
54
- },
55
- // 刷新页面方法
56
- refresh () {
57
- this.refreshing = true
58
- setTimeout(() => {
59
- this.refreshing = false
60
- }, 200)
61
- },
62
- // 清除特定页面的缓存
63
- clearCache (resultKeys) {
64
- this.clearCaches = resultKeys
65
- }
66
- },
67
- watch: {
68
- '$router.options.routes': function (val) {
69
- this.excludeKeys = []
70
- this.loadCacheConfig(val)
71
- }
72
- }
73
- }
74
- </script>
75
-
76
- <style scoped>
77
-
78
- </style>
1
+ <template>
2
+ <page-toggle-transition :disabled="animate.disabled" :animate="animate.name" :direction="animate.direction">
3
+ <router-view />
4
+ </page-toggle-transition>
5
+ </template>
6
+
7
+ <script>
8
+ import PageToggleTransition from '../components/transition/PageToggleTransition'
9
+ import { mapState } from 'vuex'
10
+
11
+ export default {
12
+ name: 'BlankView',
13
+ components: { PageToggleTransition },
14
+ computed: {
15
+ ...mapState('setting', ['multiPage', 'animate'])
16
+ }
17
+ }
18
+ </script>
19
+
20
+ <style scoped>
21
+
22
+ </style>