( fn: () => Promise<T>, acceptFn: (result: T) => boolean, timeoutMessage: string, retryCount = 200, retryInterval = 100 // millis )
| 351 | } |
| 352 | |
| 353 | private async poll<T>( |
| 354 | fn: () => Promise<T>, |
| 355 | acceptFn: (result: T) => boolean, |
| 356 | timeoutMessage: string, |
| 357 | retryCount = 200, |
| 358 | retryInterval = 100 // millis |
| 359 | ): Promise<T> { |
| 360 | let trial = 1; |
| 361 | let lastError: string = ''; |
| 362 | |
| 363 | while (true) { |
| 364 | if (trial > retryCount) { |
| 365 | this.logger.log('Timeout!'); |
| 366 | this.logger.log(lastError); |
| 367 | this.logger.log(`Timeout: ${timeoutMessage} after ${(retryCount * retryInterval) / 1000} seconds.`); |
| 368 | |
| 369 | throw new Error(`Timeout: ${timeoutMessage} after ${(retryCount * retryInterval) / 1000} seconds.`); |
| 370 | } |
| 371 | |
| 372 | let result; |
| 373 | try { |
| 374 | result = await fn(); |
| 375 | if (acceptFn(result)) { |
| 376 | return result; |
| 377 | } else { |
| 378 | lastError = 'Did not pass accept function'; |
| 379 | } |
| 380 | } catch (e: any) { |
| 381 | lastError = Array.isArray(e.stack) ? e.stack.join(os.EOL) : e.stack; |
| 382 | } |
| 383 | |
| 384 | await this.wait(retryInterval); |
| 385 | trial++; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | export function findElement(element: IElement, fn: (element: IElement) => boolean): IElement | null { |
no test coverage detected