Refactor generator to prevent duplicate reloading

This commit is contained in:
Misode
2022-05-08 16:24:39 +02:00
parent 2772d967e0
commit a432479672
13 changed files with 78 additions and 75 deletions

View File

@@ -1,13 +1,14 @@
import type { Inputs } from 'preact/hooks'
import { useEffect } from 'preact/hooks'
import type { AsyncState } from './useAsyncFn'
import type { AsyncCancel, AsyncState } from './useAsyncFn'
import { useAsyncFn } from './useAsyncFn'
export function useAsync<T>(
fn: () => Promise<T>,
export function useAsync<R>(
fn: () => Promise<R | typeof AsyncCancel>,
inputs: Inputs = [],
): AsyncState<T> {
const [state, callback] = useAsyncFn<T, () => Promise<T>>(fn, inputs, { loading: true })
initialState: AsyncState<R> = { loading: true },
): AsyncState<R> {
const [state, callback] = useAsyncFn<R, () => Promise<R | typeof AsyncCancel>>(fn, inputs, initialState)
useEffect(() => {
callback()