@mpxjs/core 2.7.16 → 2.7.26

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,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.16",
3
+ "version": "2.7.26",
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": "7a2b5ae385cbd035172ec11bc0bfba84eff10243"
44
+ "gitHead": "bfad123d02a216278c122135148ba2a3977121d0"
45
45
  }
@@ -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
- for (let key in maps) {
12
+ Object.entries(maps).forEach(([key, value]) => {
13
13
  result[key] = function (payload) {
14
- const value = maps[key]
15
- if (type === 'mutations') {
16
- return store.commit(value, payload)
17
- } else if (type === 'actions') {
18
- return store.dispatch(value, payload)
19
- } else {
20
- let getterVal = getByPath(store.getters, value, '', '__NOTFOUND__')
21
- if (getterVal === '__NOTFOUND__') {
22
- warn(`Unknown getter named [${value}].`)
23
- getterVal = ''
24
- }
25
- return getterVal
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: (depPath, maps) => {
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
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
  }
@@ -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({}, options.computed, currentInject.injectComputed)
20
+ options.computed = Object.assign({}, currentInject.injectComputed, options.computed)
21
21
  }
22
22
  if (currentInject && currentInject.injectOptions) {
23
23
  // 编译option注入,优先微信中的单独配置
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
  }
@@ -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