UNPKG

react-application-core

Version:

A react-based application core for the business applications.

283 lines 12.2 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseChannel = void 0; var inversify_1 = require("inversify"); var ts_smart_logger_1 = require("ts-smart-logger"); var R = require("ramda"); var util_1 = require("../util"); var definitions_interface_1 = require("../definitions.interface"); var di_1 = require("../di"); var definition_1 = require("../definition"); var action_1 = require("../action"); var BaseChannel = /** @class */ (function () { function BaseChannel() { this.clients = new Map(); } BaseChannel_1 = BaseChannel; /** * @stable [04.11.2020] * @param ip */ BaseChannel.prototype.disconnect = function (ip) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.unregisterClient(ip)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * @stable [06.11.2020] * @param ip * @param client */ BaseChannel.prototype.onConnect = function (ip, client) { this.onMessage(ip, definition_1.CHANNEL_CONNECT_EVENT); BaseChannel_1.logger.info("[$BaseChannel][onConnect] The client has been connected successfully. Ip: " + ip); }; /** * @stable [06.11.2020] * @param ip * @param client */ BaseChannel.prototype.onDisconnect = function (ip, client) { this.onMessage(ip, definition_1.CHANNEL_DISCONNECT_EVENT); BaseChannel_1.logger.info("[$BaseChannel][onDisconnect] The client has been disconnected. Ip: " + ip); }; /** * @stable [06.11.2020] * @param ip * @param messageName * @param payload */ BaseChannel.prototype.onMessage = function (ip, messageName, payload) { BaseChannel_1.logger.debug('[$BaseChannel][onMessage] The client received the data', payload || '[-]', '. Ip:', ip, ', message:', messageName || '[-]'); this.appStore.dispatch(action_1.ChannelActionBuilder.buildReceiveMessagePlainAction(util_1.FilterUtils.defValuesFilter({ data: util_1.JsonUtils.parseJson(payload), ip: ip, name: messageName, }))); }; /** * @stable [04.11.2020] * @param ip * @param $event * @param args */ BaseChannel.prototype.emitEvent = function (ip, $event) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var client; return __generator(this, function (_a) { switch (_a.label) { case 0: client = this.clients.get(ip); if (!client) { throw new Error("The client " + ip + " doesn't exist!"); } BaseChannel_1.logger.debug(function () { return "[$BaseChannel][emitEvent] The client is about to send a message " + (args && JSON.stringify(args) || '[-]') + ". Ip: " + ip + ", event: " + $event; }); return [4 /*yield*/, client.emit.apply(client, __spreadArrays([$event], args))]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * @stable [06.11.2020] * @param ip * @param args */ BaseChannel.prototype.emitChannelEvent = function (ip) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.emitEvent.apply(this, __spreadArrays([ip, this.eventToEmit], args))]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * @stable [06.11.2020] * @param ip * @param requestPayload */ BaseChannel.prototype.emitRequestPayload = function (ip, requestPayload) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.emitChannelEvent(ip, JSON.stringify(requestPayload))]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * @stable [08.11.2020] * @param ip */ BaseChannel.prototype.hasClient = function (ip) { return this.clients.has(ip); }; /** * @stable [06.11.2020] * @param ip * @param client * @protected */ BaseChannel.prototype.registerClient = function (ip, client) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: this.clients.set(ip, client); return [4 /*yield*/, client.on(this.eventToListen, function (message) { return _this.onMessage(ip, definitions_interface_1.UNDEF, message); })]; case 1: _a.sent(); /**/ return [4 /*yield*/, client.on(definition_1.CHANNEL_CONNECT_EVENT, function () { return _this.onConnect(ip, client); })]; case 2: /**/ _a.sent(); return [4 /*yield*/, client.on(definition_1.CHANNEL_DISCONNECT_EVENT, function () { return _this.onDisconnect(ip, client); })]; case 3: _a.sent(); return [2 /*return*/]; } }); }); }; /** * @stable [06.11.2020] * @param ip * @protected */ BaseChannel.prototype.unregisterClient = function (ip) { return __awaiter(this, void 0, void 0, function () { var client; return __generator(this, function (_a) { switch (_a.label) { case 0: client = this.clients.get(ip); if (R.isNil(client)) { return [2 /*return*/]; } return [4 /*yield*/, client.close()]; case 1: _a.sent(); this.clients.delete(ip); BaseChannel_1.logger.debug("[$BaseChannel][unregisterClient] A registration has been canceled. Ip: " + ip); return [2 /*return*/]; } }); }); }; Object.defineProperty(BaseChannel.prototype, "eventToListen", { /** * @stable [06.11.2020] * @protected */ get: function () { return this.settings.channel.eventToListen; }, enumerable: false, configurable: true }); Object.defineProperty(BaseChannel.prototype, "eventToEmit", { /** * @stable [06.11.2020] * @protected */ get: function () { return this.settings.channel.eventToEmit; }, enumerable: false, configurable: true }); var BaseChannel_1; BaseChannel.logger = ts_smart_logger_1.LoggerFactory.makeLogger('BaseChannel'); __decorate([ di_1.lazyInject(di_1.DI_TYPES.Settings), __metadata("design:type", Object) ], BaseChannel.prototype, "settings", void 0); __decorate([ di_1.lazyInject(di_1.DI_TYPES.Store), __metadata("design:type", Object) ], BaseChannel.prototype, "appStore", void 0); BaseChannel = BaseChannel_1 = __decorate([ inversify_1.injectable() ], BaseChannel); return BaseChannel; }()); exports.BaseChannel = BaseChannel; //# sourceMappingURL=base-channel.service.js.map