UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

63 lines (62 loc) 2.56 kB
'use strict'; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const error_1 = __importDefault(require("../../../error")); const runtime_store_1 = __importDefault(require("../../../runtime-store")); const constants_1 = require("../../../util_modules/constants"); const events_1 = __importDefault(require("events")); const logger_1 = require("../../../util_modules/logger"); const slave_manager_1 = require("./slave-manager"); class LocalFnEvents extends events_1.default { } class NonHttpFunction { constructor(repl, target) { var _a, _b, _c, _d; this.localFnEvents = new LocalFnEvents(); if (target.type === undefined || !Object.values(constants_1.FN_TYPE).includes(target.type)) { throw new error_1.default('target type is not defined', { exit: 2 }); } this.repl = repl; this.fn = { httpPort: -1, debugPort: runtime_store_1.default.get('context.port.debug.' + constants_1.FN_TYPE.basic, -1), type: 'functions', target, isAlive: null, restarting: false }; this.slaveManager = new slave_manager_1.SlaveManager(this.fn, this.repl, this.localFnEvents); this.call = this.slaveManager.call; (_b = (_a = this.fn.target) === null || _a === void 0 ? void 0 : _a.watcher) === null || _b === void 0 ? void 0 : _b.on('preparing', () => { if (!this.repl.paused) { this.repl.pause(); } this.fn.restarting = true; this.slaveManager.kill(); }); (_d = (_c = this.fn.target) === null || _c === void 0 ? void 0 : _c.watcher) === null || _d === void 0 ? void 0 : _d.on('compiled', () => { this.fn.restarting = false; (0, logger_1.labeled)(`functions[${target.name}]`, 'ready!').MESSAGE(); if (this.repl.paused) { repl.resume(); repl.showPrompt(); } setTimeout(() => { var _a; (_a = this.slaveManager.watcher) === null || _a === void 0 ? void 0 : _a.emit('next'); }, 1000); }); } once(event, fn) { this.localFnEvents.once(event, fn); return this; } removeListener(event, fn) { this.localFnEvents.removeListener(event, fn); return this; } } exports.default = NonHttpFunction;