33 */
44
55import {
6- AnyAsyncFunc , AnyFunc , AwaitReturnType , AwaitType , EleType ,
6+ AnyAsyncFunc , AnyFunc , AwaitReturnType , AwaitType ,
77} from '../../typescript/utilityTypes' ;
88
99/**
@@ -27,7 +27,7 @@ export function retry<T extends AnyFunc>(
2727 times = 1 ,
2828 time = 0 ,
2929) : ( ...args : Parameters < T > ) => Promise < AwaitReturnType < T > > {
30- return async function ( ...args : Parameters < T > ) : Promise < AwaitReturnType < T > > {
30+ return async function retryFunction < This = any > ( this : This , ...args : Parameters < T > ) : Promise < AwaitReturnType < T > > {
3131 let syncResult : ReturnType < T > | Promise < AwaitReturnType < T > > ;
3232 let asyncResult : AwaitReturnType < T > ;
3333
@@ -82,7 +82,7 @@ export function timeout<T extends AnyAsyncFunc>(
8282 time = 0 ,
8383 onTimeout = ( ) => Promise . reject ( new TimeoutError ( ) ) ,
8484) {
85- return function ( ...args : Parameters < T > ) : ReturnType < T > {
85+ return function timeoutFunction < This = any > ( this : This , ...args : Parameters < T > ) : ReturnType < T > {
8686 const tasks = [ func . apply ( this , args ) ] ;
8787 if ( time > 0 ) {
8888 tasks . push ( wait ( time ) . then ( onTimeout ) ) ;
@@ -96,9 +96,9 @@ export function timeout<T extends AnyAsyncFunc>(
9696 * @param array Promise 数组
9797 * @return 当 array 中所有 Promise 状态都转化为 resolved 后 resolve
9898 */
99- export async function dynamicAll ( array : Promise < any > [ ] ) : Promise < EleType < typeof array > > {
99+ export async function dynamicAll < T = any > ( array : Promise < T > [ ] ) : Promise < T [ ] > {
100100 let { length } = array ;
101- let result : AwaitType < EleType < typeof array > > ;
101+ let result : AwaitType < T [ ] > ;
102102 while ( true ) {
103103 result = await Promise . all ( array ) ; // eslint-disable-line no-await-in-loop
104104 if ( array . length === length ) {
@@ -116,6 +116,17 @@ export enum PromiseActions {
116116 wait = 'wait' /* eslint-disable-line no-shadow */ ,
117117}
118118
119+ type DeduplicatedFunction < T extends AnyFunc , A extends PromiseActions , This = any > = (
120+ this : This ,
121+ ...args : Parameters < T >
122+ ) => A extends PromiseActions . resolve
123+ ? ReturnType < T > | Promise < void >
124+ : A extends PromiseActions . reject
125+ ? ReturnType < T >
126+ : A extends PromiseActions . wait
127+ ? ReturnType < T >
128+ : ReturnType < T > | Promise < never > ;
129+
119130/**
120131 * promise 函数去重
121132 * @param {Function } func 函数
@@ -127,24 +138,23 @@ export function deduplicate<T extends AnyFunc>(
127138 func : T ,
128139 action : PromiseActions = PromiseActions . pending ,
129140 getKey : ( args : Parameters < T > ) => any = JSON . stringify ,
130- ) : ( ... args : Parameters < T > ) => ReturnType < T > | Promise < void > {
141+ ) : DeduplicatedFunction < T , typeof action > {
131142 const pendingMap = new Map < ReturnType < typeof getKey > , true > ( ) ;
132143 const resultMap = new Map < ReturnType < typeof getKey > , ReturnType < T > > ( ) ;
133144
134- return function deduplicatedFunction ( ...params : Parameters < T > ) : ReturnType < T > | Promise < void > {
145+ return function deduplicatedFunction ( ...params ) {
135146 const key = getKey ( params ) ;
136147 if ( pendingMap . get ( key ) ) {
137148 switch ( action ) {
138149 case PromiseActions . resolve :
139150 return Promise . resolve ( ) ;
140151 case PromiseActions . reject :
141152 return Promise . reject ( ) ;
142- case PromiseActions . pending :
143- return new Promise ( ( ) => undefined ) ;
144153 case PromiseActions . wait :
145154 return resultMap . get ( key ) ;
155+ case PromiseActions . pending :
146156 default :
147- return undefined ;
157+ return new Promise ( ( ) => undefined ) ;
148158 }
149159 }
150160
@@ -153,29 +163,35 @@ export function deduplicate<T extends AnyFunc>(
153163
154164 if ( result instanceof Promise ) {
155165 pendingMap . set ( key , true ) ;
156- result . then ( ( ) => {
157- pendingMap . delete ( key ) ;
158- resultMap . delete ( key ) ;
159- } ) ;
166+ result
167+ . then ( ( ) => {
168+ pendingMap . delete ( key ) ;
169+ resultMap . delete ( key ) ;
170+ } )
171+ . catch ( ( ) => {
172+ pendingMap . delete ( key ) ;
173+ resultMap . delete ( key ) ;
174+ } ) ;
160175 }
161176
162177 return result as ReturnType < T > ;
163- } ;
178+ } as DeduplicatedFunction < T , typeof action > ;
164179}
165180
166181/**
167182 * 函数仅返回最新结果
168183 * @param {Function } func 函数
169184 * @return {Function } 只有最新结果会返回的函数
170185 */
171- export function keepUpToDate < T extends AnyFunc > (
172- func : T ,
173- ) : ( ...args : Parameters < T > ) => Promise < AwaitReturnType < T > > {
186+ export function keepUpToDate < T extends AnyFunc > ( func : T ) : ( ...args : Parameters < T > ) => Promise < AwaitReturnType < T > > {
174187 let newest : null | number = null ;
175- return async function upToDateFunction ( ...params : Parameters < T > ) : Promise < AwaitReturnType < T > > {
188+ return async function upToDateFunction < This = any > (
189+ this : This ,
190+ ...params : Parameters < T >
191+ ) : Promise < AwaitReturnType < T > > {
176192 newest = Date . now ( ) ;
177193 const current = newest ;
178- const result = await func . apply ( this , params ) as AwaitReturnType < T > ;
194+ const result = ( await func . apply ( this , params ) ) as AwaitReturnType < T > ;
179195 // 如果已经不是最新则丢弃结果
180196 if ( current !== newest ) return new Promise ( ( ) => null ) ;
181197 newest = null ;
0 commit comments