Skip to content

Commit 65eebf3

Browse files
committed
move common types to types.ts
1 parent 6aaa045 commit 65eebf3

File tree

7 files changed

+153
-162
lines changed

7 files changed

+153
-162
lines changed

packages/react-async/src/Async.tsx

+11-141
Original file line numberDiff line numberDiff line change
@@ -5,147 +5,17 @@ import { IfInitial, IfPending, IfFulfilled, IfRejected, IfSettled } from "./help
55
// @ts-ignore
66
import propTypes from "./propTypes"
77
import { actionTypes, init, dispatchMiddleware, reducer as asyncReducer } from "./reducer"
8-
9-
export declare type AsyncChildren<T> = ((state: AsyncState<T>) => React.ReactNode) | React.ReactNode
10-
export declare type InitialChildren<T> =
11-
| ((state: AsyncInitial<T>) => React.ReactNode)
12-
| React.ReactNode
13-
export declare type PendingChildren<T> =
14-
| ((state: AsyncPending<T>) => React.ReactNode)
15-
| React.ReactNode
16-
export declare type FulfilledChildren<T> =
17-
| ((data: T, state: AsyncFulfilled<T>) => React.ReactNode)
18-
| React.ReactNode
19-
export declare type RejectedChildren<T> =
20-
| ((error: Error, state: AsyncRejected<T>) => React.ReactNode)
21-
| React.ReactNode
22-
export declare type SettledChildren<T> =
23-
| ((state: AsyncFulfilled<T> | AsyncRejected<T>) => React.ReactNode)
24-
| React.ReactNode
25-
26-
export declare type PromiseFn<T> = (props: AsyncProps<T>, controller: AbortController) => Promise<T>
27-
export declare type DeferFn<T> = (
28-
args: any[],
29-
props: AsyncProps<T>,
30-
controller: AbortController
31-
) => Promise<T>
32-
33-
export interface AbstractAction {
34-
type: string
35-
meta: { counter: number; [meta: string]: any }
36-
}
37-
export type Meta = AbstractAction["meta"]
38-
39-
export declare type Start = AbstractAction & { type: "start"; payload: () => Promise<void> }
40-
export declare type Cancel = AbstractAction & { type: "cancel" }
41-
export declare type Fulfill<T> = AbstractAction & { type: "fulfill"; payload: T }
42-
export declare type Reject = AbstractAction & { type: "reject"; payload: Error; error: true }
43-
export declare type AsyncAction<T> = Start | Cancel | Fulfill<T> | Reject
44-
45-
export declare interface AsyncOptions<T> {
46-
promise?: Promise<T>
47-
promiseFn?: PromiseFn<T>
48-
deferFn?: DeferFn<T>
49-
watch?: any
50-
watchFn?: (props: AsyncProps<T>, prevProps: AsyncProps<T>) => any
51-
initialValue?: T
52-
onResolve?: (data: T) => void
53-
onReject?: (error: Error) => void
54-
reducer?: (
55-
state: AsyncState<T>,
56-
action: AsyncAction<T>,
57-
internalReducer: (state: AsyncState<T>, action: AsyncAction<T>) => AsyncState<T>
58-
) => AsyncState<T>
59-
dispatcher?: (
60-
action: AsyncAction<T>,
61-
internalDispatch: (action: AsyncAction<T>) => void,
62-
props: AsyncProps<T>
63-
) => void
64-
debugLabel?: string
65-
[prop: string]: any
66-
}
67-
68-
export declare interface AsyncProps<T> extends AsyncOptions<T> {
69-
children?: AsyncChildren<T>
70-
}
71-
72-
export interface AbstractState<T> {
73-
initialValue?: T | Error
74-
counter: number
75-
promise: Promise<T>
76-
run: (...args: any[]) => void
77-
reload: () => void
78-
cancel: () => void
79-
setData: (data: T, callback?: () => void) => T
80-
setError: (error: Error, callback?: () => void) => Error
81-
}
82-
83-
export declare type AsyncInitial<T, S = AbstractState<T>> = S & {
84-
initialValue?: undefined
85-
data: undefined
86-
error: undefined
87-
value: undefined
88-
startedAt: undefined
89-
finishedAt: undefined
90-
status: "initial"
91-
isInitial: false
92-
isPending: false
93-
isLoading: false
94-
isFulfilled: false
95-
isResolved: false
96-
isRejected: false
97-
isSettled: false
98-
}
99-
export declare type AsyncPending<T, S = AbstractState<T>> = S & {
100-
data: T | undefined
101-
error: Error | undefined
102-
value: T | Error | undefined
103-
startedAt: Date
104-
finishedAt: undefined
105-
status: "pending"
106-
isInitial: false
107-
isPending: true
108-
isLoading: true
109-
isFulfilled: false
110-
isResolved: false
111-
isRejected: false
112-
isSettled: false
113-
}
114-
export declare type AsyncFulfilled<T, S = AbstractState<T>> = S & {
115-
data: T
116-
error: undefined
117-
value: T
118-
startedAt: Date
119-
finishedAt: Date
120-
status: "fulfilled"
121-
isInitial: false
122-
isPending: false
123-
isLoading: false
124-
isFulfilled: true
125-
isResolved: true
126-
isRejected: false
127-
isSettled: true
128-
}
129-
export declare type AsyncRejected<T, S = AbstractState<T>> = S & {
130-
data: T | undefined
131-
error: Error
132-
value: Error
133-
startedAt: Date
134-
finishedAt: Date
135-
status: "rejected"
136-
isInitial: false
137-
isPending: false
138-
isLoading: false
139-
isFulfilled: false
140-
isResolved: false
141-
isRejected: true
142-
isSettled: true
143-
}
144-
export declare type AsyncState<T, S extends AbstractState<T> = AbstractState<T>> =
145-
| AsyncInitial<T, S>
146-
| AsyncPending<T, S>
147-
| AsyncFulfilled<T, S>
148-
| AsyncRejected<T, S>
8+
import {
9+
AsyncProps,
10+
AsyncState,
11+
InitialChildren,
12+
PendingChildren,
13+
FulfilledChildren,
14+
SettledChildren,
15+
RejectedChildren,
16+
AbstractState,
17+
AsyncAction,
18+
} from "./types"
14919

15020
export declare class Async<T> extends React.Component<AsyncProps<T>, AsyncState<T>> {}
15121

packages/react-async/src/helpers.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
SettledChildren,
1010
AsyncState,
1111
AbstractState,
12-
} from "./Async"
12+
} from "./types"
1313

1414
/**
1515
* Due to https://github.com/microsoft/web-build-tools/issues/1050, we need

packages/react-async/src/index.ts

+2-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Async from "./Async"
2+
export { default as Async, createInstance } from "./Async"
23
export {
3-
default as Async,
4-
createInstance,
54
PromiseFn,
65
InitialChildren,
76
PendingChildren,
@@ -23,24 +22,10 @@ export {
2322
AsyncOptions,
2423
AsyncRejected,
2524
AsyncState,
26-
} from "./Async"
25+
} from "./types"
2726
export { default as useAsync, useFetch, FetchOptions } from "./useAsync"
2827
export default Async
2928
export { statusTypes } from "./status"
3029
export { default as globalScope } from "./globalScope"
3130
export * from "./helpers"
3231
export * from "./reducer"
33-
34-
/*
35-
> RejectedChildren
36-
> SettledChildren
37-
> PromiseFn
38-
> DeferFn
39-
> Fulfill
40-
> AsyncAction
41-
> AbstractState
42-
> AsyncInitial
43-
> AsyncPending
44-
> AsyncFulfilled
45-
> AsyncInitialWithout
46-
*/

packages/react-async/src/reducer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
AsyncRejected,
99
AsyncInitial,
1010
AbstractState,
11-
} from "./Async"
11+
} from "./types"
1212

1313
/**
1414
* Due to https://github.com/microsoft/web-build-tools/issues/1050, we need

packages/react-async/src/status.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PromiseFn } from "./Async"
1+
import { PromiseFn } from "./types"
22

33
export enum statusTypes {
44
initial = "initial",

packages/react-async/src/types.ts

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
export type AsyncChildren<T> = ((state: AsyncState<T>) => React.ReactNode) | React.ReactNode
2+
export type InitialChildren<T> = ((state: AsyncInitial<T>) => React.ReactNode) | React.ReactNode
3+
export type PendingChildren<T> = ((state: AsyncPending<T>) => React.ReactNode) | React.ReactNode
4+
export type FulfilledChildren<T> =
5+
| ((data: T, state: AsyncFulfilled<T>) => React.ReactNode)
6+
| React.ReactNode
7+
export type RejectedChildren<T> =
8+
| ((error: Error, state: AsyncRejected<T>) => React.ReactNode)
9+
| React.ReactNode
10+
export type SettledChildren<T> =
11+
| ((state: AsyncFulfilled<T> | AsyncRejected<T>) => React.ReactNode)
12+
| React.ReactNode
13+
14+
export type PromiseFn<T> = (props: AsyncProps<T>, controller: AbortController) => Promise<T>
15+
export type DeferFn<T> = (
16+
args: any[],
17+
props: AsyncProps<T>,
18+
controller: AbortController
19+
) => Promise<T>
20+
21+
export interface AbstractAction {
22+
type: string
23+
meta: { counter: number; [meta: string]: any }
24+
}
25+
export type Meta = AbstractAction["meta"]
26+
27+
export type Start = AbstractAction & { type: "start"; payload: () => Promise<void> }
28+
export type Cancel = AbstractAction & { type: "cancel" }
29+
export type Fulfill<T> = AbstractAction & { type: "fulfill"; payload: T }
30+
export type Reject = AbstractAction & { type: "reject"; payload: Error; error: true }
31+
export type AsyncAction<T> = Start | Cancel | Fulfill<T> | Reject
32+
33+
export interface AsyncOptions<T> {
34+
promise?: Promise<T>
35+
promiseFn?: PromiseFn<T>
36+
deferFn?: DeferFn<T>
37+
watch?: any
38+
watchFn?: (props: AsyncProps<T>, prevProps: AsyncProps<T>) => any
39+
initialValue?: T
40+
onResolve?: (data: T) => void
41+
onReject?: (error: Error) => void
42+
reducer?: (
43+
state: AsyncState<T>,
44+
action: AsyncAction<T>,
45+
internalReducer: (state: AsyncState<T>, action: AsyncAction<T>) => AsyncState<T>
46+
) => AsyncState<T>
47+
dispatcher?: (
48+
action: AsyncAction<T>,
49+
internalDispatch: (action: AsyncAction<T>) => void,
50+
props: AsyncProps<T>
51+
) => void
52+
debugLabel?: string
53+
[prop: string]: any
54+
}
55+
56+
export interface AsyncProps<T> extends AsyncOptions<T> {
57+
children?: AsyncChildren<T>
58+
}
59+
60+
export interface AbstractState<T> {
61+
initialValue?: T | Error
62+
counter: number
63+
promise: Promise<T>
64+
run: (...args: any[]) => void
65+
reload: () => void
66+
cancel: () => void
67+
setData: (data: T, callback?: () => void) => T
68+
setError: (error: Error, callback?: () => void) => Error
69+
}
70+
71+
export type AsyncInitial<T, S = AbstractState<T>> = S & {
72+
initialValue?: undefined
73+
data: undefined
74+
error: undefined
75+
value: undefined
76+
startedAt: undefined
77+
finishedAt: undefined
78+
status: "initial"
79+
isInitial: false
80+
isPending: false
81+
isLoading: false
82+
isFulfilled: false
83+
isResolved: false
84+
isRejected: false
85+
isSettled: false
86+
}
87+
export type AsyncPending<T, S = AbstractState<T>> = S & {
88+
data: T | undefined
89+
error: Error | undefined
90+
value: T | Error | undefined
91+
startedAt: Date
92+
finishedAt: undefined
93+
status: "pending"
94+
isInitial: false
95+
isPending: true
96+
isLoading: true
97+
isFulfilled: false
98+
isResolved: false
99+
isRejected: false
100+
isSettled: false
101+
}
102+
export type AsyncFulfilled<T, S = AbstractState<T>> = S & {
103+
data: T
104+
error: undefined
105+
value: T
106+
startedAt: Date
107+
finishedAt: Date
108+
status: "fulfilled"
109+
isInitial: false
110+
isPending: false
111+
isLoading: false
112+
isFulfilled: true
113+
isResolved: true
114+
isRejected: false
115+
isSettled: true
116+
}
117+
export type AsyncRejected<T, S = AbstractState<T>> = S & {
118+
data: T | undefined
119+
error: Error
120+
value: Error
121+
startedAt: Date
122+
finishedAt: Date
123+
status: "rejected"
124+
isInitial: false
125+
isPending: false
126+
isLoading: false
127+
isFulfilled: false
128+
isResolved: false
129+
isRejected: true
130+
isSettled: true
131+
}
132+
export type AsyncState<T, S extends AbstractState<T> = AbstractState<T>> =
133+
| AsyncInitial<T, S>
134+
| AsyncPending<T, S>
135+
| AsyncFulfilled<T, S>
136+
| AsyncRejected<T, S>

packages/react-async/src/useAsync.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useCallback, useDebugValue, useEffect, useMemo, useRef, useReducer } fr
33
import globalScope from "./globalScope"
44
import { actionTypes, init, dispatchMiddleware, reducer as asyncReducer } from "./reducer"
55

6-
import { AsyncOptions, AsyncState, AbstractState, PromiseFn, Meta } from "./Async"
6+
import { AsyncOptions, AsyncState, AbstractState, PromiseFn, Meta } from "./types"
77

88
export interface FetchOptions<T> extends AsyncOptions<T> {
99
defer?: boolean

0 commit comments

Comments
 (0)