-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.d.ts
277 lines (250 loc) · 6.84 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
type Setter = {
classSetter: ClassSetter
functionSetter: FunctionSetter
}
type FunctionSetter = {
[modelName: string]: {
[actionName: string]: {
setState: React.Dispatch<any>
selector?: Function
selectorRef?: unknown
}
}
}
type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y
? 1
: 2
? true
: false
interface Global {
Actions: {
[modelName: string]: {
[actionName: string]: Action
}
}
State: {
[modelName: string]: any
}
mutableState: {
[modelName: string]: any
}
AsyncState: {
[modelName: string]: undefined | ((context?: any) => Promise<Partial<any>>)
}
Context: any
Middlewares: {
[modelName: string]: Middleware[]
}
subscriptions: Subscriptions
Setter: Setter
devTools: any
withDevTools: boolean
gid: number
uid: number
storeId: number
currentStoreId: string
}
type ClassSetter = React.Dispatch<any> | undefined
type Action<S = {}, P = any, ActionKeys = {}, ExtContext extends {} = {}> = (
params: P,
context: {
state: S
actions: getConsumerActionsType<Actions<S, ActionKeys, ExtContext>>
} & ExtContext
) =>
| Partial<S>
| Promise<Partial<S> | ProduceFunc<S> | void>
| ProduceFunc<S>
| void
type ProduceFunc<S> = (state: S) => void
// v3.0 Actions
type Actions<S = {}, ActionKeys = {}, ExtContext extends object = {}> = {
[P in keyof ActionKeys]: Action<S, ActionKeys[P], ActionKeys, ExtContext>
}
// v4.1+ Custom Hooks
type CustomModelHook<State> = () => State
type Dispatch<A> = (value: A) => void
type SetStateAction<S> = S | ((prevState: S) => S)
interface ModelContext {
modelName: string
}
interface BaseContext<S = {}, P = any> {
action: Action
consumerActions: (
actions: Actions,
modelContext: ModelContext
) => getConsumerActionsType<Actions>
params: P
middlewareConfig?: Object
actionName: string
modelName: string
next?: Function
disableSelectorUpdate?: boolean
newState: Global['State'] | Function | null
Global: Global
}
interface InnerContext<S = {}> extends BaseContext<S> {
// Actions with function type context will always invoke current component's reload.
// f -> function, o -> outer, c -> class, u -> useModel
type?: 'f' | 'o' | 'c' | 'u'
__hash?: string
}
type Context<S = {}> = InnerContext<S> & {
next: Function
modelMiddlewares?: Middleware[]
}
type Middleware<S = {}> = (C: Context<S>, M: Middleware<S>[]) => Promise<void>
type MiddlewareConfig = {
logger: {
enable: boolean | ((context: BaseContext) => boolean)
}
devtools: { enable: boolean }
tryCatch: { enable: boolean }
}
interface Models<State = any, ActionKeys = any, ExtContext extends {} = {}> {
[name: string]:
| ModelType<State, ActionKeys, ExtContext>
| API<ModelType<State, ActionKeys, {}>>
}
type Selector<S, R> = (state: S) => R
interface API<MT extends ModelType = ModelType<any, any, {}>> {
__id: string
__ERROR__?: boolean
useStore: <
F extends Selector<Get<MT, 'state'>, any> = Selector<
Get<MT, 'state'>,
unknown
>
>(
selector?: F
) => [
F extends Selector<Get<MT, 'state'>, any>
? Equals<F, Selector<Get<MT, 'state'>, unknown>> extends true
? Get<MT, 'state'>
: ReturnType<F>
: Get<MT, 'state'>,
getConsumerActionsType<Get<MT, 'actions'>>
]
getState: () => Readonly<Get<MT, 'state'>>
subscribe: (
actionName: keyof MT['actions'] | Array<keyof MT['actions']>,
callback: (context: BaseContext) => void
) => void
unsubscribe: (
actionName: keyof Get<MT, 'actions'> | Array<keyof Get<MT, 'actions'>>
) => void
actions: Readonly<getConsumerActionsType<Get<MT, 'actions'>>>
}
interface LaneAPI<S> {
useStore: () => S
getState: () => S
getStore: () => S | undefined
subscribe: (callback: () => void) => void
unsubscribe: (callback: () => void) => void
}
interface APIs<M extends Models> {
useStore: <
K extends keyof M,
S extends M[K] extends API
? ArgumentTypes<Get<M[K], 'useStore'>>[1]
: M[K] extends ModelType
? Selector<Get<M[K], 'state'>, unknown>
: any
>(
name: K,
selector?: S
) => M[K] extends API
? S extends (...args: any) => void
? Equals<S, undefined> extends true
? [ReturnType<Get<M[K], 'getState'>>, Get<M[K], 'actions'>]
: ReturnType<S>
: ReturnType<Get<M[K], 'useStore'>>
: M[K] extends ModelType
? S extends (...args: any) => void
? [
Equals<ReturnType<S>, unknown> extends true
? Get<M[K], 'state'>
: ReturnType<S>,
getConsumerActionsType<Get<M[K], 'actions'>>
]
: [Get<M[K], 'state'>, getConsumerActionsType<Get<M[K], 'actions'>>]
: any
getState: <K extends keyof M>(
modelName: K
) => M[K] extends ModelType
? Readonly<Get<M[K], 'state'>>
: M[K] extends API
? ReturnType<Get<M[K], 'getState'>>
: any
getActions: <K extends keyof M>(
modelName: K
) => M[K] extends ModelType
? Readonly<getConsumerActionsType<Get<M[K], 'actions'>>>
: M[K] extends API
? M[K]['actions']
: unknown
getInitialState: <T extends any>(
context?: T | undefined,
config?: { isServer: boolean }
) => Promise<{
[modelName: string]: any
}>
subscribe: <K extends keyof M>(
modelName: K,
actionName: keyof Get<M[K], 'actions'> | Array<keyof Get<M[K], 'actions'>>,
callback: (context: BaseContext) => void
) => void
unsubscribe: <K extends keyof M>(
modelName: K,
actionName: keyof Get<M[K], 'actions'> | Array<keyof Get<M[K], 'actions'>>
) => void
actions: {
[K in keyof M]: M[K] extends API
? M[K]['actions']
: Readonly<getConsumerActionsType<Get<M[K], 'actions'>>>
}
}
// v3.0
type ModelType<
InitStateType = any,
ActionKeys = any,
ExtContext extends {} = {}
> = {
__ERROR__?: boolean
actions: {
[P in keyof ActionKeys]: Action<
InitStateType,
ActionKeys[P],
ActionKeys,
ExtContext
>
}
middlewares?: Middleware[]
state: InitStateType
asyncState?: (context?: any) => Promise<Partial<InitStateType>>
}
type ArgumentTypes<F extends Function> = F extends (...args: infer A) => any
? A
: never
// v3.0
// TODO: ArgumentTypes<A[P]>[0] = undefined | string
type getConsumerActionsType<A extends Actions<any, any, any>> = {
[P in keyof A]: ArgumentTypes<A[P]>[0] extends undefined
? (params?: ArgumentTypes<A[P]>[0]) => ReturnType<A[P]>
: (params: ArgumentTypes<A[P]>[0]) => ReturnType<A[P]>
}
type Get<Object, K extends keyof Object> = Object[K]
type ModelsProps<M extends Models> = {
useStore: <K extends keyof M>(
name: K,
models?: M
) => M[K] extends ModelType
? [Get<M[K], 'state'>, getConsumerActionsType<Get<M[K], 'actions'>>]
: M[K]
getState: <K extends keyof M>(
modelName: K
) => M[K] extends ModelType ? Readonly<Get<M[K], 'state'>> : M[K]
}
type Subscriptions = {
[key: string]: Function[]
}