@@ -4,6 +4,9 @@ import { reactive, ref } from 'vue-demi'
4
4
5
5
export type UseAsyncQueueTask < T > = ( ...args : any [ ] ) => T | Promise < T >
6
6
7
+ type MapQueueTask < T extends any [ ] > = {
8
+ [ K in keyof T ] : UseAsyncQueueTask < T [ K ] >
9
+ }
7
10
export interface UseAsyncQueueResult < T > {
8
11
state : 'aborted' | 'fulfilled' | 'pending' | 'rejected'
9
12
data : T | null
@@ -47,19 +50,16 @@ export interface UseAsyncQueueOptions {
47
50
* @param tasks
48
51
* @param options
49
52
*/
50
- export function useAsyncQueue < T1 > ( tasks : [ UseAsyncQueueTask < T1 > ] , options ?: UseAsyncQueueOptions ) : UseAsyncQueueReturn < [ UseAsyncQueueResult < T1 > ] >
51
- export function useAsyncQueue < T1 , T2 > ( tasks : [ UseAsyncQueueTask < T1 > , UseAsyncQueueTask < T2 > ] , options ?: UseAsyncQueueOptions ) : UseAsyncQueueReturn < [ UseAsyncQueueResult < T1 > , UseAsyncQueueResult < T2 > ] >
52
- export function useAsyncQueue < T1 , T2 , T3 > ( tasks : [ UseAsyncQueueTask < T1 > , UseAsyncQueueTask < T2 > , UseAsyncQueueTask < T3 > ] , options ?: UseAsyncQueueOptions ) : UseAsyncQueueReturn < [ UseAsyncQueueResult < T1 > , UseAsyncQueueResult < T2 > , UseAsyncQueueResult < T3 > ] >
53
- export function useAsyncQueue < T1 , T2 , T3 , T4 > ( tasks : [ UseAsyncQueueTask < T1 > , UseAsyncQueueTask < T2 > , UseAsyncQueueTask < T3 > , UseAsyncQueueTask < T4 > ] , options ?: UseAsyncQueueOptions ) : UseAsyncQueueReturn < [ UseAsyncQueueResult < T1 > , UseAsyncQueueResult < T2 > , UseAsyncQueueResult < T3 > , UseAsyncQueueResult < T4 > ] >
54
- export function useAsyncQueue < T1 , T2 , T3 , T4 , T5 > ( tasks : [ UseAsyncQueueTask < T1 > , UseAsyncQueueTask < T2 > , UseAsyncQueueTask < T3 > , UseAsyncQueueTask < T4 > , UseAsyncQueueTask < T5 > ] , options ?: UseAsyncQueueOptions ) : UseAsyncQueueReturn < [ UseAsyncQueueResult < T1 > , UseAsyncQueueResult < T2 > , UseAsyncQueueResult < T3 > , UseAsyncQueueResult < T4 > , UseAsyncQueueResult < T5 > ] >
55
- export function useAsyncQueue < T > ( tasks : UseAsyncQueueTask < T > [ ] , options ?: UseAsyncQueueOptions ) : UseAsyncQueueReturn < UseAsyncQueueResult < T > [ ] >
56
- export function useAsyncQueue < T = any > ( tasks : UseAsyncQueueTask < any > [ ] , options : UseAsyncQueueOptions = { } ) : UseAsyncQueueReturn < UseAsyncQueueResult < T > [ ] > {
53
+ export function useAsyncQueue < T extends any [ ] , S = MapQueueTask < T > > (
54
+ tasks : S & Array < UseAsyncQueueTask < any > > ,
55
+ options ?: UseAsyncQueueOptions ,
56
+ ) : UseAsyncQueueReturn < { [ P in keyof T ] : UseAsyncQueueResult < T [ P ] > } > {
57
57
const {
58
58
interrupt = true ,
59
59
onError = noop ,
60
60
onFinished = noop ,
61
61
signal,
62
- } = options
62
+ } = options || { }
63
63
64
64
const promiseState : Record <
65
65
UseAsyncQueueResult < T > [ 'state' ] ,
@@ -73,7 +73,7 @@ export function useAsyncQueue<T = any>(tasks: UseAsyncQueueTask<any>[], options:
73
73
74
74
const initialResult = Array . from ( Array . from ( { length : tasks . length } ) , ( ) => ( { state : promiseState . pending , data : null } ) )
75
75
76
- const result = reactive ( initialResult ) as UseAsyncQueueResult < T > [ ]
76
+ const result = reactive ( initialResult ) as { [ P in keyof T ] : UseAsyncQueueResult < T [ P ] > }
77
77
78
78
const activeIndex = ref < number > ( - 1 )
79
79
0 commit comments