@mpxjs/core 2.8.0-beta.2 → 2.8.0-beta.4

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 CHANGED
@@ -4,48 +4,29 @@
4
4
  // TypeScript Version: 4.1.3
5
5
 
6
6
  /// <reference types="miniprogram-api-typings" />
7
- /// <reference path="./mpx-store.d.ts" />
8
7
  /// <reference path="./global.d.ts" />
9
8
  /// <reference path="./node.d.ts" />
10
9
 
11
10
  // @ts-ignore
12
11
  import VueI18n from 'vue-i18n'
13
12
 
13
+ import type { GetComputedType } from '@mpxjs/store'
14
+
15
+ export * from '@mpxjs/store'
16
+
14
17
  declare module 'vue-i18n' {
15
18
  export default interface VueI18n {
16
19
  mergeMessages (messages: { [index: string]: VueI18n.LocaleMessageObject }): void;
17
20
  }
18
21
  }
19
- // declare Store types
20
- type StoreOpt<S, G, M, A, D extends MpxStore.Deps> = MpxStore.StoreOpt<S, G, M, A, D>
21
-
22
- type Store<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> = MpxStore.Store<S, G, M, A, D>
23
-
24
- type StoreOptWithThis<S, G, M, A, D extends Deps> = MpxStore.StoreOptWithThis<S, G, M, A, D>
25
-
26
- type StoreWithThis<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> = MpxStore.StoreWithThis<S, G, M, A, D>
27
-
28
- type UnboxDepsField<D extends Deps, F> = MpxStore.UnboxDepsField<D, F>
29
-
30
- type GetComputedType<T> = MpxStore.GetComputedType<T>
31
-
32
- type GetDispatchAndCommitWithThis<A, D extends Deps, AK extends 'actions' | 'mutations'> = MpxStore.GetDispatchAndCommitWithThis<A, D, AK>
33
-
34
- type MutationsAndActionsWithThis = MpxStore.MutationsAndActionsWithThis
35
-
36
- type Actions<S, G extends MpxStore.Getters<S>> = MpxStore.Actions<S, G>
37
-
38
- type Mutations<S> = MpxStore.Mutations<S>
39
-
40
- type Getters<S> = MpxStore.Getters<S>
41
-
42
- type Deps = MpxStore.Deps
43
22
 
44
23
  // utils
45
24
  type ObjectOf<T> = {
46
25
  [key: string]: T
47
26
  }
48
27
 
28
+ type AnyObject = ObjectOf<any>
29
+
49
30
  type UnionToIntersection<U> = (U extends any
50
31
  ? (k: U) => void
51
32
  : never) extends ((k: infer I) => void)
@@ -182,11 +163,11 @@ type PageOpt<D, P, C, M, Mi extends Array<any>, S extends Record<any, any>> =
182
163
  ComponentOpt<D, P, C, M, Mi, S>
183
164
  & Partial<WechatMiniprogram.Page.ILifetime>
184
165
 
185
- type ThisTypedPageOpt<D, P, C, M, Mi extends Array<any>, S extends Record<any, any>, O = {}> =
166
+ type ThisTypedPageOpt<D extends AnyObject, P, C, M, Mi extends Array<any>, S extends Record<any, any>, O = {}> =
186
167
  PageOpt<D, P, C, M, Mi, S>
187
168
  & ThisType<ComponentIns<D, P, C, M, Mi, S, O>> & O
188
169
 
189
- type ThisTypedComponentOpt<D, P, C, M, Mi extends Array<any>, S extends Record<any, any>, O = {}> =
170
+ type ThisTypedComponentOpt<D extends AnyObject, P, C, M, Mi extends Array<any>, S extends Record<any, any>, O = {}> =
190
171
  ComponentOpt<D, P, C, M, Mi, S>
191
172
  & ThisType<ComponentIns<D, P, C, M, Mi, S, O>> & O
192
173
 
@@ -245,12 +226,12 @@ interface ReplaceWxComponentIns {
245
226
  selectAllComponents (selector: string): Array<ComponentIns<{}, {}, {}, {}, []>>
246
227
  }
247
228
 
248
- type WxComponentIns<D> =
229
+ type WxComponentIns<D extends AnyObject> =
249
230
  ReplaceWxComponentIns
250
231
  & WechatMiniprogram.Component.InstanceProperties
251
232
  & WechatMiniprogram.Component.InstanceMethods<D>
252
233
 
253
- type ComponentIns<D, P, C, M, Mi extends Array<any>, S extends Record<any, any> = {}, O = {}> =
234
+ type ComponentIns<D extends AnyObject, P, C, M, Mi extends Array<any>, S extends Record<any, any> = {}, O = {}> =
254
235
  GetDataType<D> & UnboxMixinsField<Mi, 'data'> &
255
236
  M & UnboxMixinsField<Mi, 'methods'> & { [K in keyof S]: S[K] extends Ref<infer V> ? V : S[K] } &
256
237
  GetPropsType<P & UnboxMixinsField<Mi, 'properties'>> &
@@ -261,9 +242,9 @@ interface CreateConfig {
261
242
  customCtor: any
262
243
  }
263
244
 
264
- export function createComponent<D extends Data = {}, P extends Properties = {}, C = {}, M extends Methods = {}, Mi extends Array<any> = [], O = {}> (opt: ThisTypedComponentOpt<D, P, C, M, Mi, O>, config?: CreateConfig): void
245
+ export function createComponent<D extends Data = {}, P extends Properties = {}, C = {}, M extends Methods = {}, Mi extends Array<any> = [], S extends AnyObject = {}, O extends AnyObject = {}> (opt: ThisTypedComponentOpt<D, P, C, M, Mi, S, O>, config?: CreateConfig): void
265
246
 
266
- export function getMixin<D extends Data = {}, P extends Properties = {}, C = {}, M extends Methods = {}, Mi extends Array<any> = [], O = {}> (opt: ThisTypedComponentOpt<D, P, C, M, Mi, O>): {
247
+ export function getMixin<D extends Data = {}, P extends Properties = {}, C = {}, M extends Methods = {}, Mi extends Array<any> = [], S extends AnyObject = {}, O extends AnyObject = {}> (opt: ThisTypedComponentOpt<D, P, C, M, Mi, S, O>): {
267
248
  data: GetDataType<D> & UnboxMixinsField<Mi, 'data'>
268
249
  properties: P & UnboxMixinsField<Mi, 'properties'>
269
250
  computed: C & UnboxMixinsField<Mi, 'computed'>
@@ -271,42 +252,10 @@ export function getMixin<D extends Data = {}, P extends Properties = {}, C = {},
271
252
  [index: string]: any
272
253
  }
273
254
 
274
- export function createPage<D extends Data = {}, P extends Properties = {}, C = {}, M extends Methods = {}, Mi extends Array<any> = [], O = {}> (opt: ThisTypedPageOpt<D, P, C, M, Mi, O>, config?: CreateConfig): void
255
+ export function createPage<D extends Data = {}, P extends Properties = {}, C = {}, M extends Methods = {}, Mi extends Array<any> = [], O extends AnyObject = {}> (opt: ThisTypedPageOpt<D, P, C, M, Mi, O>, config?: CreateConfig): void
275
256
 
276
257
  export function createApp<T extends WechatMiniprogram.IAnyObject> (opt: WechatMiniprogram.App.Options<T>, config?: CreateConfig): void
277
258
 
278
- export function createStore<S, G extends Getters<S>, M extends Mutations<S>, A extends Actions<S, G>, D extends Deps = {}> (option: StoreOpt<S, G, M, A, D>): Store<S, G, M, A, D>
279
-
280
- export function createStoreWithThis<S = {}, G = {}, M extends MutationsAndActionsWithThis = {}, A extends MutationsAndActionsWithThis = {}, D extends Deps = {}> (option: StoreOptWithThis<S, G, M, A, D>): StoreWithThis<S, G, M, A, D>
281
-
282
- // auxiliary functions
283
- export function createStateWithThis<S = {}> (state: S): S
284
-
285
- export function createGettersWithThis<S = {}, D extends Deps = {}, G = {}, OG = {}> (getters: G & ThisType<{ state: S & UnboxDepsField<D, 'state'>, getters: GetComputedType<G & OG> & UnboxDepsField<D, 'getters'>, rootState: any }>, options?: {
286
- state?: S,
287
- getters?: OG,
288
- deps?: D
289
- }): G
290
-
291
- export function createMutationsWithThis<S = {}, D extends Deps = {}, M extends MutationsAndActionsWithThis = {}> (mutations: M & ThisType<{ state: S & UnboxDepsField<D, 'state'>, commit: GetDispatchAndCommitWithThis<M, D, 'mutations'> }>, options?: {
292
- state?: S,
293
- deps?: D
294
- }): M
295
-
296
- export function createActionsWithThis<S = {}, G = {}, M extends MutationsAndActionsWithThis = {}, D extends Deps = {}, A extends MutationsAndActionsWithThis = {}, OA extends MutationsAndActionsWithThis = {}> (actions: A & ThisType<{
297
- rootState: any,
298
- state: S & UnboxDepsField<D, 'state'>,
299
- getters: GetComputedType<G> & UnboxDepsField<D, 'getters'>,
300
- dispatch: GetDispatchAndCommitWithThis<A & OA, D, 'actions'>,
301
- commit: GetDispatchAndCommitWithThis<M, D, 'mutations'>
302
- } & MpxStore.CompatibleDispatch>, options?: {
303
- state?: S,
304
- getters?: G,
305
- mutations?: M,
306
- actions?: OA,
307
- deps?: D
308
- }): A
309
-
310
259
  type MixinType = 'app' | 'page' | 'component'
311
260
 
312
261
  export function injectMixins (mixins: object | Array<object>, options?: MixinType | MixinType[] | { types?: MixinType | MixinType[], stage?: number }): void
@@ -322,12 +271,12 @@ interface AnyConstructor {
322
271
  interface MpxConfig {
323
272
  useStrictDiff: boolean
324
273
  ignoreWarning: boolean | string | RegExp | ((msg: string, location: string, e: Error) => boolean)
325
- ignoreConflictWhiteList: Array<string>
274
+ ignoreProxyWhiteList: Array<string>
326
275
  observeClassInstance: boolean | Array<AnyConstructor>
327
- hookErrorHandler: (e: Error, target: ComponentIns<{}, {}, {}, {}, []>, hookName: string) => any | null
276
+ errorHandler: (e: Error, target: ComponentIns<{}, {}, {}, {}, []>, hookName: string) => any | null
328
277
  proxyEventHandler: (e: Event) => any | null
329
278
  setDataHandler: (data: object, target: ComponentIns<{}, {}, {}, {}, []>) => any | null
330
- forceRunWatcherSync: boolean,
279
+ forceFlushSync: boolean,
331
280
  webRouteConfig: object
332
281
  }
333
282
 
@@ -341,12 +290,13 @@ interface ImplementOptions {
341
290
 
342
291
  export function toPureObject<T extends object> (obj: T): T
343
292
 
293
+ declare type PluginInstallFunction = (app: Mpx, ...options: any[]) => any;
294
+
295
+ export type Plugin = PluginInstallFunction | {
296
+ install: PluginInstallFunction;
297
+ };
298
+
344
299
  export interface Mpx {
345
- createComponent: typeof createComponent
346
- createPage: typeof createPage
347
- createApp: typeof createApp
348
- createStore: typeof createStore
349
- createStoreWithThis: typeof createStoreWithThis
350
300
  getMixin: typeof getMixin
351
301
  mixin: typeof injectMixins
352
302
  injectMixins: typeof injectMixins
@@ -355,7 +305,7 @@ export interface Mpx {
355
305
 
356
306
  // watch: typeof watch
357
307
 
358
- use (plugin: ((...args: any) => any) | { install: (...args: any) => any, [key: string]: any }, ...rest: any): Mpx
308
+ use (plugin: Plugin, ...rest: any[]): Mpx
359
309
 
360
310
  implement (name: string, options?: ImplementOptions): void
361
311
 
@@ -488,9 +438,8 @@ type WatchCallback<T> = (
488
438
 
489
439
  type WatchSource<T> =
490
440
  | Ref<T> // ref
491
- | ComputedRef<T>
492
- | Reactive<T>
493
441
  | (() => T) // getter
442
+ | ComputedRef<T>
494
443
 
495
444
  type MultiWatchSources = (WatchSource<unknown> | object)[]
496
445
 
@@ -498,7 +447,7 @@ interface WatchEffectOptions {
498
447
  flush?: 'pre' | 'post' | 'sync' // default: 'pre'
499
448
  }
500
449
 
501
- interface WatchOptions extends WatchEffectOptions {
450
+ export interface WatchOptions extends WatchEffectOptions {
502
451
  immediate?: boolean // 默认:false
503
452
  deep?: boolean // 默认:false
504
453
  flush?: 'pre' | 'post' | 'sync' // 默认:'pre'
@@ -652,6 +601,22 @@ export function onHide (callback: () => void): void
652
601
 
653
602
  export function onResize (callback: () => void): void
654
603
 
604
+ export function onPullDownRefresh (callback: () => void): void
605
+
606
+ export function onReachBottom (callback: () => void): void
607
+
608
+ export function onShareAppMessage (callback: () => void): void
609
+
610
+ export function onShareTimeline (callback: () => void): void
611
+
612
+ export function onAddToFavorites (callback: () => void): void
613
+
614
+ export function onPageScroll (callback: () => void): void
615
+
616
+ export function onTabItemTap (callback: () => void): void
617
+
618
+ export function onSaveExitState (callback: () => void): void
619
+
655
620
  // get instance
656
621
  export function getCurrentInstance<T extends object> (): { target: T }
657
622
 
@@ -682,7 +647,7 @@ export const ONRESIZE: string
682
647
 
683
648
  declare global {
684
649
  const defineProps: <T>(props: T) => Readonly<GetPropsType<T>>
685
- const defineOptions: <D extends Data = {}, P extends Properties = {}, C = {}, M extends Methods = {}, Mi extends Array<any> = [], O = {}> (opt: ThisTypedComponentOpt<D, P, C, M, Mi, O>) => void
686
- const defineExpose: <E extends Record<string, any> = Record<string, any>>(exposed?: E) => void
650
+ const defineOptions: <D extends Data = {}, P extends Properties = {}, C = {}, M extends Methods = {}, Mi extends Array<any> = [], S extends AnyObject = {}, O extends AnyObject = {}> (opt: ThisTypedComponentOpt<D, P, C, M, Mi, S, O>) => void
651
+ const defineExpose: <E extends AnyObject = AnyObject>(exposed?: E) => void
687
652
  const useContext: () => Context
688
653
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/core",
3
- "version": "2.8.0-beta.2",
3
+ "version": "2.8.0-beta.4",
4
4
  "description": "mpx runtime core",
5
5
  "keywords": [
6
6
  "miniprogram",
@@ -19,7 +19,7 @@
19
19
  ],
20
20
  "main": "src/index.js",
21
21
  "dependencies": {
22
- "@mpxjs/utils": "^2.8.0-beta.2",
22
+ "@mpxjs/utils": "^2.8.0-beta.4",
23
23
  "lodash": "^4.1.1",
24
24
  "miniprogram-api-typings": "^3.0.2"
25
25
  },
@@ -47,5 +47,5 @@
47
47
  "url": "https://github.com/didi/mpx/issues"
48
48
  },
49
49
  "sideEffects": false,
50
- "gitHead": "cd57f83a9d47236f527bd51a8650119514f4b433"
50
+ "gitHead": "7a3dd6c2d0fceb31ee035e85b7f11091e31409d3"
51
51
  }
@@ -2,7 +2,7 @@ import { noop } from '@mpxjs/utils'
2
2
 
3
3
  export const implemented = {}
4
4
 
5
- export function implement (name, { modes = [], processor = noop(), remove = false } = {}) {
5
+ export function implement (name, { modes = [], processor = noop, remove = false } = {}) {
6
6
  if (!name) return
7
7
  if (modes.indexOf(__mpx_mode__) > -1) {
8
8
  processor()
package/src/core/proxy.js CHANGED
@@ -207,6 +207,8 @@ export default class MpxProxy {
207
207
  if (this.options.__type__ === 'page' && !this.options.__pageCtor__) {
208
208
  proxy(this.target, this.options, this.options.mpxCustomKeysForBlend, false, this.createProxyConflictHandler('page options'))
209
209
  }
210
+ // 挂载$rawOptions
211
+ this.target.$rawOptions = this.options
210
212
  if (__mpx_mode__ !== 'web') {
211
213
  // 挂载$watch
212
214
  this.target.$watch = this.watch.bind(this)
@@ -1,5 +1,5 @@
1
1
  import { getObserver } from './reactive'
2
- import { def } from '@mpxjs/utils'
2
+ import { def } from '@mpxjs/utils/src/base'
3
3
 
4
4
  const arrayProto = Array.prototype
5
5
 
@@ -4,7 +4,8 @@ import {
4
4
  ONSHOW,
5
5
  ONLOAD
6
6
  } from '../../core/innerLifecycle'
7
- import { isFunction, isBrowser } from '@mpxjs/utils'
7
+ import { isFunction } from '@mpxjs/utils'
8
+ import { isBrowser } from '@mpxjs/utils/src/env'
8
9
 
9
10
  let systemInfo = {}
10
11
 
@@ -1,5 +1,6 @@
1
1
  import { CREATED, BEFOREMOUNT, BEFOREUPDATE, UNMOUNTED } from '../../core/innerLifecycle'
2
- import { noop, error, getEnvObj } from '@mpxjs/utils'
2
+ import { noop, error } from '@mpxjs/utils'
3
+ import { getEnvObj } from '@mpxjs/utils/src/env'
3
4
 
4
5
  const envObj = getEnvObj()
5
6
 
@@ -1,7 +1,8 @@
1
1
  import transferOptions from '../core/transferOptions'
2
2
  import mergeOptions from '../core/mergeOptions'
3
3
  import builtInKeysMap from './patch/builtInKeysMap'
4
- import { makeMap, spreadProp } from '@mpxjs/utils'
4
+ import { spreadProp } from '@mpxjs/utils'
5
+ import { makeMap } from '@mpxjs/utils/src/array'
5
6
  import * as webLifecycle from '../platform/patch/web/lifecycle'
6
7
  import Mpx from '../index'
7
8
 
@@ -1,3 +1,10 @@
1
+ import {
2
+ effectScope as vueEffectScope,
3
+ getCurrentScope as getCurrentVueScope,
4
+ onScopeDispose,
5
+ getCurrentInstance as getCurrentVueInstance
6
+ } from 'vue'
7
+
1
8
  export {
2
9
  // watch
3
10
  watchEffect,
@@ -21,14 +28,31 @@ export {
21
28
  shallowRef,
22
29
  triggerRef,
23
30
  // computed
24
- computed,
25
- // effectScope
31
+ computed
32
+ } from 'vue'
33
+
34
+ const noop = () => {
35
+ }
36
+
37
+ const fixEffectScope = (scope) => {
38
+ scope.pause = noop
39
+ scope.resume = noop
40
+ }
41
+
42
+ const effectScope = (detached) => fixEffectScope(vueEffectScope(detached))
43
+ const getCurrentScope = () => fixEffectScope(getCurrentVueScope())
44
+
45
+ export {
26
46
  effectScope,
27
47
  getCurrentScope,
28
- onScopeDispose,
29
- // instance
48
+ onScopeDispose
49
+ }
50
+
51
+ const getCurrentInstance = () => getCurrentVueInstance()?.__mpxProxy
52
+
53
+ export {
30
54
  getCurrentInstance
31
- } from 'vue'
55
+ }
32
56
 
33
57
  export {
34
58
  // i18n
@@ -1,5 +1,5 @@
1
1
  import { INNER_LIFECYCLES } from '../../core/innerLifecycle'
2
- import { makeMap } from '@mpxjs/utils'
2
+ import { makeMap } from '@mpxjs/utils/src/array'
3
3
 
4
4
  let bulitInKeys
5
5
 
@@ -1,7 +1,7 @@
1
1
  import builtInKeysMap from '../builtInKeysMap'
2
2
  import mergeOptions from '../../../core/mergeOptions'
3
3
  import { diffAndCloneA } from '@mpxjs/utils'
4
- import { getCurrentInstance as getVueCurrentInstance } from '../../export/index'
4
+ import { getCurrentInstance as getCurrentVueInstance } from '../../export/index'
5
5
  import MpxProxy, { setCurrentInstance, unsetCurrentInstance } from '../../../core/proxy'
6
6
  import { BEFOREUPDATE, UPDATED, BEFOREUNMOUNT, UNMOUNTED } from '../../../core/innerLifecycle'
7
7
 
@@ -37,7 +37,7 @@ export function getDefaultOptions (type, { rawOptions = {} }) {
37
37
  const rawSetup = rawOptions.setup
38
38
  if (rawSetup) {
39
39
  rawOptions.setup = (props) => {
40
- const instance = getVueCurrentInstance().proxy
40
+ const instance = getCurrentVueInstance().proxy
41
41
  initProxy(instance, rawOptions)
42
42
  setCurrentInstance(instance.__mpxProxy)
43
43
  const newContext = {
@@ -1,333 +0,0 @@
1
- declare namespace MpxStore {
2
- type UnboxDepField<D, F> = F extends keyof D ? D[F] : {}
3
-
4
- interface compContext{
5
- __mpxProxy: object;
6
- [key: string]: any
7
- }
8
-
9
- interface Deps {
10
- [key: string]: Store | StoreWithThis
11
- }
12
-
13
- type UnboxDepsField<D extends Deps, F> = string extends keyof D ? {} : {
14
- [K in keyof D]: UnboxDepField<D[K], F>
15
- }
16
-
17
- type getMutation<M> = M extends (state: any, ...payload: infer P) => infer R ? (...payload: P) => R : never
18
-
19
- type getAction<A> = A extends (context: object, ...payload: infer P) => infer R ? (...payload: P) => R : never
20
-
21
- type Mutations<S> = {
22
- [key: string]: (this: void, state: S, ...payload: any[]) => any
23
- }
24
-
25
- interface Getters<S> {
26
- [key: string]: (this: void, state: S, getters: any, globalState: any) => any
27
- }
28
-
29
- type Actions<S, G extends Getters<S>> = {
30
- [key: string]: (this: void, context: {
31
- rootState: any,
32
- state: S,
33
- getters: GetGetters<G>,
34
- dispatch: (type: string, ...payload: any[]) => any,
35
- commit: (type: string, ...payload: any[]) => any
36
- }, ...payload: any[]) => any
37
- }
38
-
39
- type GetGetters<G> = {
40
- readonly [K in keyof G]: G[K] extends (state: any, getters: any, globalState: any) => infer R ? R : G[K]
41
- }
42
-
43
- type GetMutations<M> = {
44
- [K in keyof M]: getMutation<M[K]>
45
- }
46
-
47
- type GetActions<A> = {
48
- [K in keyof A]: getAction<A[K]>
49
- }
50
-
51
- type GetDispatch<A, D> = keyof D extends never ? (<T extends keyof A>(type: T, ...payload: A[T] extends (context: any, ...payload: infer P) => any ? P : never) => A[T] extends (context: any, ...payload: any[]) => infer R ? R : never) : ((type: string, ...payload: any[]) => any)
52
-
53
- type GetCommit<M, D> = keyof D extends never ? (<T extends keyof M>(type: T, ...payload: M[T] extends (state: any, ...payload: infer P) => any ? P : never) => M[T] extends (state: any, ...payload: any[]) => infer R ? R : never) : ((type: string, ...payload: any[]) => any)
54
-
55
- interface Store<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> {
56
-
57
- __deps: D
58
- __state: S
59
- __getters: GetGetters<G>
60
-
61
- state: S & UnboxDepsField<D, 'state'>
62
- getters: GetGetters<G> & UnboxDepsField<D, 'getters'>
63
- mutations: GetMutations<M> & UnboxDepsField<D, 'mutations'>
64
- actions: GetActions<A> & UnboxDepsField<D, 'actions'>
65
-
66
- dispatch: GetDispatch<A, D>
67
-
68
- commit: GetCommit<M, D>
69
-
70
- mapState<K extends keyof S>(maps: K[]): {
71
- [I in K]: () => S[I]
72
- }
73
- mapState(depPath: string, maps: string[]): object
74
-
75
- // mapState support object
76
- mapState<T extends { [key: string]: keyof GetAllMapKeys<S, D, 'state'> }>(obj: T): {
77
- [I in keyof T]: () => GetAllMapKeys<S, D, 'state'>[T[I]]
78
- }
79
-
80
- mapGetters<K extends keyof G>(maps: K[]): {
81
- [I in K]: () => GetGetters<G>[I]
82
- }
83
- mapGetters(depPath: string, maps: string[]): {
84
- [key: string]: () => any
85
- }
86
-
87
- mapMutations<K extends keyof M>(maps: K[]): Pick<GetMutations<M>, K>
88
- mapMutations(depPath: string, maps: string[]): {
89
- [key: string]: (...payloads: any[]) => any
90
- }
91
-
92
- mapActions<K extends keyof A>(maps: K[]): Pick<GetActions<A>, K>
93
- mapActions(depPath: string, maps: string[]): {
94
- [key: string]: (...payloads: any[]) => any
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
108
-
109
- mapActionsToInstance<K extends keyof A>(maps: K[], context: compContext): Pick<GetActions<A>, K>
110
- mapActionsToInstance(depPath: string, maps: string[], context: compContext): void
111
- }
112
- type GetComputedSetKeys<T> = {
113
- [K in keyof T]: T[K] extends {
114
- get(): any,
115
- set(val: any): void
116
- } ? K : never
117
- }[keyof T]
118
-
119
- type GetComputedType<T> = {
120
- readonly [K in Exclude<keyof T, GetComputedSetKeys<T>>]: T[K] extends () => infer R ? R : T[K]
121
- } & {
122
- [K in GetComputedSetKeys<T>]: T[K] extends {
123
- get(): infer R,
124
- set(val: any): void
125
- } ? R : T[K]
126
- }
127
-
128
- interface MutationsAndActionsWithThis {
129
- [key: string]: (...payload: any[]) => any
130
- }
131
-
132
- type UnionToIntersection<U> = (U extends any
133
- ? (k: U) => void
134
- : never) extends ((k: infer I) => void)
135
- ? I
136
- : never;
137
-
138
- interface mapStateFunctionType<S, G> {
139
- [key: string]: (state: S, getter: G) => any
140
- }
141
- interface DeeperMutationsAndActions {
142
- [key: string]: ((...payload: any[]) => any) | MutationsAndActionsWithThis
143
- }
144
-
145
- interface DeeperStateAndGetters {
146
- [key: string]: any | DeeperStateAndGetters
147
- }
148
-
149
- /**
150
- * remove compatible code in Mpx.
151
- * if you need using createStoreWithThis mix with createStore
152
- * you can add a global define file
153
- * use Declaration Merging(https://www.typescriptlang.org/docs/handbook/declaration-merging.html) on CompatibleDispatch:
154
- * @example
155
- * declare module MpxStore {
156
- * interface CompatibleDispatch {
157
- * dispatch(type: string, ...payload: any[]): any
158
- * commit(type: string, ...payload: any[]): any
159
- * }
160
- * }
161
- */
162
- interface CompatibleDispatch {
163
- // dispatch(type: string, ...payload: any[]): any
164
- // commit(type: string, ...payload: any[]): any
165
- }
166
-
167
- // Store Type Bindings
168
- type StringKeyof<T> = Exclude<keyof T, symbol>
169
-
170
- type CombineStringKey<H extends string | number, L extends string | number> = H extends '' ? `${L}` : `${H}.${L}`
171
-
172
- type GetActionsKey<A, P extends string | number = ''> = UnionToIntersection<{
173
- [K in StringKeyof<A>]: {
174
- [RK in CombineStringKey<P, K>]: A[K] extends DeeperMutationsAndActions ? GetActionsKey<A[K], RK> : Record<RK, A[K]>
175
- }[CombineStringKey<P, K>]
176
- }[StringKeyof<A>]> // {actA: () => void, storeB.actB: () => void}
177
-
178
- type GetStateAndGettersKey<D extends Deps, DK extends keyof D, T extends 'state' | 'getters', P extends string | number = ''> = UnionToIntersection<{
179
- [K in StringKeyof<D[DK][`__${T}`]>]: {
180
- [RK in CombineStringKey<P, K>]: D[DK][`__${T}`][K]
181
- }
182
- }[StringKeyof<D[DK][`__${T}`]>] | {
183
- [K in StringKeyof<D[DK]['__deps']>]: GetStateAndGettersKey<D[DK]['__deps'], K, T, CombineStringKey<P, K>>
184
- }[StringKeyof<D[DK]['__deps']>]>
185
-
186
- // type GetStateAndGettersKey<S, P extends string | number = ''> = UnionToIntersection<{
187
- // [K in StringKeyof<S>]: {
188
- // [RK in CombineStringKey<P, K>]: S[K] extends DeeperStateAndGetters ? GetStateAndGettersKey<S[K], RK> : Record<RK, S[K]>
189
- // }[CombineStringKey<P, K>]
190
- // }[StringKeyof<S>]> // {stateA: any, storeB.stateB: any}
191
-
192
- type GetAllDepsType<A, D extends Deps, AK extends 'state' | 'getters' | 'actions' | 'mutations'> = {
193
- [K in StringKeyof<A>]: A[K]
194
- } & UnionToIntersection<{
195
- [K in StringKeyof<D>]: AK extends 'actions' | 'mutations' ? {
196
- [P in keyof GetActionsKey<D[K][AK], K>]: GetActionsKey<D[K][AK], K>[P]
197
- } : AK extends 'state' | 'getters' ? { // state, getters
198
- [P in keyof GetStateAndGettersKey<D, K, AK, K>]: GetStateAndGettersKey<D, K, AK, K>[P]
199
- // [P in keyof GetStateAndGettersKey<D[K][AK], K>]: GetStateAndGettersKey<D[K][AK], K>[P]
200
- } : {}
201
- }[StringKeyof<D>]>
202
- type GetDispatchAndCommitWithThis<A, D extends Deps, AK extends 'actions' | 'mutations'> = (<T extends keyof GetAllDepsType<A, D, AK>>(type: T, ...payload: GetAllDepsType<A, D, AK>[T] extends (...payload: infer P) => any ? P : never) => GetAllDepsType<A, D, AK>[T] extends (...payload: any[]) => infer R ? R : never)
203
-
204
- // type GetAllMapKeys<S, D extends Deps, SK extends 'state' | 'getters'> = GetAllDepsType<S, D, SK> & GetStateAndGettersKey<S>
205
- type GetAllMapKeys<S, D extends Deps, SK extends 'state' | 'getters'> = GetAllDepsType<S, D, SK> // 关闭对state、getters本身传入对象的深层次推导,因为过深的递归会导致ts推导直接挂掉
206
-
207
- interface StoreOptWithThis<S, G, M, A, D extends Deps> {
208
- state?: S
209
- getters?: G & ThisType<{ state: S & UnboxDepsField<D, 'state'>, getters: GetComputedType<G> & UnboxDepsField<D, 'getters'>, rootState: any }>
210
- mutations?: M & ThisType<{ state: S & UnboxDepsField<D, 'state'> }>
211
- actions?: A & ThisType<{
212
- rootState: any,
213
- state: S & UnboxDepsField<D, 'state'>,
214
- getters: GetComputedType<G> & UnboxDepsField<D, 'getters'>,
215
- dispatch: GetDispatchAndCommitWithThis<A, D, 'actions'>,
216
- commit: GetDispatchAndCommitWithThis<M, D, 'mutations'>
217
- } & CompatibleDispatch>
218
- deps?: D
219
- modules?: Record<string, StoreOptWithThis<{}, {}, {}, {}, {}>>
220
- }
221
-
222
- interface IStoreWithThis<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> {
223
-
224
- __deps: D
225
- __state: S
226
- __getters: GetComputedType<G>
227
-
228
- state: S & UnboxDepsField<D, 'state'>
229
- getters: GetComputedType<G> & UnboxDepsField<D, 'getters'>
230
- mutations: M & UnboxDepsField<D, 'mutations'>
231
- actions: A & UnboxDepsField<D, 'actions'>
232
-
233
- dispatch: GetDispatchAndCommitWithThis<A, D, 'actions'>
234
-
235
- commit: GetDispatchAndCommitWithThis<M, D, 'mutations'>
236
-
237
- mapState<K extends keyof S>(maps: K[]): {
238
- [I in K]: () => S[I]
239
- }
240
- mapState<T extends string, P extends string>(depPath: P, maps: readonly T[]): {
241
- [K in T]: () => (CombineStringKey<P, K> extends keyof GetAllMapKeys<S, D, 'state'> ? GetAllMapKeys<S, D, 'state'>[CombineStringKey<P, K>] : any)
242
- }
243
- mapState<T extends mapStateFunctionType<S & UnboxDepsField<D, 'state'>, GetComputedType<G> & UnboxDepsField<D, 'getters'>>>(obj: ThisType<any> & T): {
244
- [I in keyof T]: () => ReturnType<T[I]>
245
- }
246
- // Support chain derivation
247
- mapState<T extends { [key: string]: keyof GetAllMapKeys<S, D, 'state'> }>(obj: T): {
248
- [I in keyof T]: () => GetAllMapKeys<S, D, 'state'>[T[I]]
249
- }
250
- mapState<T extends { [key: string]: keyof S }>(obj: T): {
251
- [I in keyof T]: () => S[T[I]]
252
- }
253
- mapState<T extends { [key: string]: string }>(obj: T): {
254
- [I in keyof T]: (...payloads: any[]) => any
255
- }
256
-
257
- mapGetters<K extends keyof G>(maps: K[]): Pick<G, K>
258
- mapGetters<T extends string, P extends string>(depPath: P, maps: readonly T[]): {
259
- // use GetComputedType to get getters' returns
260
- [K in T]: () => (CombineStringKey<P, K> extends keyof GetAllMapKeys<GetComputedType<G>, D, 'getters'> ? GetAllMapKeys<GetComputedType<G>, D, 'getters'>[CombineStringKey<P, K>] : any)
261
- }
262
- // Support chain derivation
263
- mapGetters<T extends { [key: string]: keyof GetAllMapKeys<GetComputedType<G>, D, 'getters'> }>(obj: T): {
264
- [I in keyof T]: () => GetAllMapKeys<GetComputedType<G>, D, 'getters'>[T[I]]
265
- }
266
- mapGetters<T extends { [key: string]: keyof G }>(obj: T): {
267
- [I in keyof T]: G[T[I]]
268
- }
269
- // When importing js in ts file, use this method to be compatible
270
- mapGetters<T extends { [key: string]: string }>(obj: T): {
271
- [I in keyof T]: (...payloads: any[]) => any
272
- }
273
-
274
- mapMutations<K extends keyof M>(maps: K[]): Pick<M, K>
275
- mapMutations<T extends string, P extends string>(depPath: P, maps: readonly T[]): {
276
- [K in T]: CombineStringKey<P, K> extends keyof GetAllDepsType<M, D, 'mutations'> ? GetAllDepsType<M, D, 'mutations'>[CombineStringKey<P, K>] : (...payloads: any[]) => any
277
- }
278
- mapMutations<T extends { [key: string]: keyof M }>(obj: T): {
279
- [I in keyof T]: M[T[I]]
280
- }
281
- mapMutations<T extends { [key: string]: string }>(obj: T): {
282
- [I in keyof T]: (...payloads: any[]) => any
283
- }
284
-
285
- mapActions<K extends keyof A>(maps: K[]): Pick<A, K>
286
- mapActions<T extends string, P extends string>(depPath: P, maps: readonly T[]): {
287
- [K in T]: CombineStringKey<P, K> extends keyof GetAllDepsType<A, D, 'actions'> ? GetAllDepsType<A, D, 'actions'>[CombineStringKey<P, K>] : (...payloads: any[]) => any
288
- }
289
- mapActions<T extends { [key: string]: keyof A }>(obj: T): {
290
- [I in keyof T]: A[T[I]]
291
- }
292
- mapActions<T extends { [key: string]: string }>(obj: T): {
293
- [I in keyof T]: (...payloads: any[]) => any
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
321
- }
322
-
323
- type StoreWithThis<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> = IStoreWithThis<S, G, M, A, D> & CompatibleDispatch
324
-
325
- interface StoreOpt<S, G, M, A, D extends Deps> {
326
- state?: S,
327
- getters?: G
328
- mutations?: M,
329
- actions?: A,
330
- deps?: D
331
- modules?: Record<string, StoreOpt<{}, {}, {}, {}, {}>>
332
- }
333
- }