Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const KafkaConnector = require('./kafka')
- const RedisConnector = require('./redis')
- const SocketConnector = require('./socket')
- const RoutesConnector = require('./routes')
- class ConnectorManager {
- constructor () {
- this.init()
- }
- init () {
- this.kafka = new KafkaConnector()
- this.redis = new RedisConnector()
- this.socket = new SocketConnector()
- this.routes = new RoutesConnector()
- }
- /**
- * wrapper for centralized event registration on particular connector
- * @param {*} connector
- * @param {*} evt
- * @param {*} cb
- */
- listen (connector, evt, cb) {
- let listenerCount
- if (connector && evt && cb) {
- if (this[connector]) {
- listenerCount = this[connector].listenerCount(evt)
- if (listenerCount < this[connector].getMaxListeners()) {
- this[connector].on(evt, cb)
- } else {
- throw new this.MaxListenerExceededException(connector, evt)
- }
- } else {
- throw new this.InvalidConnectorException(connector)
- }
- } else {
- throw new this.InvalidArgumentsException(connector)
- }
- }
- /**
- * wrapper for centralized event trigger on particular connector
- * @param {*} connector
- * @param {*} evt
- * @param {*} data
- */
- trigger (connector, evt, data) {
- if (connector && evt) {
- if (this[connector]) {
- console.log(
- `Emitting on ${connector} connector, ${evt} event, with data ${data}`
- )
- this[connector].emit(evt, data)
- } else {
- throw new this.InvalidConnectorException(connector)
- }
- } else {
- throw new this.InvalidArgumentsException(connector)
- }
- }
- MaxListenerExceededException (connector, event) {
- this.connector = connector
- this.event = event
- this.error = `Maximum listener count exceeded on ${connector} connector for event ${event}`
- }
- InvalidConnectorException (connector) {
- this.connector = connector
- this.error = 'Invalid Connector'
- }
- InvalidArgumentsException () {
- this.error = 'Invalid Arguments'
- }
- }
- module.exports = new ConnectorManager()
Add Comment
Please, Sign In to add comment