Source

AWS/waitForAWSService.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_INTERVAL = exports.DEFAULT_MAX_TRY_TIME = void 0;
// 25 times
exports.DEFAULT_MAX_TRY_TIME = 25;
// Every 20 seconds
exports.DEFAULT_INTERVAL = 20000;
/** @ignore   Wait for service async */
function describeRecur(_a) {
    var describe = _a.describe, input = _a.input, _b = _a.retryCounter, retryCounter = _b === void 0 ? 0 : _b, hasService = _a.hasService, callback = _a.callback, maxTryTime = _a.maxTryTime, interval = _a.interval;
    describe(input, function (err, data) {
        if (err || !hasService(data)) {
            // Abort if retry time reaches maximum
            if (retryCounter >= maxTryTime) {
                if (err)
                    callback(err);
                else
                    callback(null);
            }
            else {
                // Pass resolve as a callback and increment `retryCounter`
                setTimeout(function () {
                    describeRecur({
                        describe: describe,
                        input: input,
                        retryCounter: retryCounter + 1,
                        hasService: hasService,
                        callback: callback,
                        maxTryTime: maxTryTime,
                        interval: interval,
                    });
                }, interval);
            }
        }
        else {
            callback(null, data);
        }
    });
}
/**
 * Wait for some service to be active.
 * This is a supplemental help for some services which does not natively have a `waitFor` helper.
 *
 * Example:
 *
 * ```
 * const dynamodbStreams = new AWS.DynamoDBStreams();
  type I = AWS.DynamoDBStreams.DescribeStreamInput
  type O = AWS.DynamoDBStreams.DescribeStreamOutput
  export type Result = O
  const waitForStream = (input: I): Promise<null | Result> => waitForAWSService<I, O, AWS.AWSError>(
  // Prevent `this` context problem
  (...args) => dynamodbStreams.describeStream(...args),
  input,
  data => !!data?.StreamDescription
  )
 * ```
 * @category AWS
 * @module waitForAWSService
 */
function waitForAWSService(describe, input, hasService, _a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.maxTryTime, maxTryTime = _c === void 0 ? exports.DEFAULT_MAX_TRY_TIME : _c, _d = _b.interval, interval = _d === void 0 ? exports.DEFAULT_INTERVAL : _d;
    return new Promise(function (resolve, reject) {
        var callback = function (err, data) {
            if (err)
                reject(err);
            else
                resolve(data !== null && data !== void 0 ? data : null);
        };
        describeRecur({
            describe: describe,
            input: input,
            retryCounter: 0,
            hasService: hasService,
            callback: callback,
            maxTryTime: maxTryTime,
            interval: interval,
        });
    });
}
exports.default = waitForAWSService;