"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.asyncCommand = exports.AsyncCommand = void 0;
const promises_1 = require("timers/promises");
const models_1 = require("../models");
class AsyncCommand extends models_1.Command {
}
exports.AsyncCommand = AsyncCommand;
const asyncCommand = () => (onExecute) => {
    return ((data, context) => {
        let result = onExecute
            ? onExecute(data, context)
            : context.handler(data, context.options);
        if (context.options.timeout) {
            result = Promise.race([
                result,
                (0, promises_1.setTimeout)(context.options.timeout).then(() => {
                    throw new Error(`AsyncCommand Timeout Error: timeout of ${context.options.timeout} reached for command ${context.name}`);
                }),
            ]);
        }
        if (!result || !(result instanceof Promise)) {
            throw new Error('AsyncCommandHandler must return a promise');
        }
        return result;
    });
};
exports.asyncCommand = asyncCommand;
//# sourceMappingURL=asyncCommand.js.map