@mpxjs/core 2.7.25 → 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.25",
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": "0bcd275c2d8a3fe33ba383b5c2b53e1988217f9e"
44
+ "gitHead": "bfad123d02a216278c122135148ba2a3977121d0"
45
45
  }
@@ -3,7 +3,7 @@ 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) {
@@ -41,11 +41,54 @@ function mapFactory (type, store) {
41
41
  }
42
42
  }
43
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
+
44
59
  export default function (store) {
45
60
  return {
46
61
  mapGetters: mapFactory('getters', store),
47
62
  mapMutations: mapFactory('mutations', store),
48
63
  mapActions: mapFactory('actions', store),
49
- mapState: mapFactory('state', 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
+ }
50
93
  }
51
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注入,优先微信中的单独配置
@@ -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