Skip to content

Commit

Permalink
fix: useAsyncProcessQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
ssi02014 committed Apr 20, 2024
1 parent 443f52b commit 74fcb2f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/document.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
jobs:
document-publish:
runs-on: ubuntu-latest
if: github.event.pull_request.merged == true && github.event.pull_request.user == 'github-actions[bot]'
if: github.event.pull_request.merged == true && github.event.pull_request.user.login == 'github-actions[bot]'
steps:
- name: Checkout 🔔
uses: actions/checkout@v4
Expand Down
22 changes: 11 additions & 11 deletions packages/react/src/hooks/useAsyncProcessQueue/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { useCallback, useState, useRef } from 'react';

type RequestFunction<Data> = (requestData?: any) => Promise<Data>;
type RequestFunction<T> = (requestData?: any) => Promise<T>;

interface UseAsyncProcessQueueOptions {
keepPreviousData?: boolean;
}

export const useAsyncProcessQueue = <Data = unknown, Error = unknown>({
export const useAsyncProcessQueue = <T = unknown, E = unknown>({
keepPreviousData = false,
}: UseAsyncProcessQueueOptions = {}) => {
const requestQueue = useRef<RequestFunction<Data>[]>([]);
const requestQueue = useRef<RequestFunction<T>[]>([]);

const [data, setData] = useState<Data>();
const [error, setError] = useState<Error>();
const [data, setData] = useState<T | null>(null);
const [error, setError] = useState<E | null>(null);
const [isLoading, setIsLoading] = useState(false);

const handleRequestQueue = useCallback(async () => {
Expand All @@ -27,10 +27,10 @@ export const useAsyncProcessQueue = <Data = unknown, Error = unknown>({
const res = await requestFunc();

setData(res);
setError(undefined);
setError(null);
} catch (err) {
setData(undefined);
setError(err as Error);
setData(null);
setError(err as E);
} finally {
requestQueue.current.shift();
setIsLoading(false);
Expand All @@ -40,13 +40,13 @@ export const useAsyncProcessQueue = <Data = unknown, Error = unknown>({
}, []);

const addToProcessQueue = useCallback(
async (callbackFunc: RequestFunction<Data>) => {
async (callbackFunc: RequestFunction<T>) => {
requestQueue.current.push(callbackFunc);

if (requestQueue.current.length === 1) {
if (!keepPreviousData) {
setData(undefined);
setError(undefined);
setData(null);
setError(null);
}

await handleRequestQueue();
Expand Down

0 comments on commit 74fcb2f

Please sign in to comment.