UNPKG

@node-elion/utils

Version:

Super scalable enterprise Node.js server library

98 lines 4.64 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.BindFactory = void 0; /* eslint-disable max-classes-per-file */ const logger_1 = require("../logger"); class BindFactory { constructor(executor, additionalContext) { this.additionalContext = {}; this.additionalContext = additionalContext; this.executor = executor; return this.emulateFunction(); } emulateFunction() { return Object.assign((...params) => __awaiter(this, void 0, void 0, function* () { return this.executor.bind(this.additionalContext)(...params); }), Object.assign(Object.assign({}, this), { bind: this.bind, emulateFunction: this.emulateFunction })); } bind(context) { this.additionalContext = Object.assign(Object.assign({}, this.additionalContext), context); return this.emulateFunction(); } } exports.BindFactory = BindFactory; class ChainAction { constructor(options) { var _a, _b, _c, _d; this.__chainActions = true; this.options = { logger: (options === null || options === void 0 ? void 0 : options.logger) ? options.logger : new logger_1.Logger(), chainName: (_a = options === null || options === void 0 ? void 0 : options.chainName) !== null && _a !== void 0 ? _a : "", ableRollback: (_b = options === null || options === void 0 ? void 0 : options.ableRollback) !== null && _b !== void 0 ? _b : true, optimizeSubChainActions: (_c = options === null || options === void 0 ? void 0 : options.optimizeSubChainActions) !== null && _c !== void 0 ? _c : true, usePreBind: (_d = options === null || options === void 0 ? void 0 : options.usePreBind) !== null && _d !== void 0 ? _d : false, }; this.logger = this.options.logger; this.actions = []; } setExecutorContext(executor) { return executor.bind({ __step: { logger: this.logger, chainName: this.options.chainName, abort: this.abort.bind(this), }, }); } setRollbackContext(rollback) { return rollback.bind({ __step: { logger: this.logger, chainName: this.options.chainName, }, }); } step(executor, rollback, options) { var _a, _b; this.actions.push({ executor: { call: this.setExecutorContext(executor), }, rollback: rollback ? { call: this.setRollbackContext(rollback), } : null, options: { abortOnFail: (_a = options === null || options === void 0 ? void 0 : options.abortOnFail) !== null && _a !== void 0 ? _a : true, name: (_b = options === null || options === void 0 ? void 0 : options.name) !== null && _b !== void 0 ? _b : "", }, }); return Object.assign((...params) => __awaiter(this, void 0, void 0, function* () { return this.execute.bind(this)(...params); }), this); } stepFactory(factory) { const { executor, rollback, options } = factory({ executorBind: (executorFunction, additionalContext) => new BindFactory(executorFunction, additionalContext), rollbackBind: (rollbackFunction, additionalContext) => new BindFactory(rollbackFunction, additionalContext), }); return this.step(executor, rollback, options); } upgradeLogger(logger) { this.logger = logger.child({ name: this.options.chainName }); return this; } // public turnToSubChain(): ChainAction {} // eslint-disable-next-line class-methods-use-this,@typescript-eslint/no-unused-vars execute(...params) { } // eslint-disable-next-line class-methods-use-this abort() { } } exports.default = ChainAction; //# sourceMappingURL=chainActions.js.map