@mpxjs/core 2.7.11 → 2.7.25
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.
- package/package.json +2 -2
- package/src/core/mapStore.js +26 -29
- package/src/core/proxy.js +9 -2
- package/src/index.js +0 -22
- package/src/observer/watch.js +7 -0
- package/src/observer/watcher.js +21 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mpxjs/core",
|
|
3
|
-
"version": "2.7.
|
|
3
|
+
"version": "2.7.25",
|
|
4
4
|
"description": "mpx runtime core",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"miniprogram",
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"url": "https://github.com/didi/mpx/issues"
|
|
42
42
|
},
|
|
43
43
|
"sideEffects": false,
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "0bcd275c2d8a3fe33ba383b5c2b53e1988217f9e"
|
|
45
45
|
}
|
package/src/core/mapStore.js
CHANGED
|
@@ -9,23 +9,34 @@ function mapFactory (type, store) {
|
|
|
9
9
|
return function (depPath, maps) {
|
|
10
10
|
maps = normalizeMap(depPath, maps)
|
|
11
11
|
const result = {}
|
|
12
|
-
|
|
12
|
+
Object.entries(maps).forEach(([key, value]) => {
|
|
13
13
|
result[key] = function (payload) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
14
|
+
switch (type) {
|
|
15
|
+
case 'state':
|
|
16
|
+
if (typeof value === 'function') {
|
|
17
|
+
return value.call(this, store.state, store.getters)
|
|
18
|
+
} else {
|
|
19
|
+
let stateVal = getByPath(store.state, value, '', '__NOTFOUND__')
|
|
20
|
+
if (stateVal === '__NOTFOUND__') {
|
|
21
|
+
warn(`Unknown state named [${value}].`)
|
|
22
|
+
stateVal = ''
|
|
23
|
+
}
|
|
24
|
+
return stateVal
|
|
25
|
+
}
|
|
26
|
+
case 'getters':
|
|
27
|
+
let getterVal = getByPath(store.getters, value, '', '__NOTFOUND__')
|
|
28
|
+
if (getterVal === '__NOTFOUND__') {
|
|
29
|
+
warn(`Unknown getter named [${value}].`)
|
|
30
|
+
getterVal = ''
|
|
31
|
+
}
|
|
32
|
+
return getterVal
|
|
33
|
+
case 'mutations':
|
|
34
|
+
return store.commit(value, payload)
|
|
35
|
+
case 'actions':
|
|
36
|
+
return store.dispatch(value, payload)
|
|
26
37
|
}
|
|
27
38
|
}
|
|
28
|
-
}
|
|
39
|
+
})
|
|
29
40
|
return result
|
|
30
41
|
}
|
|
31
42
|
}
|
|
@@ -35,20 +46,6 @@ export default function (store) {
|
|
|
35
46
|
mapGetters: mapFactory('getters', store),
|
|
36
47
|
mapMutations: mapFactory('mutations', store),
|
|
37
48
|
mapActions: mapFactory('actions', store),
|
|
38
|
-
mapState: (
|
|
39
|
-
maps = normalizeMap(depPath, maps)
|
|
40
|
-
const result = {}
|
|
41
|
-
Object.keys(maps).forEach(key => {
|
|
42
|
-
const value = maps[key]
|
|
43
|
-
result[key] = function () {
|
|
44
|
-
if (typeof value === 'function') {
|
|
45
|
-
return value.call(this, store.state, store.getters)
|
|
46
|
-
} else if (typeof value === 'string') {
|
|
47
|
-
return getByPath(store.state, value)
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
})
|
|
51
|
-
return result
|
|
52
|
-
}
|
|
49
|
+
mapState: mapFactory('state', store)
|
|
53
50
|
}
|
|
54
51
|
}
|
package/src/core/proxy.js
CHANGED
|
@@ -47,6 +47,7 @@ export default class MPXProxy {
|
|
|
47
47
|
this.ignoreProxyMap = makeMap(EXPORT_MPX.config.ignoreProxyWhiteList)
|
|
48
48
|
if (__mpx_mode__ !== 'web') {
|
|
49
49
|
this._watchers = []
|
|
50
|
+
this._namedWatchers = {}
|
|
50
51
|
this._watcher = null
|
|
51
52
|
this.localKeysMap = {} // 非props key
|
|
52
53
|
this.renderData = {} // 渲染函数中收集的数据
|
|
@@ -142,6 +143,12 @@ export default class MPXProxy {
|
|
|
142
143
|
// 强制执行render
|
|
143
144
|
this.target.$forceUpdate = (...rest) => this.forceUpdate(...rest)
|
|
144
145
|
this.target.$nextTick = fn => this.nextTick(fn)
|
|
146
|
+
this.target.$getPausableWatchers = () => this._watchers.filter(item => item.pausable)
|
|
147
|
+
this.target.$getWatcherByName = (name) => {
|
|
148
|
+
if (!this._namedWatchers) return null
|
|
149
|
+
return this._namedWatchers[name] || null
|
|
150
|
+
}
|
|
151
|
+
this.target.$getRenderWatcher = () => this._watcher
|
|
145
152
|
}
|
|
146
153
|
}
|
|
147
154
|
|
|
@@ -416,11 +423,11 @@ export default class MPXProxy {
|
|
|
416
423
|
warn(`Failed to execute render function, degrade to full-set-data mode.`, this.options.mpxFileResource, e)
|
|
417
424
|
this.render()
|
|
418
425
|
}
|
|
419
|
-
}, noop)
|
|
426
|
+
}, noop, { pausable: true })
|
|
420
427
|
} else {
|
|
421
428
|
renderWatcher = new Watcher(this, () => {
|
|
422
429
|
this.render()
|
|
423
|
-
}, noop)
|
|
430
|
+
}, noop, { pausable: true })
|
|
424
431
|
}
|
|
425
432
|
this._watcher = renderWatcher
|
|
426
433
|
}
|
package/src/index.js
CHANGED
|
@@ -104,13 +104,6 @@ if (__mpx_mode__ === 'web') {
|
|
|
104
104
|
watch = vm.$watch.bind(vm)
|
|
105
105
|
const set = Vue.set.bind(Vue)
|
|
106
106
|
const del = Vue.delete.bind(Vue)
|
|
107
|
-
const remove = function (...args) {
|
|
108
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
109
|
-
error('$remove will be removed in next minor version, please use $delete instead!', this.$rawOptions && this.$rawOptions.mpxFileResource)
|
|
110
|
-
}
|
|
111
|
-
return del.apply(this, args)
|
|
112
|
-
}
|
|
113
|
-
// todo 补齐web必要api
|
|
114
107
|
APIs = {
|
|
115
108
|
createApp,
|
|
116
109
|
createPage,
|
|
@@ -124,15 +117,10 @@ if (__mpx_mode__ === 'web') {
|
|
|
124
117
|
watch,
|
|
125
118
|
use,
|
|
126
119
|
set,
|
|
127
|
-
remove,
|
|
128
120
|
delete: del,
|
|
129
121
|
getMixin,
|
|
130
122
|
implement
|
|
131
123
|
}
|
|
132
|
-
|
|
133
|
-
InstanceAPIs = {
|
|
134
|
-
$remove: remove
|
|
135
|
-
}
|
|
136
124
|
} else {
|
|
137
125
|
observable = function (obj) {
|
|
138
126
|
observe(obj)
|
|
@@ -140,18 +128,10 @@ if (__mpx_mode__ === 'web') {
|
|
|
140
128
|
}
|
|
141
129
|
|
|
142
130
|
const vm = {}
|
|
143
|
-
|
|
144
131
|
watch = function (expOrFn, cb, options) {
|
|
145
132
|
return watchWithVm(vm, expOrFn, cb, options)
|
|
146
133
|
}
|
|
147
134
|
|
|
148
|
-
const remove = function (...args) {
|
|
149
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
150
|
-
error('$remove will be removed in next minor version, please use $delete instead!', this.$rawOptions && this.$rawOptions.mpxFileResource)
|
|
151
|
-
}
|
|
152
|
-
return del.apply(this, args)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
135
|
APIs = {
|
|
156
136
|
createApp,
|
|
157
137
|
createPage,
|
|
@@ -165,7 +145,6 @@ if (__mpx_mode__ === 'web') {
|
|
|
165
145
|
watch,
|
|
166
146
|
use,
|
|
167
147
|
set,
|
|
168
|
-
remove,
|
|
169
148
|
delete: del,
|
|
170
149
|
getMixin,
|
|
171
150
|
implement
|
|
@@ -173,7 +152,6 @@ if (__mpx_mode__ === 'web') {
|
|
|
173
152
|
|
|
174
153
|
InstanceAPIs = {
|
|
175
154
|
$set: set,
|
|
176
|
-
$remove: remove,
|
|
177
155
|
$delete: del
|
|
178
156
|
}
|
|
179
157
|
}
|
package/src/observer/watch.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isObject, noop } from '../helper/utils'
|
|
2
|
+
import { error } from '../helper/log'
|
|
2
3
|
import Watcher from './watcher'
|
|
3
4
|
import { queueWatcher } from './scheduler'
|
|
4
5
|
|
|
@@ -20,6 +21,12 @@ export function watch (vm, expOrFn, cb, options) {
|
|
|
20
21
|
options = options || {}
|
|
21
22
|
options.user = true
|
|
22
23
|
const watcher = new Watcher(vm, expOrFn, cb, options)
|
|
24
|
+
if (!vm._namedWatchers) vm._namedWatchers = {}
|
|
25
|
+
const name = options.name
|
|
26
|
+
if (name) {
|
|
27
|
+
if (vm._namedWatchers[name]) error(`已存在name=${name} 的 watcher,当存在多个 name 相同 watcher 时仅保留当次创建的 watcher,如需都保留请使用不同的 name!`)
|
|
28
|
+
vm._namedWatchers[name] = watcher
|
|
29
|
+
}
|
|
23
30
|
if (options.immediate) {
|
|
24
31
|
cb.call(vm.target, watcher.value)
|
|
25
32
|
} else if (options.immediateAsync) {
|
package/src/observer/watcher.js
CHANGED
|
@@ -24,6 +24,7 @@ export default class Watcher {
|
|
|
24
24
|
this.deep = !!options.deep
|
|
25
25
|
this.lazy = !!options.lazy
|
|
26
26
|
this.sync = !!options.sync
|
|
27
|
+
this.name = options.name
|
|
27
28
|
} else {
|
|
28
29
|
this.deep = this.lazy = this.sync = false
|
|
29
30
|
}
|
|
@@ -31,6 +32,10 @@ export default class Watcher {
|
|
|
31
32
|
this.id = ++uid // uid for batching
|
|
32
33
|
this.active = true
|
|
33
34
|
this.immediateAsync = false
|
|
35
|
+
// 是否暂停,默认为否
|
|
36
|
+
this.paused = false
|
|
37
|
+
// 是否可被暂停,默认为否,不可被暂停
|
|
38
|
+
this.pausable = !!(options && options.pausable) || false
|
|
34
39
|
this.dirty = this.lazy // for lazy watchers
|
|
35
40
|
this.deps = []
|
|
36
41
|
this.newDeps = []
|
|
@@ -115,7 +120,7 @@ export default class Watcher {
|
|
|
115
120
|
// 支持临时将某个异步watcher修改为sync执行,在模拟setData时使用
|
|
116
121
|
update (sync) {
|
|
117
122
|
/* istanbul ignore else */
|
|
118
|
-
if (this.lazy) {
|
|
123
|
+
if (this.lazy || this.paused) {
|
|
119
124
|
this.dirty = true
|
|
120
125
|
} else if (this.sync || sync) {
|
|
121
126
|
if (sync) dequeueWatcher(this)
|
|
@@ -125,6 +130,21 @@ export default class Watcher {
|
|
|
125
130
|
}
|
|
126
131
|
}
|
|
127
132
|
|
|
133
|
+
pause () {
|
|
134
|
+
// pausable=false 不可暂停
|
|
135
|
+
if (!this.pausable) return
|
|
136
|
+
this.paused = true
|
|
137
|
+
}
|
|
138
|
+
resume () {
|
|
139
|
+
// pausable=false 不可恢复
|
|
140
|
+
if (!this.pausable) return
|
|
141
|
+
// paused 阶段被触发,则 resume 后执行一次run
|
|
142
|
+
this.paused = false
|
|
143
|
+
if (this.dirty) {
|
|
144
|
+
this.dirty = false
|
|
145
|
+
this.run()
|
|
146
|
+
}
|
|
147
|
+
}
|
|
128
148
|
/**
|
|
129
149
|
* Scheduler job interface.
|
|
130
150
|
* Will be called by the scheduler.
|