@mpxjs/store 2.8.0-beta.2 → 2.8.0
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 +440 -0
- package/package.json +4 -3
- package/src/mapStore.js +31 -13
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
import type { ComputedRef } from '@mpxjs/core'
|
|
2
|
+
|
|
3
|
+
type UnboxDepField<D, F> = F extends keyof D ? D[F] : {}
|
|
4
|
+
|
|
5
|
+
type GetReturnOrSelf<T> = T extends (...args: any)=> infer R ? R : T
|
|
6
|
+
|
|
7
|
+
interface compContext {
|
|
8
|
+
__mpxProxy: object;
|
|
9
|
+
[key: string]: any
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface Deps {
|
|
13
|
+
[key: string]: Store | StoreWithThis
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type UnboxDepsField<D extends Deps, F> = string extends keyof D ? {} : {
|
|
17
|
+
[K in keyof D]: UnboxDepField<D[K], F>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type getMutation<M> = M extends (state: any, ...payload: infer P) => infer R ? (...payload: P) => R : never
|
|
21
|
+
|
|
22
|
+
type getAction<A> = A extends (context: object, ...payload: infer P) => infer R ? (...payload: P) => R : never
|
|
23
|
+
|
|
24
|
+
type Mutations<S> = {
|
|
25
|
+
[key: string]: (this: void, state: S, ...payload: any[]) => any
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface Getters<S> {
|
|
29
|
+
[key: string]: (this: void, state: S, getters: any, globalState: any) => any
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type Actions<S, G extends Getters<S>> = {
|
|
33
|
+
[key: string]: (this: void, context: {
|
|
34
|
+
rootState: any,
|
|
35
|
+
state: S,
|
|
36
|
+
getters: GetGetters<G>,
|
|
37
|
+
dispatch: (type: string, ...payload: any[]) => any,
|
|
38
|
+
commit: (type: string, ...payload: any[]) => any
|
|
39
|
+
}, ...payload: any[]) => any
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type GetGetters<G> = {
|
|
43
|
+
readonly [K in keyof G]: G[K] extends (state: any, getters: any, globalState: any) => infer R ? R : G[K]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type GetMutations<M> = {
|
|
47
|
+
[K in keyof M]: getMutation<M[K]>
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type GetActions<A> = {
|
|
51
|
+
[K in keyof A]: getAction<A[K]>
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
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)
|
|
55
|
+
|
|
56
|
+
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)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
// do not exist in tip
|
|
60
|
+
declare const DEPS_SYMBOL: unique symbol;
|
|
61
|
+
declare const STATE_SYMBOL: unique symbol;
|
|
62
|
+
declare const GETTERS_SYMBOL: unique symbol;
|
|
63
|
+
|
|
64
|
+
type DepsSymbol = typeof DEPS_SYMBOL
|
|
65
|
+
type StateSymbol = typeof STATE_SYMBOL
|
|
66
|
+
type GettersSymbol = typeof GETTERS_SYMBOL
|
|
67
|
+
|
|
68
|
+
interface Store<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> {
|
|
69
|
+
|
|
70
|
+
[DEPS_SYMBOL]: D
|
|
71
|
+
[STATE_SYMBOL]: S
|
|
72
|
+
[GETTERS_SYMBOL]: GetGetters<G>
|
|
73
|
+
|
|
74
|
+
state: S & UnboxDepsField<D, 'state'>
|
|
75
|
+
getters: GetGetters<G> & UnboxDepsField<D, 'getters'>
|
|
76
|
+
mutations: GetMutations<M> & UnboxDepsField<D, 'mutations'>
|
|
77
|
+
actions: GetActions<A> & UnboxDepsField<D, 'actions'>
|
|
78
|
+
|
|
79
|
+
dispatch: GetDispatch<A, D>
|
|
80
|
+
|
|
81
|
+
commit: GetCommit<M, D>
|
|
82
|
+
|
|
83
|
+
mapState<K extends keyof S>(maps: K[]): {
|
|
84
|
+
[I in K]: () => S[I]
|
|
85
|
+
}
|
|
86
|
+
mapState(depPath: string, maps: string[]): object
|
|
87
|
+
|
|
88
|
+
// mapState support object
|
|
89
|
+
mapState<T extends { [key: string]: keyof GetAllMapKeys<S, D, StateSymbol> }>(obj: T): {
|
|
90
|
+
[I in keyof T]: () => GetAllMapKeys<S, D, StateSymbol>[T[I]]
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
mapGetters<K extends keyof G>(maps: K[]): {
|
|
94
|
+
[I in K]: () => GetGetters<G>[I]
|
|
95
|
+
}
|
|
96
|
+
mapGetters(depPath: string, maps: string[]): {
|
|
97
|
+
[key: string]: () => any
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
mapMutations<K extends keyof M>(maps: K[]): Pick<GetMutations<M>, K>
|
|
101
|
+
mapMutations(depPath: string, maps: string[]): {
|
|
102
|
+
[key: string]: (...payloads: any[]) => any
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
mapActions<K extends keyof A>(maps: K[]): Pick<GetActions<A>, K>
|
|
106
|
+
mapActions(depPath: string, maps: string[]): {
|
|
107
|
+
[key: string]: (...payloads: any[]) => any
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 组合式 API
|
|
111
|
+
mapStateToRefs<K extends keyof S>(maps: K[]): {
|
|
112
|
+
[I in K]: ComputedRef<S[I]>
|
|
113
|
+
}
|
|
114
|
+
mapStateToRefs(depPath: string, maps: string[]): object
|
|
115
|
+
|
|
116
|
+
// mapState support object
|
|
117
|
+
mapStateToRefs<T extends { [key: string]: keyof GetAllMapKeys<S, D, StateSymbol> }>(obj: T): {
|
|
118
|
+
[I in keyof T]: ComputedRef<GetAllMapKeys<S, D, StateSymbol>[T[I]]>
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
mapGettersToRefs<K extends keyof G>(maps: K[]): {
|
|
122
|
+
[I in K]: ComputedRef<GetGetters<G>[I]>
|
|
123
|
+
}
|
|
124
|
+
mapGettersToRefs(depPath: string, maps: string[]): {
|
|
125
|
+
[key: string]: ComputedRef<any>
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 下面是新增的异步store的接口类型
|
|
129
|
+
mapStateToInstance<K extends keyof S>(maps: K[], context: compContext): void
|
|
130
|
+
mapStateToInstance(depPath: string, maps: string[], context: compContext): void
|
|
131
|
+
|
|
132
|
+
// mapState support object
|
|
133
|
+
mapStateToInstance<T extends { [key: string]: keyof GetAllMapKeys<S, D, StateSymbol> }>(obj: T, context: compContext): void
|
|
134
|
+
|
|
135
|
+
mapGettersToInstance<K extends keyof G>(maps: K[], context: compContext): void
|
|
136
|
+
mapGettersToInstance(depPath: string, maps: string[], context: compContext): void
|
|
137
|
+
|
|
138
|
+
mapMutationsToInstance<K extends keyof M>(maps: K[], context: compContext): Pick<GetMutations<M>, K>
|
|
139
|
+
mapMutationsToInstance(depPath: string, maps: string[], context: compContext): void
|
|
140
|
+
|
|
141
|
+
mapActionsToInstance<K extends keyof A>(maps: K[], context: compContext): Pick<GetActions<A>, K>
|
|
142
|
+
mapActionsToInstance(depPath: string, maps: string[], context: compContext): void
|
|
143
|
+
}
|
|
144
|
+
type GetComputedSetKeys<T> = {
|
|
145
|
+
[K in keyof T]: T[K] extends {
|
|
146
|
+
get(): any,
|
|
147
|
+
set(val: any): void
|
|
148
|
+
} ? K : never
|
|
149
|
+
}[keyof T]
|
|
150
|
+
|
|
151
|
+
export type GetComputedType<T> = {
|
|
152
|
+
readonly [K in Exclude<keyof T, GetComputedSetKeys<T>>]: T[K] extends () => infer R ? R : T[K]
|
|
153
|
+
} & {
|
|
154
|
+
[K in GetComputedSetKeys<T>]: T[K] extends {
|
|
155
|
+
get(): infer R,
|
|
156
|
+
set(val: any): void
|
|
157
|
+
} ? R : T[K]
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
interface MutationsAndActionsWithThis {
|
|
161
|
+
[key: string]: (...payload: any[]) => any
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
type UnionToIntersection<U> = (U extends any
|
|
165
|
+
? (k: U) => void
|
|
166
|
+
: never) extends ((k: infer I) => void)
|
|
167
|
+
? I
|
|
168
|
+
: never;
|
|
169
|
+
|
|
170
|
+
interface mapStateFunctionType<S, G> {
|
|
171
|
+
[key: string]: (state: S, getter: G) => any
|
|
172
|
+
}
|
|
173
|
+
interface DeeperMutationsAndActions {
|
|
174
|
+
[key: string]: ((...payload: any[]) => any) | MutationsAndActionsWithThis
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
interface DeeperStateAndGetters {
|
|
178
|
+
[key: string]: any | DeeperStateAndGetters
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* remove compatible code in Mpx.
|
|
183
|
+
* if you need using createStoreWithThis mix with createStore
|
|
184
|
+
* you can add a global define file
|
|
185
|
+
* use Declaration Merging(https://www.typescriptlang.org/docs/handbook/declaration-merging.html) on CompatibleDispatch:
|
|
186
|
+
* @example
|
|
187
|
+
* declare module '@mpxjs/core' {
|
|
188
|
+
* interface CompatibleDispatch {
|
|
189
|
+
* dispatch(type: string, ...payload: any[]): any
|
|
190
|
+
* commit(type: string, ...payload: any[]): any
|
|
191
|
+
* }
|
|
192
|
+
* }
|
|
193
|
+
*/
|
|
194
|
+
export interface CompatibleDispatch {
|
|
195
|
+
// dispatch(type: string, ...payload: any[]): any
|
|
196
|
+
// commit(type: string, ...payload: any[]): any
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Store Type Bindings
|
|
200
|
+
type StringKeyof<T> = Exclude<keyof T, symbol>
|
|
201
|
+
|
|
202
|
+
type CombineStringKey<H extends string | number, L extends string | number> = H extends '' ? `${L}` : `${H}.${L}`
|
|
203
|
+
|
|
204
|
+
type GetActionsKey<A, P extends string | number = ''> = UnionToIntersection<{
|
|
205
|
+
[K in StringKeyof<A>]: {
|
|
206
|
+
[RK in CombineStringKey<P, K>]: A[K] extends DeeperMutationsAndActions ? GetActionsKey<A[K], RK> : Record<RK, A[K]>
|
|
207
|
+
}[CombineStringKey<P, K>]
|
|
208
|
+
}[StringKeyof<A>]> // {actA: () => void, storeB.actB: () => void}
|
|
209
|
+
|
|
210
|
+
type GetStateAndGettersKey<D extends Deps, DK extends keyof D, T extends StateSymbol | GettersSymbol, P extends string | number = ''> = UnionToIntersection<{
|
|
211
|
+
[K in StringKeyof<D[DK][T]>]: {
|
|
212
|
+
[RK in CombineStringKey<P, K>]: D[DK][T][K]
|
|
213
|
+
}
|
|
214
|
+
}[StringKeyof<D[DK][T]>] | {
|
|
215
|
+
[K in StringKeyof<D[DK][DepsSymbol]>]: GetStateAndGettersKey<D[DK][DepsSymbol], K, T, CombineStringKey<P, K>>
|
|
216
|
+
}[StringKeyof<D[DK][DepsSymbol]>]>
|
|
217
|
+
|
|
218
|
+
// type GetStateAndGettersKey<S, P extends string | number = ''> = UnionToIntersection<{
|
|
219
|
+
// [K in StringKeyof<S>]: {
|
|
220
|
+
// [RK in CombineStringKey<P, K>]: S[K] extends DeeperStateAndGetters ? GetStateAndGettersKey<S[K], RK> : Record<RK, S[K]>
|
|
221
|
+
// }[CombineStringKey<P, K>]
|
|
222
|
+
// }[StringKeyof<S>]> // {stateA: any, storeB.stateB: any}
|
|
223
|
+
|
|
224
|
+
type GetAllDepsType<A, D extends Deps, AK extends StateSymbol | GettersSymbol | 'actions' | 'mutations'> = {
|
|
225
|
+
[K in StringKeyof<A>]: A[K]
|
|
226
|
+
} & UnionToIntersection<{
|
|
227
|
+
[K in StringKeyof<D>]: AK extends 'actions' | 'mutations' ? {
|
|
228
|
+
[P in keyof GetActionsKey<D[K][AK], K>]: GetActionsKey<D[K][AK], K>[P]
|
|
229
|
+
} : AK extends StateSymbol | GettersSymbol ? { // state, getters
|
|
230
|
+
[P in keyof GetStateAndGettersKey<D, K, AK, K>]: GetStateAndGettersKey<D, K, AK, K>[P]
|
|
231
|
+
// [P in keyof GetStateAndGettersKey<D[K][AK], K>]: GetStateAndGettersKey<D[K][AK], K>[P]
|
|
232
|
+
} : {}
|
|
233
|
+
}[StringKeyof<D>]>
|
|
234
|
+
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)
|
|
235
|
+
|
|
236
|
+
// type GetAllMapKeys<S, D extends Deps, SK extends 'state' | 'getters'> = GetAllDepsType<S, D, SK> & GetStateAndGettersKey<S>
|
|
237
|
+
type GetAllMapKeys<S, D extends Deps, SK extends StateSymbol | GettersSymbol> = GetAllDepsType<S, D, SK> // 关闭对state、getters本身传入对象的深层次推导,因为过深的递归会导致ts推导直接挂掉
|
|
238
|
+
|
|
239
|
+
interface StoreOptWithThis<S, G, M, A, D extends Deps> {
|
|
240
|
+
state?: S
|
|
241
|
+
getters?: G & ThisType<{ state: S & UnboxDepsField<D, 'state'>, getters: GetComputedType<G> & UnboxDepsField<D, 'getters'>, rootState: any }>
|
|
242
|
+
mutations?: M & ThisType<{ state: S & UnboxDepsField<D, 'state'> }>
|
|
243
|
+
actions?: A & ThisType<{
|
|
244
|
+
rootState: any,
|
|
245
|
+
state: S & UnboxDepsField<D, 'state'>,
|
|
246
|
+
getters: GetComputedType<G> & UnboxDepsField<D, 'getters'>,
|
|
247
|
+
dispatch: GetDispatchAndCommitWithThis<A, D, 'actions'>,
|
|
248
|
+
commit: GetDispatchAndCommitWithThis<M, D, 'mutations'>
|
|
249
|
+
} & CompatibleDispatch>
|
|
250
|
+
deps?: D
|
|
251
|
+
modules?: Record<string, StoreOptWithThis<{}, {}, {}, {}, {}>>
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
interface IStoreWithThis<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> {
|
|
255
|
+
|
|
256
|
+
[DEPS_SYMBOL]: D
|
|
257
|
+
[STATE_SYMBOL]: S
|
|
258
|
+
[GETTERS_SYMBOL]: GetComputedType<G>
|
|
259
|
+
|
|
260
|
+
state: S & UnboxDepsField<D, 'state'>
|
|
261
|
+
getters: GetComputedType<G> & UnboxDepsField<D, 'getters'>
|
|
262
|
+
mutations: M & UnboxDepsField<D, 'mutations'>
|
|
263
|
+
actions: A & UnboxDepsField<D, 'actions'>
|
|
264
|
+
|
|
265
|
+
dispatch: GetDispatchAndCommitWithThis<A, D, 'actions'>
|
|
266
|
+
|
|
267
|
+
commit: GetDispatchAndCommitWithThis<M, D, 'mutations'>
|
|
268
|
+
|
|
269
|
+
mapState<K extends keyof S>(maps: K[]): {
|
|
270
|
+
[I in K]: () => S[I]
|
|
271
|
+
}
|
|
272
|
+
mapState<T extends string, P extends string>(depPath: P, maps: readonly T[]): {
|
|
273
|
+
[K in T]: () => (CombineStringKey<P, K> extends keyof GetAllMapKeys<S, D, StateSymbol> ? GetAllMapKeys<S, D, StateSymbol>[CombineStringKey<P, K>] : any)
|
|
274
|
+
}
|
|
275
|
+
mapState<T extends mapStateFunctionType<S & UnboxDepsField<D, 'state'>, GetComputedType<G> & UnboxDepsField<D, 'getters'>>>(obj: ThisType<any> & T): {
|
|
276
|
+
[I in keyof T]: () => ReturnType<T[I]>
|
|
277
|
+
}
|
|
278
|
+
// Support chain derivation
|
|
279
|
+
mapState<T extends { [key: string]: keyof GetAllMapKeys<S, D, StateSymbol> }>(obj: T): {
|
|
280
|
+
[I in keyof T]: () => GetAllMapKeys<S, D, StateSymbol>[T[I]]
|
|
281
|
+
}
|
|
282
|
+
mapState<T extends { [key: string]: keyof S }>(obj: T): {
|
|
283
|
+
[I in keyof T]: () => S[T[I]]
|
|
284
|
+
}
|
|
285
|
+
mapState<T extends { [key: string]: string }>(obj: T): {
|
|
286
|
+
[I in keyof T]: (...payloads: any[]) => any
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
mapGetters<K extends keyof G>(maps: K[]): Pick<G, K>
|
|
290
|
+
mapGetters<T extends string, P extends string>(depPath: P, maps: readonly T[]): {
|
|
291
|
+
// use GetComputedType to get getters' returns
|
|
292
|
+
[K in T]: () => (CombineStringKey<P, K> extends keyof GetAllMapKeys<GetComputedType<G>, D, GettersSymbol> ? GetAllMapKeys<GetComputedType<G>, D, GettersSymbol>[CombineStringKey<P, K>] : any)
|
|
293
|
+
}
|
|
294
|
+
// Support chain derivation
|
|
295
|
+
mapGetters<T extends { [key: string]: keyof GetAllMapKeys<GetComputedType<G>, D, GettersSymbol> }>(obj: T): {
|
|
296
|
+
[I in keyof T]: () => GetAllMapKeys<GetComputedType<G>, D, GettersSymbol>[T[I]]
|
|
297
|
+
}
|
|
298
|
+
mapGetters<T extends { [key: string]: keyof G }>(obj: T): {
|
|
299
|
+
[I in keyof T]: G[T[I]]
|
|
300
|
+
}
|
|
301
|
+
// When importing js in ts file, use this method to be compatible
|
|
302
|
+
mapGetters<T extends { [key: string]: string }>(obj: T): {
|
|
303
|
+
[I in keyof T]: (...payloads: any[]) => any
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
mapMutations<K extends keyof M>(maps: K[]): Pick<M, K>
|
|
307
|
+
mapMutations<T extends string, P extends string>(depPath: P, maps: readonly T[]): {
|
|
308
|
+
[K in T]: CombineStringKey<P, K> extends keyof GetAllDepsType<M, D, 'mutations'> ? GetAllDepsType<M, D, 'mutations'>[CombineStringKey<P, K>] : (...payloads: any[]) => any
|
|
309
|
+
}
|
|
310
|
+
mapMutations<T extends { [key: string]: keyof M }>(obj: T): {
|
|
311
|
+
[I in keyof T]: M[T[I]]
|
|
312
|
+
}
|
|
313
|
+
mapMutations<T extends { [key: string]: string }>(obj: T): {
|
|
314
|
+
[I in keyof T]: (...payloads: any[]) => any
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
mapActions<K extends keyof A>(maps: K[]): Pick<A, K>
|
|
318
|
+
mapActions<T extends string, P extends string>(depPath: P, maps: readonly T[]): {
|
|
319
|
+
[K in T]: CombineStringKey<P, K> extends keyof GetAllDepsType<A, D, 'actions'> ? GetAllDepsType<A, D, 'actions'>[CombineStringKey<P, K>] : (...payloads: any[]) => any
|
|
320
|
+
}
|
|
321
|
+
mapActions<T extends { [key: string]: keyof A }>(obj: T): {
|
|
322
|
+
[I in keyof T]: A[T[I]]
|
|
323
|
+
}
|
|
324
|
+
mapActions<T extends { [key: string]: string }>(obj: T): {
|
|
325
|
+
[I in keyof T]: (...payloads: any[]) => any
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// 组合式 API
|
|
329
|
+
mapStateToRefs<K extends keyof S>(maps: K[]): {
|
|
330
|
+
[I in K]: ComputedRef<S[I]>
|
|
331
|
+
}
|
|
332
|
+
mapStateToRefs<T extends string, P extends string>(depPath: P, maps: readonly T[]): {
|
|
333
|
+
[K in T]: ComputedRef<CombineStringKey<P, K> extends keyof GetAllMapKeys<S, D, StateSymbol> ? GetAllMapKeys<S, D, StateSymbol>[CombineStringKey<P, K>] : any>
|
|
334
|
+
}
|
|
335
|
+
mapStateToRefs<T extends mapStateFunctionType<S & UnboxDepsField<D, 'state'>, GetComputedType<G> & UnboxDepsField<D, 'getters'>>>(obj: ThisType<any> & T): {
|
|
336
|
+
[I in keyof T]: ComputedRef<ReturnType<T[I]>>
|
|
337
|
+
}
|
|
338
|
+
// Support chain derivation
|
|
339
|
+
mapStateToRefs<T extends { [key: string]: keyof GetAllMapKeys<S, D, StateSymbol> }>(obj: T): {
|
|
340
|
+
[I in keyof T]: ComputedRef<GetAllMapKeys<S, D, StateSymbol>[T[I]]>
|
|
341
|
+
}
|
|
342
|
+
mapStateToRefs<T extends { [key: string]: keyof S }>(obj: T): {
|
|
343
|
+
[I in keyof T]: ComputedRef<S[T[I]]>
|
|
344
|
+
}
|
|
345
|
+
mapStateToRefs<T extends { [key: string]: string }>(obj: T): {
|
|
346
|
+
[I in keyof T]: ComputedRef<any>
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
mapGettersToRefs<K extends keyof G>(maps: K[]): {
|
|
350
|
+
[I in K]: ComputedRef<GetReturnOrSelf<G[I]>>
|
|
351
|
+
}
|
|
352
|
+
mapGettersToRefs<T extends string, P extends string>(depPath: P, maps: readonly T[]): {
|
|
353
|
+
// use GetComputedType to get getters' returns
|
|
354
|
+
[K in T]: ComputedRef<CombineStringKey<P, K> extends keyof GetAllMapKeys<GetComputedType<G>, D, GettersSymbol> ? GetAllMapKeys<GetComputedType<G>, D, GettersSymbol>[CombineStringKey<P, K>] : any>
|
|
355
|
+
}
|
|
356
|
+
// Support chain derivation
|
|
357
|
+
mapGettersToRefs<T extends { [key: string]: keyof GetAllMapKeys<GetComputedType<G>, D, GettersSymbol> }>(obj: T): {
|
|
358
|
+
[I in keyof T]: ComputedRef<GetAllMapKeys<GetComputedType<G>, D, GettersSymbol>[T[I]]>
|
|
359
|
+
}
|
|
360
|
+
mapGettersToRefs<T extends { [key: string]: keyof G }>(obj: T): {
|
|
361
|
+
[I in keyof T]: ComputedRef<GetReturnOrSelf<G[T[I]]>>
|
|
362
|
+
}
|
|
363
|
+
// When importing js in ts file, use this method to be compatible
|
|
364
|
+
mapGettersToRefs<T extends { [key: string]: string }>(obj: T): {
|
|
365
|
+
[I in keyof T]: ComputedRef<any>
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// 异步store api
|
|
369
|
+
mapStateToInstance<K extends keyof S>(maps: K[], context: compContext): void
|
|
370
|
+
mapStateToInstance<T extends string, P extends string>(depPath: P, maps: readonly T[], context: compContext): void
|
|
371
|
+
mapStateToInstance<T extends mapStateFunctionType<S & UnboxDepsField<D, 'state'>, GetComputedType<G> & UnboxDepsField<D, 'getters'>>>(obj: ThisType<any> & T, context: compContext): void
|
|
372
|
+
// Support chain derivation
|
|
373
|
+
mapStateToInstance<T extends { [key: string]: keyof GetAllMapKeys<S, D, StateSymbol> }>(obj: T, context: compContext): void
|
|
374
|
+
mapStateToInstance<T extends { [key: string]: keyof S }>(obj: T, context: compContext): void
|
|
375
|
+
mapStateToInstance<T extends { [key: string]: string }>(obj: T, context: compContext): void
|
|
376
|
+
|
|
377
|
+
mapGettersToInstance<K extends keyof G>(maps: K[], context: compContext): void
|
|
378
|
+
mapGettersToInstance<T extends string, P extends string>(depPath: P, maps: readonly T[], context: compContext): void
|
|
379
|
+
// Support chain derivation
|
|
380
|
+
mapGettersToInstance<T extends { [key: string]: keyof GetAllMapKeys<GetComputedType<G>, D, GettersSymbol> }>(obj: T, context: compContext): void
|
|
381
|
+
mapGettersToInstance<T extends { [key: string]: keyof G }>(obj: T, context: compContext): void
|
|
382
|
+
// When importing js in ts file, use this method to be compatible
|
|
383
|
+
mapGettersToInstance<T extends { [key: string]: string }>(obj: T, context: compContext): void
|
|
384
|
+
|
|
385
|
+
mapMutationsToInstance<K extends keyof M>(maps: K[], context: compContext): void
|
|
386
|
+
mapMutationsToInstance<T extends string, P extends string>(depPath: P, maps: readonly T[], context: compContext): void
|
|
387
|
+
mapMutationsToInstance<T extends { [key: string]: keyof M }>(obj: T, context: compContext): void
|
|
388
|
+
mapMutationsToInstance<T extends { [key: string]: string }>(obj: T, context: compContext): void
|
|
389
|
+
|
|
390
|
+
mapActionsToInstance<K extends keyof A>(maps: K[], context: compContext): void
|
|
391
|
+
mapActionsToInstance<T extends string, P extends string>(depPath: P, maps: readonly T[], context: compContext): void
|
|
392
|
+
mapActionsToInstance<T extends { [key: string]: keyof A }>(obj: T, context: compContext): void
|
|
393
|
+
mapActionsToInstance<T extends { [key: string]: string }>(obj: T, context: compContext): void
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
type StoreWithThis<S = {}, G = {}, M = {}, A = {}, D extends Deps = {}> = IStoreWithThis<S, G, M, A, D> & CompatibleDispatch
|
|
397
|
+
|
|
398
|
+
interface StoreOpt<S, G, M, A, D extends Deps> {
|
|
399
|
+
state?: S,
|
|
400
|
+
getters?: G
|
|
401
|
+
mutations?: M,
|
|
402
|
+
actions?: A,
|
|
403
|
+
deps?: D
|
|
404
|
+
modules?: Record<string, StoreOpt<{}, {}, {}, {}, {}>>
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
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>
|
|
408
|
+
|
|
409
|
+
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>
|
|
410
|
+
|
|
411
|
+
// auxiliary functions
|
|
412
|
+
export function createStateWithThis<S = {}>(state: S): S
|
|
413
|
+
|
|
414
|
+
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?: {
|
|
415
|
+
state?: S,
|
|
416
|
+
getters?: OG,
|
|
417
|
+
deps?: D
|
|
418
|
+
}): G
|
|
419
|
+
|
|
420
|
+
export function createMutationsWithThis<S = {}, D extends Deps = {}, M extends MutationsAndActionsWithThis = {}>(mutations: M & ThisType<{ state: S & UnboxDepsField<D, 'state'>, commit: GetDispatchAndCommitWithThis<M, D, 'mutations'> }>, options?: {
|
|
421
|
+
state?: S,
|
|
422
|
+
deps?: D
|
|
423
|
+
}): M
|
|
424
|
+
|
|
425
|
+
export function createActionsWithThis<S = {}, G = {}, M extends MutationsAndActionsWithThis = {}, D extends Deps = {}, A extends MutationsAndActionsWithThis = {}, OA extends MutationsAndActionsWithThis = {}>(actions: A & ThisType<{
|
|
426
|
+
rootState: any,
|
|
427
|
+
state: S & UnboxDepsField<D, 'state'>,
|
|
428
|
+
getters: GetComputedType<G> & UnboxDepsField<D, 'getters'>,
|
|
429
|
+
dispatch: GetDispatchAndCommitWithThis<A & OA, D, 'actions'>,
|
|
430
|
+
commit: GetDispatchAndCommitWithThis<M, D, 'mutations'>
|
|
431
|
+
} & CompatibleDispatch>, options?: {
|
|
432
|
+
state?: S,
|
|
433
|
+
getters?: G,
|
|
434
|
+
mutations?: M,
|
|
435
|
+
actions?: OA,
|
|
436
|
+
deps?: D
|
|
437
|
+
}): A
|
|
438
|
+
|
|
439
|
+
// use this to avoid symbol export
|
|
440
|
+
export { }
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mpxjs/store",
|
|
3
|
-
"version": "2.8.0
|
|
3
|
+
"version": "2.8.0",
|
|
4
4
|
"description": "A store for mpx framework",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
+
"types": "@types/index.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
9
|
},
|
|
@@ -25,10 +26,10 @@
|
|
|
25
26
|
},
|
|
26
27
|
"homepage": "https://github.com/didi/mpx#readme",
|
|
27
28
|
"dependencies": {
|
|
28
|
-
"@mpxjs/utils": "^2.8.0
|
|
29
|
+
"@mpxjs/utils": "^2.8.0"
|
|
29
30
|
},
|
|
30
31
|
"peerDependencies": {
|
|
31
32
|
"@mpxjs/core": "^2.8.0"
|
|
32
33
|
},
|
|
33
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "c52503f66f8ab3832e7ec94357d9e1586fbc4460"
|
|
34
35
|
}
|
package/src/mapStore.js
CHANGED
|
@@ -5,6 +5,8 @@ import {
|
|
|
5
5
|
isObject
|
|
6
6
|
} from '@mpxjs/utils'
|
|
7
7
|
|
|
8
|
+
import { computed } from '@mpxjs/core'
|
|
9
|
+
|
|
8
10
|
function normalizeMap (prefix, arr) {
|
|
9
11
|
if (typeof prefix !== 'string') {
|
|
10
12
|
arr = prefix
|
|
@@ -81,37 +83,53 @@ function checkMapInstance (args) {
|
|
|
81
83
|
}
|
|
82
84
|
|
|
83
85
|
export default function (store) {
|
|
86
|
+
const mapState = mapFactory('state', store)
|
|
87
|
+
const mapGetters = mapFactory('getters', store)
|
|
88
|
+
const mapMutations = mapFactory('mutations', store)
|
|
89
|
+
const mapActions = mapFactory('actions', store)
|
|
90
|
+
|
|
84
91
|
return {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
//
|
|
92
|
+
mapState,
|
|
93
|
+
mapGetters,
|
|
94
|
+
mapMutations,
|
|
95
|
+
mapActions,
|
|
96
|
+
// map*ToRefs用于组合式API解构获取响应式数据
|
|
97
|
+
mapStateToRefs: (...args) => {
|
|
98
|
+
const result = {}
|
|
99
|
+
Object.entries(mapState(...args)).forEach(([key, value]) => {
|
|
100
|
+
result[key] = computed(value)
|
|
101
|
+
})
|
|
102
|
+
return result
|
|
103
|
+
},
|
|
104
|
+
mapGettersToRefs: (...args) => {
|
|
105
|
+
const result = {}
|
|
106
|
+
Object.entries(mapGetters(...args)).forEach(([key, value]) => {
|
|
107
|
+
result[key] = computed(value)
|
|
108
|
+
})
|
|
109
|
+
return result
|
|
110
|
+
},
|
|
111
|
+
// 以下是map*ToInstance用于异步store的,参数args:depPath, maps, context
|
|
90
112
|
mapStateToInstance: (...args) => {
|
|
91
113
|
const { context, restParams } = checkMapInstance(args)
|
|
92
|
-
const
|
|
93
|
-
const result = mapStateFun(...restParams)
|
|
114
|
+
const result = mapState(...restParams)
|
|
94
115
|
// 将result挂载到mpxProxy实例属性上
|
|
95
116
|
context.__mpxProxy.options.computed = context.__mpxProxy.options.computed || {}
|
|
96
117
|
Object.assign(context.__mpxProxy.options.computed, result)
|
|
97
118
|
},
|
|
98
119
|
mapGettersToInstance: (...args) => {
|
|
99
120
|
const { context, restParams } = checkMapInstance(args)
|
|
100
|
-
const
|
|
101
|
-
const result = mapGetFun(...restParams)
|
|
121
|
+
const result = mapGetters(...restParams)
|
|
102
122
|
context.__mpxProxy.options.computed = context.__mpxProxy.options.computed || {}
|
|
103
123
|
Object.assign(context.__mpxProxy.options.computed, result)
|
|
104
124
|
},
|
|
105
125
|
mapMutationsToInstance: (...args) => {
|
|
106
126
|
const { context, restParams } = checkMapInstance(args)
|
|
107
|
-
const
|
|
108
|
-
const result = mapMutationFun(...restParams)
|
|
127
|
+
const result = mapMutations(...restParams)
|
|
109
128
|
Object.assign(context, result)
|
|
110
129
|
},
|
|
111
130
|
mapActionsToInstance: (...args) => {
|
|
112
131
|
const { context, restParams } = checkMapInstance(args)
|
|
113
|
-
const
|
|
114
|
-
const result = mapActionFun(...restParams)
|
|
132
|
+
const result = mapActions(...restParams)
|
|
115
133
|
Object.assign(context, result)
|
|
116
134
|
}
|
|
117
135
|
}
|