Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing newline issue with pubsub.ts that caused lint to fail #16

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 45 additions & 31 deletions src/pubsub.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
'use strict';

const EventEmitter = require('events');
const nconf = require('nconf');

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.reset = exports.removeAllListeners = exports.on = exports.publish = void 0;
const events_1 = require("events");
const nconf_1 = __importDefault(require("nconf"));
let real;
let noCluster;
let singleHost;

function get() {
if (real) {
return real;
}

let pubsub;

if (!nconf.get('isCluster')) {
if (!nconf_1.default.get('isCluster')) {
if (noCluster) {
real = noCluster;
return real;
}
noCluster = new EventEmitter();
noCluster = new events_1.EventEmitter();
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
noCluster.publish = noCluster.emit.bind(noCluster);
pubsub = noCluster;
} else if (nconf.get('singleHostCluster')) {
}
else if (nconf_1.default.get('singleHostCluster')) {
if (singleHost) {
real = singleHost;
return real;
}
singleHost = new EventEmitter();
singleHost = new events_1.EventEmitter();
if (!process.send) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
singleHost.publish = singleHost.emit.bind(singleHost);
} else {
}
else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
singleHost.publish = function (event, data) {
process.send({
action: 'pubsub',
Expand All @@ -40,32 +45,41 @@ function get() {
};
process.on('message', (message) => {
if (message && typeof message === 'object' && message.action === 'pubsub') {
// eslint-disable-next-line max-len
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
singleHost.emit(message.event, message.data);
}
});
}
pubsub = singleHost;
} else if (nconf.get('redis')) {
}
else if (nconf_1.default.get('redis')) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
pubsub = require('./database/redis/pubsub');
} else {
}
else {
throw new Error('[[error:redis-required-for-pubsub]]');
}

real = pubsub;
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return pubsub;
}

module.exports = {
publish: function (event, data) {
get().publish(event, data);
},
on: function (event, callback) {
get().on(event, callback);
},
removeAllListeners: function (event) {
get().removeAllListeners(event);
},
reset: function () {
real = null;
},
};
function publish(event, data) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
get().publish(event, data);
}
exports.publish = publish;
function on(event, callback) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
get().on(event, callback);
}
exports.on = on;
function removeAllListeners(event) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
get().removeAllListeners(event);
}
exports.removeAllListeners = removeAllListeners;
function reset() {
real = null;
}
exports.reset = reset;
87 changes: 87 additions & 0 deletions src/pubsub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { EventEmitter } from 'events';

import nconf from 'nconf';

let real;
let noCluster;
let singleHost;
interface PubSubMessage {
action: string;
event: string;
data: unknown;
}
function get() {
if (real) {
return real as string;
}

let pubsub;

if (!nconf.get('isCluster')) {
if (noCluster) {
real = noCluster as string;
return real as string;
}
noCluster = new EventEmitter();
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
noCluster.publish = noCluster.emit.bind(noCluster) as string[];
pubsub = noCluster as string[];
} else if (nconf.get('singleHostCluster')) {
if (singleHost) {
real = singleHost as string;
return real as string;
}
singleHost = new EventEmitter();
if (!process.send) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
singleHost.publish = singleHost.emit.bind(singleHost) as string[];
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
singleHost.publish = function (event, data) {
process.send({
action: 'pubsub',
event: event as string,
data: data as string,
});
};
process.on('message', (message: PubSubMessage) => {
if (message && typeof message === 'object' && message.action === 'pubsub') {
// eslint-disable-next-line max-len
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
singleHost.emit(message.event, message.data);
}
});
}
pubsub = singleHost as string;
} else if (nconf.get('redis')) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
pubsub = require('./database/redis/pubsub');
} else {
throw new Error('[[error:redis-required-for-pubsub]]');
}

real = pubsub as string;
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return pubsub;
}
function publish(event, data) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
get().publish(event, data) as string[];
}
function on(event, callback) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
get().on(event, callback) as string[];
}
function removeAllListeners(event: EventEmitter) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
get().removeAllListeners(event) as string[];
}
function reset() {
real = null;
}
export {
publish,
on,
removeAllListeners,
reset,
};
Loading