vulcain-corejs
Version: 
Vulcain micro-service framework
49 lines (47 loc) • 2.29 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments)).next());
    });
};
const commandMetricsFactory_1 = require("../metrics/commandMetricsFactory");
const circuitBreaker_1 = require("./circuitBreaker");
const command_1 = require("./command");
const commandProperties_1 = require("./commandProperties");
const hystrixCommandsCache = new Map();
/**
 * Command attribute
 */
function Command(config = {}, commandKey, commandGroup) {
    return function (command) {
        commandGroup = commandGroup || "hystrix";
        commandKey = commandKey || command.name;
        let properties = new commandProperties_1.CommandProperties(commandKey, commandGroup, config);
        commandMetricsFactory_1.CommandMetricsFactory.getOrCreate(properties); // register command - do not delete this line
        circuitBreaker_1.CircuitBreakerFactory.getOrCreate(properties); // register command - do not delete this line
        hystrixCommandsCache.set(commandKey, { properties, command });
    };
}
exports.Command = Command;
class CommandFactory {
    static getAsync(commandKey, context, schema) {
        return __awaiter(this, void 0, void 0, function* () {
            let cache = hystrixCommandsCache.get(commandKey);
            if (cache) {
                let resolvedCommand = context.container.resolve(cache.command);
                let cmd = new command_1.HystrixCommand(cache.properties, resolvedCommand, context);
                yield cmd.setSchemaOnCommandAsync(schema);
                return cmd;
            }
            throw new Error(`Command ${commandKey} not found`);
        });
    }
    static resetCache() {
        hystrixCommandsCache.clear();
    }
}
exports.CommandFactory = CommandFactory;
//# sourceMappingURL=commandFactory.js.map