@minimaltech/node-infra
Version:
Minimal Technology NodeJS Infrastructure - Loopback 4 Framework
93 lines • 3.83 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocketIOClientHelper = void 0;
const helpers_1 = require("../../../helpers");
const utilities_1 = require("../../../utilities");
const socket_io_client_1 = require("socket.io-client");
class SocketIOClientHelper {
constructor(opts) {
this.logger = helpers_1.LoggerFactory.getLogger([SocketIOClientHelper.name]);
this.identifier = opts.identifier;
this.host = opts.host;
this.options = opts.options;
this.configure();
}
// -----------------------------------------------------------------
configure() {
if (this.client) {
this.logger.info('[configure][%s] SocketIO Client already established! Client: %j', this.identifier, this.client);
return;
}
this.client = (0, socket_io_client_1.io)(this.host, this.options);
}
// -----------------------------------------------------------------
getSocketClient() {
return this.client;
}
// -----------------------------------------------------------------
subscribe(opts) {
const { events: eventHandlers, ignoreDuplicate = false } = opts;
const eventNames = Object.keys(eventHandlers);
this.logger.info('[subscribe][%s] Handling events: %j', this.identifier, eventNames);
for (const eventName of eventNames) {
const handler = eventHandlers[eventName];
if (!handler) {
this.logger.info('[subscribe][%s] Ignore handling event %s because of no handler!', this.identifier, eventName);
continue;
}
if (ignoreDuplicate && this.client.hasListeners(eventName)) {
this.logger.info('[subscribe][%s] Ignore handling event %s because of duplicate handler!', this.identifier, eventName);
continue;
}
this.client.on(eventName, (...props) => {
handler(this.client, ...props);
});
}
}
// -----------------------------------------------------------------
unsubscribe(opts) {
var _a;
const { events: eventNames } = opts;
this.logger.info('[unsubscribe][%s] Handling events: %j', this.identifier, eventNames);
for (const eventName of eventNames) {
if (!((_a = this.client) === null || _a === void 0 ? void 0 : _a.hasListeners(eventName))) {
continue;
}
this.client.off(eventName);
}
}
// -----------------------------------------------------------------
connect() {
if (!this.client) {
this.logger.info('[connect][%s] Invalid client to connect!', this.identifier);
return;
}
this.client.connect();
}
// -----------------------------------------------------------------
disconnect() {
if (!this.client) {
this.logger.info('[disconnect][%s] Invalid client to disconnect!', this.identifier);
return;
}
this.client.disconnect();
}
// -----------------------------------------------------------------
emit(opts) {
var _a;
if (!((_a = this.client) === null || _a === void 0 ? void 0 : _a.connected)) {
throw (0, utilities_1.getError)({
statusCode: 400,
message: `[emit] Invalid socket client state to emit!`,
});
}
const { topic, message, doLog = false } = opts;
this.client.emit(topic, message);
if (!doLog) {
return;
}
this.logger.info('[emit][%s] Topic: %s | Message: %j', this.identifier, topic, message);
}
}
exports.SocketIOClientHelper = SocketIOClientHelper;
//# sourceMappingURL=socket-io-client.helper.js.map