@mpxjs/core 2.7.52 → 2.8.0-beta.2
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 +342 -27
- package/package.json +10 -5
- package/src/convertor/convertor.js +2 -2
- package/src/convertor/mergeLifecycle.js +4 -4
- package/src/convertor/wxToAli.js +3 -4
- package/src/convertor/wxToSwan.js +2 -2
- package/src/convertor/wxToTt.js +1 -10
- package/src/convertor/wxToWeb.js +14 -7
- package/src/core/implement.js +2 -2
- package/src/core/injectMixins.js +1 -1
- package/src/core/innerLifecycle.js +15 -2
- package/src/core/mergeOptions.js +11 -5
- package/src/core/proxy.js +343 -229
- package/src/core/transferOptions.js +5 -2
- package/src/helper/const.js +10 -0
- package/src/index.js +73 -147
- package/src/observer/array.js +12 -17
- package/src/observer/computed.js +27 -56
- package/src/observer/dep.js +1 -1
- package/src/observer/effect.js +113 -0
- package/src/observer/effectScope.js +109 -0
- package/src/observer/{index.js → reactive.js} +74 -70
- package/src/observer/ref.js +97 -0
- package/src/observer/scheduler.js +171 -56
- package/src/observer/watch.js +163 -39
- package/src/platform/builtInMixins/i18nMixin.js +238 -31
- package/src/platform/builtInMixins/pageScrollMixin.web.js +4 -5
- package/src/platform/builtInMixins/pageStatusMixin.js +76 -54
- package/src/platform/builtInMixins/pageStatusMixin.web.js +35 -22
- package/src/platform/builtInMixins/proxyEventMixin.js +40 -22
- package/src/platform/builtInMixins/proxyEventMixin.web.js +16 -24
- package/src/platform/builtInMixins/refsMixin.js +82 -73
- package/src/platform/builtInMixins/refsMixin.web.js +0 -47
- package/src/platform/builtInMixins/relationsMixin.js +10 -9
- package/src/platform/builtInMixins/renderHelperMixin.js +1 -1
- package/src/platform/builtInMixins/showMixin.js +1 -1
- package/src/platform/createApp.js +5 -5
- package/src/platform/export/api.js +23 -0
- package/src/platform/export/api.web.js +26 -0
- package/src/platform/export/index.js +45 -0
- package/src/platform/export/index.web.js +36 -0
- package/src/platform/index.js +1 -5
- package/src/platform/patch/ali/getDefaultOptions.js +33 -31
- package/src/platform/patch/ali/lifecycle.js +21 -13
- package/src/platform/patch/builtInKeysMap.js +2 -1
- package/src/platform/patch/index.js +4 -9
- package/src/platform/patch/swan/getDefaultOptions.js +3 -3
- package/src/platform/patch/swan/lifecycle.js +17 -14
- package/src/platform/patch/web/getDefaultOptions.js +40 -16
- package/src/platform/patch/web/lifecycle.js +6 -3
- package/src/platform/patch/wx/getDefaultOptions.js +38 -31
- package/src/platform/patch/wx/lifecycle.js +18 -11
- package/src/runtime/createFactory.js +6 -2
- package/src/vue.web.js +3 -0
- package/src/vuePlugin.js +31 -0
- package/src/core/createStore.js +0 -241
- package/src/core/mapStore.js +0 -94
- package/src/helper/env.js +0 -20
- package/src/helper/getByPath.js +0 -127
- package/src/helper/log.js +0 -31
- package/src/helper/utils.js +0 -652
- package/src/observer/watcher.js +0 -244
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { hasOwn } from '
|
|
2
|
-
import
|
|
1
|
+
import { hasOwn } from '@mpxjs/utils'
|
|
2
|
+
import MpxProxy from '../../../core/proxy'
|
|
3
3
|
import builtInKeysMap from '../builtInKeysMap'
|
|
4
4
|
import mergeOptions from '../../../core/mergeOptions'
|
|
5
|
-
import { queueWatcher } from '../../../observer/scheduler'
|
|
6
5
|
|
|
7
6
|
function transformProperties (properties) {
|
|
8
7
|
if (!properties) {
|
|
@@ -24,15 +23,10 @@ function transformProperties (properties) {
|
|
|
24
23
|
} else {
|
|
25
24
|
newFiled = Object.assign({}, rawFiled)
|
|
26
25
|
}
|
|
27
|
-
newFiled.observer = function (value
|
|
26
|
+
newFiled.observer = function (value) {
|
|
28
27
|
if (this.__mpxProxy) {
|
|
29
28
|
this[key] = value
|
|
30
|
-
|
|
31
|
-
// 只有当当前没有渲染任务时,属性更新才需要单独触发updated,否则可以由渲染任务结束后触发updated
|
|
32
|
-
if (this.__mpxProxy.curRenderTask && this.__mpxProxy.curRenderTask.state === 'finished') {
|
|
33
|
-
this.__mpxProxy.updated()
|
|
34
|
-
}
|
|
35
|
-
})
|
|
29
|
+
this.__mpxProxy.propsUpdated()
|
|
36
30
|
}
|
|
37
31
|
}
|
|
38
32
|
newProps[key] = newFiled
|
|
@@ -41,27 +35,27 @@ function transformProperties (properties) {
|
|
|
41
35
|
}
|
|
42
36
|
|
|
43
37
|
function transformApiForProxy (context, currentInject) {
|
|
44
|
-
const rawSetData = context.setData
|
|
38
|
+
const rawSetData = context.setData
|
|
45
39
|
Object.defineProperties(context, {
|
|
46
40
|
setData: {
|
|
47
41
|
get () {
|
|
48
42
|
return function (data, callback) {
|
|
49
|
-
return
|
|
43
|
+
return context.__mpxProxy.forceUpdate(data, { sync: true }, callback)
|
|
50
44
|
}
|
|
51
45
|
},
|
|
52
46
|
configurable: true
|
|
53
47
|
},
|
|
54
|
-
|
|
48
|
+
__getProps: {
|
|
55
49
|
get () {
|
|
56
50
|
return (options) => {
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
if (hasOwn(
|
|
61
|
-
|
|
51
|
+
const props = {}
|
|
52
|
+
const validProps = Object.assign({}, options.properties, options.props)
|
|
53
|
+
Object.keys(context.data).forEach((key) => {
|
|
54
|
+
if (hasOwn(validProps, key)) {
|
|
55
|
+
props[key] = context.data[key]
|
|
62
56
|
}
|
|
63
|
-
}
|
|
64
|
-
return
|
|
57
|
+
})
|
|
58
|
+
return props
|
|
65
59
|
}
|
|
66
60
|
},
|
|
67
61
|
configurable: false
|
|
@@ -73,13 +67,27 @@ function transformApiForProxy (context, currentInject) {
|
|
|
73
67
|
configurable: false
|
|
74
68
|
}
|
|
75
69
|
})
|
|
70
|
+
|
|
71
|
+
// // 抹平处理tt不支持驼峰事件名的问题
|
|
72
|
+
// if (__mpx_mode__ === 'tt') {
|
|
73
|
+
// const rawTriggerEvent = context.triggerEvent
|
|
74
|
+
// Object.defineProperty(context, 'triggerEvent', {
|
|
75
|
+
// get () {
|
|
76
|
+
// return function (eventName, eventDetail) {
|
|
77
|
+
// return rawTriggerEvent.call(this, eventName.toLowerCase(), eventDetail)
|
|
78
|
+
// }
|
|
79
|
+
// },
|
|
80
|
+
// configurable: true
|
|
81
|
+
// })
|
|
82
|
+
// }
|
|
83
|
+
|
|
76
84
|
// 绑定注入的render
|
|
77
85
|
if (currentInject) {
|
|
78
86
|
if (currentInject.render) {
|
|
79
87
|
Object.defineProperties(context, {
|
|
80
88
|
__injectedRender: {
|
|
81
89
|
get () {
|
|
82
|
-
return currentInject.render
|
|
90
|
+
return currentInject.render
|
|
83
91
|
},
|
|
84
92
|
configurable: false
|
|
85
93
|
}
|
|
@@ -116,17 +124,16 @@ export function filterOptions (options) {
|
|
|
116
124
|
return newOptions
|
|
117
125
|
}
|
|
118
126
|
|
|
119
|
-
export function initProxy (context, rawOptions, currentInject
|
|
127
|
+
export function initProxy (context, rawOptions, currentInject) {
|
|
120
128
|
if (!context.__mpxProxy) {
|
|
121
129
|
// 提供代理对象需要的api
|
|
122
130
|
transformApiForProxy(context, currentInject)
|
|
123
|
-
// 缓存options
|
|
124
|
-
context.$rawOptions = rawOptions
|
|
125
131
|
// 创建proxy对象
|
|
126
|
-
context.__mpxProxy = new
|
|
127
|
-
context.__mpxProxy.created(
|
|
128
|
-
} else if (context.__mpxProxy.
|
|
129
|
-
context.__mpxProxy
|
|
132
|
+
context.__mpxProxy = new MpxProxy(rawOptions, context)
|
|
133
|
+
context.__mpxProxy.created()
|
|
134
|
+
} else if (context.__mpxProxy.isUnmounted()) {
|
|
135
|
+
context.__mpxProxy = new MpxProxy(rawOptions, context, true)
|
|
136
|
+
context.__mpxProxy.created()
|
|
130
137
|
}
|
|
131
138
|
}
|
|
132
139
|
|
|
@@ -137,14 +144,14 @@ export function getDefaultOptions (type, { rawOptions = {}, currentInject }) {
|
|
|
137
144
|
hookNames = ['onLoad', 'onReady', 'onUnload']
|
|
138
145
|
}
|
|
139
146
|
const rootMixins = [{
|
|
140
|
-
[hookNames[0]] (
|
|
141
|
-
initProxy(this, rawOptions, currentInject
|
|
147
|
+
[hookNames[0]] () {
|
|
148
|
+
initProxy(this, rawOptions, currentInject)
|
|
142
149
|
},
|
|
143
150
|
[hookNames[1]] () {
|
|
144
151
|
if (this.__mpxProxy) this.__mpxProxy.mounted()
|
|
145
152
|
},
|
|
146
153
|
[hookNames[2]] () {
|
|
147
|
-
if (this.__mpxProxy) this.__mpxProxy.
|
|
154
|
+
if (this.__mpxProxy) this.__mpxProxy.unmounted()
|
|
148
155
|
}
|
|
149
156
|
}]
|
|
150
157
|
rawOptions.mixins = rawOptions.mixins ? rootMixins.concat(rawOptions.mixins) : rootMixins
|
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
CREATED,
|
|
3
|
+
UNMOUNTED,
|
|
4
|
+
MOUNTED,
|
|
5
|
+
ONSHOW,
|
|
6
|
+
ONHIDE,
|
|
7
|
+
ONLOAD
|
|
8
|
+
} from '../../../core/innerLifecycle'
|
|
2
9
|
|
|
3
10
|
const APP_HOOKS = [
|
|
4
11
|
'onLaunch',
|
|
@@ -18,33 +25,33 @@ const PAGE_HOOKS = [
|
|
|
18
25
|
'onUnload',
|
|
19
26
|
'onPullDownRefresh',
|
|
20
27
|
'onReachBottom',
|
|
21
|
-
'onShareAppMessage',
|
|
22
28
|
'onPageScroll',
|
|
29
|
+
'onAddToFavorites',
|
|
30
|
+
'onShareAppMessage',
|
|
31
|
+
'onShareTimeline',
|
|
32
|
+
'onResize',
|
|
23
33
|
'onTabItemTap',
|
|
24
|
-
'
|
|
34
|
+
'onSaveExitState'
|
|
25
35
|
]
|
|
26
36
|
|
|
27
37
|
const COMPONENT_HOOKS = [
|
|
28
|
-
'beforeCreate',
|
|
29
38
|
'created',
|
|
30
39
|
'attached',
|
|
31
40
|
'ready',
|
|
32
41
|
'moved',
|
|
33
42
|
'detached',
|
|
34
|
-
'updated',
|
|
35
43
|
'pageShow',
|
|
36
|
-
'pageHide'
|
|
37
|
-
'definitionFilter'
|
|
44
|
+
'pageHide'
|
|
38
45
|
]
|
|
39
46
|
|
|
40
47
|
export const lifecycleProxyMap = {
|
|
41
|
-
[BEFORECREATE]: ['beforeCreate'],
|
|
42
48
|
// 类微信平台中onLoad不能代理到CREATED上,否则Component构造页面时无法获取页面参数
|
|
43
49
|
[CREATED]: ['created', 'attached'],
|
|
44
|
-
[UPDATED]: ['updated'],
|
|
45
|
-
[BEFOREMOUNT]: ['beforeMount'],
|
|
46
50
|
[MOUNTED]: ['ready', 'onReady'],
|
|
47
|
-
[
|
|
51
|
+
[UNMOUNTED]: ['detached', 'onUnload'],
|
|
52
|
+
[ONSHOW]: ['pageShow', 'onShow'],
|
|
53
|
+
[ONHIDE]: ['pageHide', 'onHide'],
|
|
54
|
+
[ONLOAD]: ['onLoad']
|
|
48
55
|
}
|
|
49
56
|
|
|
50
57
|
export const LIFECYCLE = {
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
const
|
|
1
|
+
const factoryMap = {
|
|
2
|
+
App: require('../index').createApp,
|
|
3
|
+
Page: require('../index').createPage,
|
|
4
|
+
Component: require('../index').createComponent
|
|
5
|
+
}
|
|
2
6
|
|
|
3
7
|
module.exports = (type) => (...args) => {
|
|
4
8
|
if (type === 'Behavior') {
|
|
@@ -13,5 +17,5 @@ module.exports = (type) => (...args) => {
|
|
|
13
17
|
}
|
|
14
18
|
return args[0]
|
|
15
19
|
}
|
|
16
|
-
return
|
|
20
|
+
return factoryMap[type].apply(null, args.concat({ isNative: true }))
|
|
17
21
|
}
|
package/src/vue.web.js
CHANGED
package/src/vuePlugin.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { walkChildren, parseSelector, error } from '@mpxjs/utils'
|
|
2
|
+
import * as webApi from '@mpxjs/api-proxy/src/web/api'
|
|
3
|
+
|
|
4
|
+
export default function install (Vue) {
|
|
5
|
+
Vue.prototype.triggerEvent = function (eventName, eventDetail) {
|
|
6
|
+
return this.$emit(eventName, {
|
|
7
|
+
type: eventName,
|
|
8
|
+
detail: eventDetail
|
|
9
|
+
})
|
|
10
|
+
}
|
|
11
|
+
Vue.prototype.selectComponent = function (selector, all) {
|
|
12
|
+
const result = []
|
|
13
|
+
if (/[>\s]/.test(selector)) {
|
|
14
|
+
const location = this.__mpxProxy.options.mpxFileResource
|
|
15
|
+
error('The selectComponent or selectAllComponents only supports the basic selector, the relation selector is not supported.', location)
|
|
16
|
+
} else {
|
|
17
|
+
const selectorGroups = parseSelector(selector)
|
|
18
|
+
walkChildren(this, selectorGroups, this, result, all)
|
|
19
|
+
}
|
|
20
|
+
return all ? result : result[0]
|
|
21
|
+
}
|
|
22
|
+
Vue.prototype.selectAllComponents = function (selector) {
|
|
23
|
+
return this.selectComponent(selector, true)
|
|
24
|
+
}
|
|
25
|
+
Vue.prototype.createSelectorQuery = function () {
|
|
26
|
+
return webApi.createSelectorQuery().in(this)
|
|
27
|
+
}
|
|
28
|
+
Vue.prototype.createIntersectionObserver = function (component, options) {
|
|
29
|
+
return webApi.createIntersectionObserver(component, options)
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/core/createStore.js
DELETED
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
import { observe } from '../observer/index'
|
|
2
|
-
|
|
3
|
-
import { initComputed } from '../observer/computed'
|
|
4
|
-
|
|
5
|
-
import Vue from '../vue'
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
proxy,
|
|
9
|
-
getByPath
|
|
10
|
-
} from '../helper/utils'
|
|
11
|
-
|
|
12
|
-
import { warn } from '../helper/log'
|
|
13
|
-
|
|
14
|
-
// 兼容在web和小程序平台中创建表现一致的store
|
|
15
|
-
|
|
16
|
-
import mapStore from './mapStore'
|
|
17
|
-
|
|
18
|
-
function transformGetters (getters, module, store) {
|
|
19
|
-
const newGetters = {}
|
|
20
|
-
for (let key in getters) {
|
|
21
|
-
if (key in store.getters) {
|
|
22
|
-
warn(`Duplicate getter type: ${key}.`)
|
|
23
|
-
}
|
|
24
|
-
const getter = function () {
|
|
25
|
-
if (store.withThis) {
|
|
26
|
-
return getters[key].call({
|
|
27
|
-
state: module.state,
|
|
28
|
-
getters: store.getters,
|
|
29
|
-
rootState: store.state
|
|
30
|
-
})
|
|
31
|
-
}
|
|
32
|
-
return getters[key](module.state, store.getters, store.state)
|
|
33
|
-
}
|
|
34
|
-
newGetters[key] = getter
|
|
35
|
-
}
|
|
36
|
-
return newGetters
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function transformMutations (mutations, module, store) {
|
|
40
|
-
const newMutations = {}
|
|
41
|
-
for (let key in mutations) {
|
|
42
|
-
if (store.mutations[key]) {
|
|
43
|
-
warn(`Duplicate mutation type: ${key}.`)
|
|
44
|
-
}
|
|
45
|
-
const context = {
|
|
46
|
-
state: module.state,
|
|
47
|
-
commit: store.commit.bind(store)
|
|
48
|
-
}
|
|
49
|
-
const mutation = function (...payload) {
|
|
50
|
-
if (store.withThis) return mutations[key].apply(context, payload)
|
|
51
|
-
return mutations[key](module.state, ...payload)
|
|
52
|
-
}
|
|
53
|
-
newMutations[key] = mutation
|
|
54
|
-
}
|
|
55
|
-
return newMutations
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function transformActions (actions, module, store) {
|
|
59
|
-
const newActions = {}
|
|
60
|
-
for (let key in actions) {
|
|
61
|
-
if (store.actions[key]) {
|
|
62
|
-
warn(`Duplicate action type: ${key}.`)
|
|
63
|
-
}
|
|
64
|
-
newActions[key] = function (...payload) {
|
|
65
|
-
const context = {
|
|
66
|
-
rootState: store.state,
|
|
67
|
-
state: module.state,
|
|
68
|
-
getters: store.getters,
|
|
69
|
-
dispatch: store.dispatch.bind(store),
|
|
70
|
-
commit: store.commit.bind(store)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
let result
|
|
74
|
-
if (store.withThis) {
|
|
75
|
-
result = actions[key].apply(context, payload)
|
|
76
|
-
} else {
|
|
77
|
-
result = actions[key](context, ...payload)
|
|
78
|
-
}
|
|
79
|
-
// action一定返回一个promise
|
|
80
|
-
if (result && typeof result.then === 'function' && typeof result.catch === 'function') {
|
|
81
|
-
return result
|
|
82
|
-
} else {
|
|
83
|
-
return Promise.resolve(result)
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return newActions
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function mergeDeps (module, deps) {
|
|
91
|
-
const mergeProps = ['state', 'getters', 'mutations', 'actions']
|
|
92
|
-
Object.keys(deps).forEach(key => {
|
|
93
|
-
const store = deps[key]
|
|
94
|
-
mergeProps.forEach(prop => {
|
|
95
|
-
if (module[prop] && (key in module[prop])) {
|
|
96
|
-
warn(`Deps's name [${key}] conflicts with ${prop}'s key in current options.`)
|
|
97
|
-
} else {
|
|
98
|
-
module[prop] = module[prop] || {}
|
|
99
|
-
if (prop === 'getters') {
|
|
100
|
-
// depsGetters单独存放,不需要重新进行初始化
|
|
101
|
-
module.depsGetters = module.depsGetters || {}
|
|
102
|
-
module.depsGetters[key] = store.getters
|
|
103
|
-
// module[prop][key] = () => store[prop]
|
|
104
|
-
} else {
|
|
105
|
-
module[prop][key] = store[prop]
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
})
|
|
109
|
-
})
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
class Store {
|
|
113
|
-
constructor (options) {
|
|
114
|
-
const {
|
|
115
|
-
plugins = []
|
|
116
|
-
} = options
|
|
117
|
-
this.withThis = options.withThis
|
|
118
|
-
this.__wrappedGetters = {}
|
|
119
|
-
this.__depsGetters = {}
|
|
120
|
-
this.getters = {}
|
|
121
|
-
this.mutations = {}
|
|
122
|
-
this.actions = {}
|
|
123
|
-
this._subscribers = []
|
|
124
|
-
this.state = this.registerModule(options).state
|
|
125
|
-
this.resetStoreVM()
|
|
126
|
-
Object.assign(this, mapStore(this))
|
|
127
|
-
plugins.forEach(plugin => plugin(this))
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
dispatch (type, ...payload) {
|
|
131
|
-
const action = getByPath(this.actions, type)
|
|
132
|
-
if (!action) {
|
|
133
|
-
return Promise.reject(new Error(`unknown action type: ${type}`))
|
|
134
|
-
} else {
|
|
135
|
-
return action(...payload)
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
commit (type, ...payload) {
|
|
140
|
-
const mutation = getByPath(this.mutations, type)
|
|
141
|
-
if (!mutation) {
|
|
142
|
-
warn(`Unknown mutation type: ${type}.`)
|
|
143
|
-
} else {
|
|
144
|
-
mutation(...payload)
|
|
145
|
-
return this._subscribers.slice().forEach(sub => sub({ type, payload }, this.state))
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
subscribe (fn, options) {
|
|
150
|
-
return genericSubscribe(fn, this._subscribers, options)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
registerModule (module) {
|
|
154
|
-
const state = module.state || {}
|
|
155
|
-
const reactiveModule = {
|
|
156
|
-
state
|
|
157
|
-
}
|
|
158
|
-
if (module.getters) {
|
|
159
|
-
reactiveModule.getters = transformGetters(module.getters, reactiveModule, this)
|
|
160
|
-
}
|
|
161
|
-
if (module.mutations) {
|
|
162
|
-
reactiveModule.mutations = transformMutations(module.mutations, reactiveModule, this)
|
|
163
|
-
}
|
|
164
|
-
if (module.actions) {
|
|
165
|
-
reactiveModule.actions = transformActions(module.actions, reactiveModule, this)
|
|
166
|
-
}
|
|
167
|
-
if (module.deps) {
|
|
168
|
-
mergeDeps(reactiveModule, module.deps)
|
|
169
|
-
}
|
|
170
|
-
Object.assign(this.__depsGetters, reactiveModule.depsGetters)
|
|
171
|
-
Object.assign(this.__wrappedGetters, reactiveModule.getters)
|
|
172
|
-
// merge mutations
|
|
173
|
-
Object.assign(this.mutations, reactiveModule.mutations)
|
|
174
|
-
// merge actions
|
|
175
|
-
Object.assign(this.actions, reactiveModule.actions)
|
|
176
|
-
// 子module
|
|
177
|
-
if (module.modules) {
|
|
178
|
-
const childs = module.modules
|
|
179
|
-
Object.keys(childs).forEach(key => {
|
|
180
|
-
reactiveModule.state[key] = this.registerModule(childs[key]).state
|
|
181
|
-
})
|
|
182
|
-
}
|
|
183
|
-
return reactiveModule
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
resetStoreVM () {
|
|
187
|
-
if (__mpx_mode__ === 'web') {
|
|
188
|
-
this._vm = new Vue({
|
|
189
|
-
data: {
|
|
190
|
-
__mpxState: this.state
|
|
191
|
-
},
|
|
192
|
-
computed: this.__wrappedGetters
|
|
193
|
-
})
|
|
194
|
-
const computedKeys = Object.keys(this.__wrappedGetters)
|
|
195
|
-
proxy(this.getters, this._vm, computedKeys)
|
|
196
|
-
proxy(this.getters, this.__depsGetters)
|
|
197
|
-
} else {
|
|
198
|
-
this._vm = {}
|
|
199
|
-
observe(this.state, true)
|
|
200
|
-
initComputed(this._vm, this.getters, this.__wrappedGetters)
|
|
201
|
-
proxy(this.getters, this.__depsGetters)
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function genericSubscribe (fn, subs, options) {
|
|
207
|
-
if (subs.indexOf(fn) < 0) {
|
|
208
|
-
options && options.prepend
|
|
209
|
-
? subs.unshift(fn)
|
|
210
|
-
: subs.push(fn)
|
|
211
|
-
}
|
|
212
|
-
return () => {
|
|
213
|
-
const i = subs.indexOf(fn)
|
|
214
|
-
if (i > -1) {
|
|
215
|
-
subs.splice(i, 1)
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export default function createStore (options) {
|
|
221
|
-
return new Store(options)
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// ts util functions
|
|
225
|
-
export function createStateWithThis (state) {
|
|
226
|
-
return state
|
|
227
|
-
}
|
|
228
|
-
export function createGettersWithThis (getters, options = {}) {
|
|
229
|
-
return getters
|
|
230
|
-
}
|
|
231
|
-
export function createMutationsWithThis (mutations, options = {}) {
|
|
232
|
-
return mutations
|
|
233
|
-
}
|
|
234
|
-
export function createActionsWithThis (actions, options = {}) {
|
|
235
|
-
return actions
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
export function createStoreWithThis (options) {
|
|
239
|
-
options.withThis = true
|
|
240
|
-
return new Store(options)
|
|
241
|
-
}
|
package/src/core/mapStore.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
normalizeMap,
|
|
3
|
-
getByPath
|
|
4
|
-
} from '../helper/utils'
|
|
5
|
-
|
|
6
|
-
import { warn, error } from '../helper/log'
|
|
7
|
-
|
|
8
|
-
function mapFactory (type, store) {
|
|
9
|
-
return function (depPath, maps) {
|
|
10
|
-
maps = normalizeMap(depPath, maps)
|
|
11
|
-
const result = {}
|
|
12
|
-
Object.entries(maps).forEach(([key, value]) => {
|
|
13
|
-
result[key] = function (payload) {
|
|
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)
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
})
|
|
40
|
-
return result
|
|
41
|
-
}
|
|
42
|
-
}
|
|
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
|
-
|
|
59
|
-
export default function (store) {
|
|
60
|
-
return {
|
|
61
|
-
mapGetters: mapFactory('getters', store),
|
|
62
|
-
mapMutations: mapFactory('mutations', store),
|
|
63
|
-
mapActions: mapFactory('actions', store),
|
|
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)
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
package/src/helper/env.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export function getEnvObj () {
|
|
2
|
-
switch (__mpx_mode__) {
|
|
3
|
-
case 'wx':
|
|
4
|
-
return wx
|
|
5
|
-
case 'ali':
|
|
6
|
-
return my
|
|
7
|
-
case 'swan':
|
|
8
|
-
return swan
|
|
9
|
-
case 'qq':
|
|
10
|
-
return qq
|
|
11
|
-
case 'tt':
|
|
12
|
-
return tt
|
|
13
|
-
case 'jd':
|
|
14
|
-
return jd
|
|
15
|
-
case 'qa':
|
|
16
|
-
return qa
|
|
17
|
-
case 'dd':
|
|
18
|
-
return dd
|
|
19
|
-
}
|
|
20
|
-
}
|