diff --git a/src/index.ts b/src/index.ts index 05c52c9..5a4649e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,12 +43,31 @@ export type AsyncStateStatus = | 'success' | 'error'; -export type AsyncState = { - status: AsyncStateStatus; - loading: boolean; - error: Error | undefined; - result: R | undefined; -}; +export declare type AsyncState = + | { + status: 'not-requested'; + loading: false; + result: undefined; + error: undefined; + } + | { + status: 'loading'; + loading: true; + error: undefined; + result: undefined; + } + | { + status: 'success'; + loading: false; + error: undefined; + result: R; + } + | { + status: 'error'; + loading: false; + error: Error; + result: undefined; + }; type SetLoading = (asyncState: AsyncState) => AsyncState; type SetResult = (result: R, asyncState: AsyncState) => AsyncState; type SetError = (error: Error, asyncState: AsyncState) => AsyncState; @@ -135,7 +154,6 @@ const normalizeOptions = ( type UseAsyncStateResult = { value: AsyncState; set: Dispatch>>; - merge: (value: Partial>) => void; reset: () => void; setLoading: () => void; setResult: (r: R) => void; @@ -167,19 +185,9 @@ const useAsyncState = ( [value, setValue] ); - const merge = useCallback( - (state: Partial>) => - setValue({ - ...value, - ...state, - }), - [value, setValue] - ); - return { value, set: setValue, - merge, reset, setLoading, setResult, @@ -217,7 +225,6 @@ export type UseAsyncReturn< Args extends any[] = UnknownArgs > = AsyncState & { set: (value: AsyncState) => void; - merge: (value: Partial>) => void; reset: () => void; execute: (...args: Args) => Promise; currentPromise: Promise | null; @@ -298,7 +305,6 @@ const useAsyncInternal = ( return { ...AsyncState.value, set: AsyncState.set, - merge: AsyncState.merge, reset: AsyncState.reset, execute: executeAsyncOperationMemo, currentPromise: CurrentPromise.get(),