@mpxjs/core 2.7.18 → 2.7.29
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/@types/index.d.ts +2 -0
- package/@types/mpx-store.d.ts +45 -0
- package/package.json +2 -2
- package/src/core/mapStore.js +69 -29
- package/src/core/proxy.js +39 -12
- package/src/core/transferOptions.js +1 -1
- package/src/observer/computed.js +6 -4
- package/src/observer/watch.js +13 -0
- package/src/platform/patch/ali/getDefaultOptions.js +14 -12
- package/src/platform/patch/swan/getDefaultOptions.js +4 -8
- package/src/platform/patch/web/getDefaultOptions.js +16 -14
- package/src/platform/patch/wx/getDefaultOptions.js +14 -13
package/@types/index.d.ts
CHANGED
package/@types/mpx-store.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
declare namespace MpxStore {
|
|
2
2
|
type UnboxDepField<D, F> = F extends keyof D ? D[F] : {}
|
|
3
3
|
|
|
4
|
+
interface compContext{
|
|
5
|
+
__mpxProxy: object;
|
|
6
|
+
[key: string]: any
|
|
7
|
+
}
|
|
8
|
+
|
|
4
9
|
interface Deps {
|
|
5
10
|
[key: string]: Store | StoreWithThis
|
|
6
11
|
}
|
|
@@ -88,7 +93,21 @@ declare namespace MpxStore {
|
|
|
88
93
|
mapActions(depPath: string, maps: string[]): {
|
|
89
94
|
[key: string]: (...payloads: any[]) => any
|
|
90
95
|
}
|
|
96
|
+
// 下面是新增的异步store的接口类型
|
|
97
|
+
mapStateToInstance<K extends keyof S>(maps: K[], context: compContext): void
|
|
98
|
+
mapStateToInstance(depPath: string, maps: string[], context: compContext): void
|
|
99
|
+
|
|
100
|
+
// mapState support object
|
|
101
|
+
mapStateToInstance<T extends { [key: string]: keyof GetAllMapKeys<S, D, 'state'> }>(obj: T, context: compContext): void
|
|
102
|
+
|
|
103
|
+
mapGettersToInstance<K extends keyof G>(maps: K[], context: compContext): void
|
|
104
|
+
mapGettersToInstance(depPath: string, maps: string[], context: compContext): void
|
|
105
|
+
|
|
106
|
+
mapMutationsToInstance<K extends keyof M>(maps: K[], context: compContext): Pick<GetMutations<M>, K>
|
|
107
|
+
mapMutationsToInstance(depPath: string, maps: string[], context: compContext): void
|
|
91
108
|
|
|
109
|
+
mapActionsToInstance<K extends keyof A>(maps: K[], context: compContext): Pick<GetActions<A>, K>
|
|
110
|
+
mapActionsToInstance(depPath: string, maps: string[], context: compContext): void
|
|
92
111
|
}
|
|
93
112
|
type GetComputedSetKeys<T> = {
|
|
94
113
|
[K in keyof T]: T[K] extends {
|
|
@@ -273,6 +292,32 @@ declare namespace MpxStore {
|
|
|
273
292
|
mapActions<T extends { [key: string]: string }>(obj: T): {
|
|
274
293
|
[I in keyof T]: (...payloads: any[]) => any
|
|
275
294
|
}
|
|
295
|
+
// 异步store api
|
|
296
|
+
mapStateToInstance<K extends keyof S>(maps: K[], context: compContext): void
|
|
297
|
+
mapStateToInstance<T extends string, P extends string>(depPath: P, maps: readonly T[], context: compContext):void
|
|
298
|
+
mapStateToInstance<T extends mapStateFunctionType<S & UnboxDepsField<D, 'state'>, GetComputedType<G> & UnboxDepsField<D, 'getters'>>>(obj: ThisType<any> & T, context: compContext): void
|
|
299
|
+
// Support chain derivation
|
|
300
|
+
mapStateToInstance<T extends { [key: string]: keyof GetAllMapKeys<S, D, 'state'> }>(obj: T, context: compContext): void
|
|
301
|
+
mapStateToInstance<T extends { [key: string]: keyof S }>(obj: T, context: compContext): void
|
|
302
|
+
mapStateToInstance<T extends { [key: string]: string }>(obj: T, context: compContext): void
|
|
303
|
+
|
|
304
|
+
mapGettersToInstance<K extends keyof G>(maps: K[], context: compContext): void
|
|
305
|
+
mapGettersToInstance<T extends string, P extends string>(depPath: P, maps: readonly T[], context: compContext): void
|
|
306
|
+
// Support chain derivation
|
|
307
|
+
mapGettersToInstance<T extends { [key: string]: keyof GetAllMapKeys<GetComputedType<G>, D, 'getters'> }>(obj: T, context: compContext): void
|
|
308
|
+
mapGettersToInstance<T extends { [key: string]: keyof G }>(obj: T, context: compContext): void
|
|
309
|
+
// When importing js in ts file, use this method to be compatible
|
|
310
|
+
mapGettersToInstance<T extends { [key: string]: string }>(obj: T, context: compContext): void
|
|
311
|
+
|
|
312
|
+
mapMutationsToInstance<K extends keyof M>(maps: K[], context: compContext): void
|
|
313
|
+
mapMutationsToInstance<T extends string, P extends string>(depPath: P, maps: readonly T[], context: compContext): void
|
|
314
|
+
mapMutationsToInstance<T extends { [key: string]: keyof M }>(obj: T, context: compContext): void
|
|
315
|
+
mapMutationsToInstance<T extends { [key: string]: string }>(obj: T, context: compContext): void
|
|
316
|
+
|
|
317
|
+
mapActionsToInstance<K extends keyof A>(maps: K[], context: compContext): void
|
|
318
|
+
mapActionsToInstance<T extends string, P extends string>(depPath: P, maps: readonly T[], context: compContext): void
|
|
319
|
+
mapActionsToInstance<T extends { [key: string]: keyof A }>(obj: T, context: compContext): void
|
|
320
|
+
mapActionsToInstance<T extends { [key: string]: string }>(obj: T, context: compContext): void
|
|
276
321
|
}
|
|
277
322
|
|
|
278
323
|
type StoreWithThis<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> = IStoreWithThis<S, G, M, A, D> & CompatibleDispatch
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mpxjs/core",
|
|
3
|
-
"version": "2.7.
|
|
3
|
+
"version": "2.7.29",
|
|
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": "36f20ba8f22e0a1506679237d1e16df5a2e322b6"
|
|
45
45
|
}
|
package/src/core/mapStore.js
CHANGED
|
@@ -3,52 +3,92 @@ import {
|
|
|
3
3
|
getByPath
|
|
4
4
|
} from '../helper/utils'
|
|
5
5
|
|
|
6
|
-
import { warn } from '../helper/log'
|
|
6
|
+
import { warn, error } from '../helper/log'
|
|
7
7
|
|
|
8
8
|
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
|
}
|
|
32
43
|
|
|
44
|
+
function checkMapInstance (args) {
|
|
45
|
+
const context = args[args.length - 1]
|
|
46
|
+
const isValid = context && typeof context === 'object' && context.__mpxProxy
|
|
47
|
+
if (!isValid) {
|
|
48
|
+
error(`调用map**ToInstance时必须传入当前component实例this`)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
args.splice(-1)
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
restParams: args,
|
|
55
|
+
context
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
33
59
|
export default function (store) {
|
|
34
60
|
return {
|
|
35
61
|
mapGetters: mapFactory('getters', store),
|
|
36
62
|
mapMutations: mapFactory('mutations', store),
|
|
37
63
|
mapActions: mapFactory('actions', store),
|
|
38
|
-
mapState: (
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
64
|
+
mapState: mapFactory('state', store),
|
|
65
|
+
// 以下是map**ToInstance用于异步store的,参数args:depPath, maps, context
|
|
66
|
+
mapStateToInstance: (...args) => {
|
|
67
|
+
const { context, restParams } = checkMapInstance(args)
|
|
68
|
+
const mapStateFun = mapFactory('state', store)
|
|
69
|
+
const result = mapStateFun(...restParams)
|
|
70
|
+
// 将result挂载到mpxProxy实例属性上
|
|
71
|
+
context.__mpxProxy.options.computed = context.__mpxProxy.options.computed || {}
|
|
72
|
+
Object.assign(context.__mpxProxy.options.computed, result)
|
|
73
|
+
},
|
|
74
|
+
mapGettersToInstance: (...args) => {
|
|
75
|
+
const { context, restParams } = checkMapInstance(args)
|
|
76
|
+
const mapGetFun = mapFactory('getters', store)
|
|
77
|
+
const result = mapGetFun(...restParams)
|
|
78
|
+
context.__mpxProxy.options.computed = context.__mpxProxy.options.computed || {}
|
|
79
|
+
Object.assign(context.__mpxProxy.options.computed, result)
|
|
80
|
+
},
|
|
81
|
+
mapMutationsToInstance: (...args) => {
|
|
82
|
+
const { context, restParams } = checkMapInstance(args)
|
|
83
|
+
const mapMutationFun = mapFactory('mutations', store)
|
|
84
|
+
const result = mapMutationFun(...restParams)
|
|
85
|
+
Object.assign(context, result)
|
|
86
|
+
},
|
|
87
|
+
mapActionsToInstance: (...args) => {
|
|
88
|
+
const { context, restParams } = checkMapInstance(args)
|
|
89
|
+
const mapActionFun = mapFactory('actions', store)
|
|
90
|
+
const result = mapActionFun(...restParams)
|
|
91
|
+
Object.assign(context, result)
|
|
52
92
|
}
|
|
53
93
|
}
|
|
54
94
|
}
|
package/src/core/proxy.js
CHANGED
|
@@ -41,13 +41,14 @@ export default class MPXProxy {
|
|
|
41
41
|
this.uid = uid++
|
|
42
42
|
this.name = options.name || ''
|
|
43
43
|
this.options = options
|
|
44
|
-
//
|
|
45
|
-
this.state =
|
|
44
|
+
// beforeCreate -> created -> mounted -> destroyed
|
|
45
|
+
this.state = BEFORECREATE
|
|
46
46
|
this.lockTask = asyncLock()
|
|
47
47
|
this.ignoreProxyMap = makeMap(EXPORT_MPX.config.ignoreProxyWhiteList)
|
|
48
48
|
if (__mpx_mode__ !== 'web') {
|
|
49
49
|
this._watchers = []
|
|
50
50
|
this._namedWatchers = {}
|
|
51
|
+
this._computedWatchers = {}
|
|
51
52
|
this._watcher = null
|
|
52
53
|
this.localKeysMap = {} // 非props key
|
|
53
54
|
this.renderData = {} // 渲染函数中收集的数据
|
|
@@ -62,13 +63,27 @@ export default class MPXProxy {
|
|
|
62
63
|
this.initApi()
|
|
63
64
|
this.callUserHook(BEFORECREATE)
|
|
64
65
|
if (__mpx_mode__ !== 'web') {
|
|
65
|
-
this.initState(
|
|
66
|
+
this.initState()
|
|
66
67
|
}
|
|
67
68
|
this.state = CREATED
|
|
68
69
|
this.callUserHook(CREATED, params)
|
|
69
70
|
if (__mpx_mode__ !== 'web') {
|
|
70
|
-
|
|
71
|
-
|
|
71
|
+
this.initRender()
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
reCreated (params) {
|
|
76
|
+
const options = this.options
|
|
77
|
+
this.state = BEFORECREATE
|
|
78
|
+
this.callUserHook(BEFORECREATE)
|
|
79
|
+
if (__mpx_mode__ !== 'web') {
|
|
80
|
+
this.initComputed(options.computed, true)
|
|
81
|
+
this.initWatch(options.watch)
|
|
82
|
+
}
|
|
83
|
+
this.state = CREATED
|
|
84
|
+
this.callUserHook(CREATED, params)
|
|
85
|
+
if (__mpx_mode__ !== 'web') {
|
|
86
|
+
this.initRender()
|
|
72
87
|
}
|
|
73
88
|
}
|
|
74
89
|
|
|
@@ -118,6 +133,10 @@ export default class MPXProxy {
|
|
|
118
133
|
this.callUserHook(DESTROYED)
|
|
119
134
|
}
|
|
120
135
|
|
|
136
|
+
isDestroyed () {
|
|
137
|
+
return this.state === DESTROYED
|
|
138
|
+
}
|
|
139
|
+
|
|
121
140
|
initApi () {
|
|
122
141
|
// 挂载扩展属性到实例上
|
|
123
142
|
proxy(this.target, this.options.proto, Object.keys(this.options.proto), true, (key) => {
|
|
@@ -168,10 +187,15 @@ export default class MPXProxy {
|
|
|
168
187
|
this.initWatch(options.watch)
|
|
169
188
|
}
|
|
170
189
|
|
|
171
|
-
initComputed (computedOpt) {
|
|
190
|
+
initComputed (computedOpt, reInit) {
|
|
172
191
|
if (computedOpt) {
|
|
173
|
-
|
|
174
|
-
|
|
192
|
+
if (reInit) {
|
|
193
|
+
// target传递null以跳过computed挂载,仅重新初始化watchers
|
|
194
|
+
initComputed(this, null, computedOpt)
|
|
195
|
+
} else {
|
|
196
|
+
this.collectLocalKeys(computedOpt)
|
|
197
|
+
initComputed(this, this.data, computedOpt)
|
|
198
|
+
}
|
|
175
199
|
}
|
|
176
200
|
}
|
|
177
201
|
|
|
@@ -194,7 +218,9 @@ export default class MPXProxy {
|
|
|
194
218
|
})
|
|
195
219
|
Object.assign(this.data, dataFn.call(this.target))
|
|
196
220
|
}
|
|
221
|
+
// 此时data中不包括props数据
|
|
197
222
|
this.collectLocalKeys(this.data)
|
|
223
|
+
// 将props数据合并到data中
|
|
198
224
|
Object.keys(initialData).forEach((key) => {
|
|
199
225
|
if (!hasOwn(this.data, key)) {
|
|
200
226
|
// 除了data函数返回的数据外深拷贝切断引用关系,避免后续watch由于小程序内部对data赋值重复触发watch
|
|
@@ -263,6 +289,7 @@ export default class MPXProxy {
|
|
|
263
289
|
while (i--) {
|
|
264
290
|
this._watchers[i].teardown()
|
|
265
291
|
}
|
|
292
|
+
this._watchers.length = 0
|
|
266
293
|
}
|
|
267
294
|
|
|
268
295
|
render () {
|
|
@@ -414,9 +441,10 @@ export default class MPXProxy {
|
|
|
414
441
|
}
|
|
415
442
|
|
|
416
443
|
initRender () {
|
|
417
|
-
|
|
444
|
+
if (this.options.__nativeRender__) return this.doRender()
|
|
445
|
+
|
|
418
446
|
if (this.target.__injectedRender) {
|
|
419
|
-
|
|
447
|
+
this._watcher = new Watcher(this, () => {
|
|
420
448
|
try {
|
|
421
449
|
return this.target.__injectedRender()
|
|
422
450
|
} catch (e) {
|
|
@@ -425,11 +453,10 @@ export default class MPXProxy {
|
|
|
425
453
|
}
|
|
426
454
|
}, noop, { pausable: true })
|
|
427
455
|
} else {
|
|
428
|
-
|
|
456
|
+
this._watcher = new Watcher(this, () => {
|
|
429
457
|
this.render()
|
|
430
458
|
}, noop, { pausable: true })
|
|
431
459
|
}
|
|
432
|
-
this._watcher = renderWatcher
|
|
433
460
|
}
|
|
434
461
|
|
|
435
462
|
forceUpdate (data, options, callback) {
|
|
@@ -17,7 +17,7 @@ export default function transferOptions (options, type) {
|
|
|
17
17
|
}
|
|
18
18
|
if (currentInject && currentInject.injectComputed) {
|
|
19
19
|
// 编译计算属性注入
|
|
20
|
-
options.computed = Object.assign({},
|
|
20
|
+
options.computed = Object.assign({}, currentInject.injectComputed, options.computed)
|
|
21
21
|
}
|
|
22
22
|
if (currentInject && currentInject.injectOptions) {
|
|
23
23
|
// 编译option注入,优先微信中的单独配置
|
package/src/observer/computed.js
CHANGED
|
@@ -20,10 +20,12 @@ export function initComputed (vm, target, computed) {
|
|
|
20
20
|
noop,
|
|
21
21
|
{ lazy: true }
|
|
22
22
|
)
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
if (target) {
|
|
24
|
+
if (!(key in target)) {
|
|
25
|
+
defineComputed(vm, target, key, userDef)
|
|
26
|
+
} else {
|
|
27
|
+
error(`The computed key [${key}] is duplicated with data/props, please check.`, vm.options.mpxFileResource)
|
|
28
|
+
}
|
|
27
29
|
}
|
|
28
30
|
}
|
|
29
31
|
}
|
package/src/observer/watch.js
CHANGED
|
@@ -20,6 +20,19 @@ export function watch (vm, expOrFn, cb, options) {
|
|
|
20
20
|
|
|
21
21
|
options = options || {}
|
|
22
22
|
options.user = true
|
|
23
|
+
|
|
24
|
+
if (options.once) {
|
|
25
|
+
const _cb = cb
|
|
26
|
+
const onceCb = typeof options.once === 'function'
|
|
27
|
+
? options.once
|
|
28
|
+
: function () { return true }
|
|
29
|
+
cb = function (...args) {
|
|
30
|
+
const res = onceCb.apply(this, args)
|
|
31
|
+
if (res) watcher.teardown()
|
|
32
|
+
_cb.apply(this, args)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
23
36
|
const watcher = new Watcher(vm, expOrFn, cb, options)
|
|
24
37
|
if (!vm._namedWatchers) vm._namedWatchers = {}
|
|
25
38
|
const name = options.name
|
|
@@ -84,23 +84,24 @@ function filterOptions (options, type) {
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
function initProxy (context, rawOptions, currentInject, params) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
87
|
+
if (!context.__mpxProxy) {
|
|
88
|
+
// 提供代理对象需要的api
|
|
89
|
+
transformApiForProxy(context, currentInject)
|
|
90
|
+
// 缓存options
|
|
91
|
+
context.$rawOptions = rawOptions
|
|
92
|
+
// 创建proxy对象
|
|
93
|
+
context.__mpxProxy = new MPXProxy(rawOptions, context)
|
|
94
|
+
context.__mpxProxy.created(params)
|
|
95
|
+
} else if (context.__mpxProxy.isDestroyed()) {
|
|
96
|
+
context.__mpxProxy.reCreated(params)
|
|
97
|
+
}
|
|
95
98
|
}
|
|
96
99
|
|
|
97
100
|
export function getDefaultOptions (type, { rawOptions = {}, currentInject }) {
|
|
98
101
|
const hookNames = type === 'component' ? ['onInit', 'didMount', 'didUnmount'] : ['onLoad', 'onReady', 'onUnload']
|
|
99
102
|
const rootMixins = [{
|
|
100
103
|
[hookNames[0]] (...params) {
|
|
101
|
-
|
|
102
|
-
initProxy(this, rawOptions, currentInject, params)
|
|
103
|
-
}
|
|
104
|
+
initProxy(this, rawOptions, currentInject, params)
|
|
104
105
|
},
|
|
105
106
|
deriveDataFromProps (nextProps) {
|
|
106
107
|
if (this.__mpxProxy && this.__mpxProxy.isMounted() && nextProps && nextProps !== this.props) {
|
|
@@ -130,6 +131,7 @@ export function getDefaultOptions (type, { rawOptions = {}, currentInject }) {
|
|
|
130
131
|
},
|
|
131
132
|
didUpdate () {
|
|
132
133
|
if (this.__mpxProxy) {
|
|
134
|
+
// todo: lockTask必要性待验证,属性更新触发自身setData时,updated执行与wx对齐,updated触发机制也考虑与wx对齐(props update && setData callback)
|
|
133
135
|
this.__mpxProxy.lockTask(() => {
|
|
134
136
|
this.__mpxProxy.updated()
|
|
135
137
|
})
|
|
@@ -143,7 +145,7 @@ export function getDefaultOptions (type, { rawOptions = {}, currentInject }) {
|
|
|
143
145
|
}
|
|
144
146
|
},
|
|
145
147
|
[hookNames[2]] () {
|
|
146
|
-
this.__mpxProxy
|
|
148
|
+
if (this.__mpxProxy) this.__mpxProxy.destroyed()
|
|
147
149
|
}
|
|
148
150
|
}]
|
|
149
151
|
rawOptions.mixins = rawOptions.mixins ? rootMixins.concat(rawOptions.mixins) : rootMixins
|
|
@@ -10,24 +10,20 @@ export function getDefaultOptions (type, { rawOptions = {}, currentInject }) {
|
|
|
10
10
|
|
|
11
11
|
const rootMixin = {
|
|
12
12
|
[hookNames[0]] (...params) {
|
|
13
|
-
|
|
14
|
-
initProxy(this, rawOptions, currentInject, params)
|
|
15
|
-
}
|
|
13
|
+
initProxy(this, rawOptions, currentInject, params)
|
|
16
14
|
},
|
|
17
15
|
[hookNames[1]] () {
|
|
18
|
-
this.__mpxProxy
|
|
16
|
+
if (this.__mpxProxy) this.__mpxProxy.mounted()
|
|
19
17
|
},
|
|
20
18
|
[hookNames[2]] () {
|
|
21
|
-
this.__mpxProxy
|
|
19
|
+
if (this.__mpxProxy) this.__mpxProxy.destroyed()
|
|
22
20
|
}
|
|
23
21
|
}
|
|
24
22
|
|
|
25
23
|
// 如构造页面,优先使用onInit进行初始化
|
|
26
24
|
if (type === 'page') {
|
|
27
25
|
rootMixin.onInit = function (...params) {
|
|
28
|
-
|
|
29
|
-
initProxy(this, rawOptions, currentInject, params)
|
|
30
|
-
}
|
|
26
|
+
initProxy(this, rawOptions, currentInject, params)
|
|
31
27
|
}
|
|
32
28
|
}
|
|
33
29
|
|
|
@@ -24,31 +24,33 @@ function filterOptions (options) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
function initProxy (context, rawOptions, params) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
if (!context.__mpxProxy) {
|
|
28
|
+
// 缓存options
|
|
29
|
+
context.$rawOptions = rawOptions
|
|
30
|
+
// 创建proxy对象
|
|
31
|
+
context.__mpxProxy = new MPXProxy(rawOptions, context)
|
|
32
|
+
context.__mpxProxy.created(params)
|
|
33
|
+
} else if (context.__mpxProxy.isDestroyed()) {
|
|
34
|
+
context.__mpxProxy.reCreated(params)
|
|
35
|
+
}
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
export function getDefaultOptions (type, { rawOptions = {} }) {
|
|
36
39
|
const rootMixins = [{
|
|
37
40
|
created () {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
41
|
+
const query = (global.__mpxRouter && global.__mpxRouter.currentRoute && global.__mpxRouter.currentRoute.query) || {}
|
|
42
|
+
initProxy(this, rawOptions, [query])
|
|
43
|
+
// web中单独触发onLoad
|
|
44
|
+
this.onLoad && this.onLoad(query)
|
|
43
45
|
},
|
|
44
46
|
mounted () {
|
|
45
|
-
this.__mpxProxy
|
|
47
|
+
if (this.__mpxProxy) this.__mpxProxy.mounted()
|
|
46
48
|
},
|
|
47
49
|
updated () {
|
|
48
|
-
this.__mpxProxy
|
|
50
|
+
if (this.__mpxProxy) this.__mpxProxy.updated()
|
|
49
51
|
},
|
|
50
52
|
destroyed () {
|
|
51
|
-
this.__mpxProxy
|
|
53
|
+
if (this.__mpxProxy) this.__mpxProxy.destroyed()
|
|
52
54
|
}
|
|
53
55
|
}]
|
|
54
56
|
// 为了在builtMixin中可以使用某些rootMixin实现的特性(如数据响应等),此处builtInMixin在rootMixin之后执行,但是当builtInMixin使用存在对应内建生命周期的目标平台声明周期写法时,可能会出现用户生命周期比builtInMixin中的生命周期先执行的情况,为了避免这种情况发生,builtInMixin应该尽可能使用内建生命周期来编写
|
|
@@ -117,14 +117,17 @@ export function filterOptions (options) {
|
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
export function initProxy (context, rawOptions, currentInject, params) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
120
|
+
if (!context.__mpxProxy) {
|
|
121
|
+
// 提供代理对象需要的api
|
|
122
|
+
transformApiForProxy(context, currentInject)
|
|
123
|
+
// 缓存options
|
|
124
|
+
context.$rawOptions = rawOptions
|
|
125
|
+
// 创建proxy对象
|
|
126
|
+
context.__mpxProxy = new MPXProxy(rawOptions, context)
|
|
127
|
+
context.__mpxProxy.created(params)
|
|
128
|
+
} else if (context.__mpxProxy.isDestroyed()) {
|
|
129
|
+
context.__mpxProxy.reCreated(params)
|
|
130
|
+
}
|
|
128
131
|
}
|
|
129
132
|
|
|
130
133
|
export function getDefaultOptions (type, { rawOptions = {}, currentInject }) {
|
|
@@ -135,15 +138,13 @@ export function getDefaultOptions (type, { rawOptions = {}, currentInject }) {
|
|
|
135
138
|
}
|
|
136
139
|
const rootMixins = [{
|
|
137
140
|
[hookNames[0]] (...params) {
|
|
138
|
-
|
|
139
|
-
initProxy(this, rawOptions, currentInject, params)
|
|
140
|
-
}
|
|
141
|
+
initProxy(this, rawOptions, currentInject, params)
|
|
141
142
|
},
|
|
142
143
|
[hookNames[1]] () {
|
|
143
|
-
this.__mpxProxy
|
|
144
|
+
if (this.__mpxProxy) this.__mpxProxy.mounted()
|
|
144
145
|
},
|
|
145
146
|
[hookNames[2]] () {
|
|
146
|
-
this.__mpxProxy
|
|
147
|
+
if (this.__mpxProxy) this.__mpxProxy.destroyed()
|
|
147
148
|
}
|
|
148
149
|
}]
|
|
149
150
|
rawOptions.mixins = rawOptions.mixins ? rootMixins.concat(rawOptions.mixins) : rootMixins
|