|
1 | 1 | import * as React from "react"
|
2 | 2 |
|
3 |
| -type ChildrenFunction = (state: object) => React.ReactNode |
| 3 | +type AsyncChildren<T> = ((state: AsyncState<T>) => React.ReactNode) | React.ReactNode |
4 | 4 |
|
5 |
| -interface AsyncProps { |
6 |
| - children?: React.ReactNode | ChildrenFunction |
7 |
| - promiseFn?(props: object): Promise<any> |
8 |
| - deferFn?(props: object): Promise<any> |
| 5 | +interface AsyncProps<T> { |
| 6 | + promiseFn?: (props: object) => Promise<T> |
| 7 | + deferFn?: (...args, props: object) => Promise<T> |
9 | 8 | watch?: any
|
10 |
| - initialValue?: any |
11 |
| - onResolve?(data: any): any |
12 |
| - onReject?(error: Error): any |
| 9 | + initialValue?: T |
| 10 | + onResolve?: (data: T) => void |
| 11 | + onError?: (error: Error) => void |
| 12 | + children?: AsyncChildren<T> |
13 | 13 | }
|
14 | 14 |
|
15 |
| -interface PendingProps { |
16 |
| - children?: React.ReactNode | ChildrenFunction |
17 |
| - persist?: boolean |
| 15 | +interface AsyncState<T> { |
| 16 | + initialValue?: T |
| 17 | + data?: T |
| 18 | + error?: Error |
| 19 | + isLoading: boolean |
| 20 | + startedAt?: Date |
| 21 | + finishedAt?: Date |
| 22 | + cancel: () => void |
| 23 | + run: (...args) => Promise<T> |
| 24 | + reload: () => void |
| 25 | + setData: (data: T, callback?: () => void) => T |
| 26 | + setError: (error: Error, callback?: () => void) => Error |
18 | 27 | }
|
19 | 28 |
|
20 |
| -interface LoadingProps { |
21 |
| - children?: React.ReactNode | ChildrenFunction |
22 |
| - initial?: boolean |
| 29 | +class Async<T> extends React.Component<AsyncProps<T>, AsyncState<T>> { |
| 30 | + static Pending: React.FunctionComponent<{ children?: AsyncChildren; persist?: boolean }> |
| 31 | + static Loading: React.FunctionComponent<{ children?: AsyncChildren; initial?: boolean }> |
| 32 | + static Resolved: React.FunctionComponent<{ children?: AsyncChildren; persist?: boolean }> |
| 33 | + static Rejected: React.FunctionComponent<{ children?: AsyncChildren; persist?: boolean }> |
23 | 34 | }
|
24 | 35 |
|
25 |
| -interface ResolvedProps { |
26 |
| - children?: React.ReactNode | ChildrenFunction |
27 |
| - persist?: boolean |
28 |
| -} |
29 |
| - |
30 |
| -interface RejectedProps { |
31 |
| - children?: React.ReactNode | ChildrenFunction |
32 |
| - persist?: boolean |
33 |
| -} |
34 |
| - |
35 |
| -declare class Async extends React.Component<AsyncProps, any> { |
36 |
| - public static Pending: React.SFC<PendingProps> |
37 |
| - public static Loading: React.SFC<LoadingProps> |
38 |
| - public static Resolved: React.SFC<ResolvedProps> |
39 |
| - public static Rejected: React.SFC<RejectedProps> |
40 |
| -} |
41 |
| - |
42 |
| -declare function createInstance(defaultProps?: AsyncProps): Async |
| 36 | +function createInstance<T>(defaultProps?: AsyncProps<T> = {}): Async<T> |
43 | 37 |
|
44 | 38 | export default createInstance
|
0 commit comments