UNPKG

playwright-pom-materials

Version:

Playwright POM materials

52 lines (51 loc) 3.07 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.retryUntilTimeout = void 0; const retryOptions_1 = require("./retryOptions"); /** * 引数 fn が true を返すまで処理を繰り返す関数。 * 繰り返し処理を行う最大時間をオプションの timeout で指定可能。 * * 例外が起きたらすぐに throw する場合は options.throwOn に 'catch' を追加する。 * timeoutを過ぎた際に例外を throw する場合は options.throwOn に 'timeout' を追加する。 * 例外が起きてもリトライし、timeoutを過ぎても例外を throw しない場合は options.throwOn に [] を指定する。 * * @param { {(): Promise<boolean>} } fn 実行に成功したら true を返す(Promiseを返す)関数 * @param [options] * @param {number} [options.timeout] タイムアウトをミリ秒で指定。デフォルトは 2 * 60 * config.timeoutUnit * @param {number} [options.interval] fnを実行するまでの間隔。ミリ秒で指定。デフォルトは 10 * config.timeoutUnit * @param { {(number): Promise<void>} } [options.intervalFunc] fnを実行するまでの間隔を指定する関数。デフォルトは sleep * @param { ('catch'|'timeout')[] } [options.throwOn] 例外が起きた場合にthrowするタイミング。デフォルトは ['timeout'] * @param { {(errors: unknown[]): Promise<void>} } [options.errorFunc] 例外が起きた場合に呼び出される関数。デフォルトでは、最後のエラー あるいは 'Timeout Error' というErrorをthrowする */ const retryUntilTimeout = (fn, options) => __awaiter(void 0, void 0, void 0, function* () { const opts = Object.assign(Object.assign({}, retryOptions_1.defaultRetryUntilTimeoutOptions), options); const t0 = Date.now(); const errors = []; while (Date.now() - t0 < opts.timeout) { try { const x = yield fn(); if (x) return; } catch (err) { errors.push(err); if (opts.throwOn.includes('catch')) yield opts.errorFunc(errors); console.debug('retry function ignored an error: ', err); } yield opts.intervalFunc(opts.interval); } if (opts.throwOn.includes('timeout')) yield opts.errorFunc(errors); }); exports.retryUntilTimeout = retryUntilTimeout;