-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
87 lines (69 loc) · 2.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
/**
* Load OST Notification module.
*/
const rootPrefix = '.',
version = require(rootPrefix + '/package.json').version,
rabbitmqHelper = require(rootPrefix + '/lib/rabbitmq/helper'),
OSTBase = require('@ostdotcom/base'),
coreConstant = require(rootPrefix + '/config/coreConstant');
const InstanceComposer = OSTBase.InstanceComposer;
require(rootPrefix + '/lib/rabbitmq/helper');
require(rootPrefix + '/lib/rabbitmq/connection');
require(rootPrefix + '/services/publishEvent');
require(rootPrefix + '/services/subscribeEvent');
/**
* OST Notification
*
* @param configStrategy
* @constructor
*/
const OSTNotification = function(configStrategy) {
const oThis = this;
if (!configStrategy) {
throw 'Mandatory argument configStrategy missing.';
}
const instanceComposer = (oThis.ic = new InstanceComposer(configStrategy));
oThis.version = version;
oThis.connection = instanceComposer.getInstanceFor(coreConstant.icNameSpace, 'rabbitmqConnection');
oThis.publishEvent = instanceComposer.getInstanceFor(coreConstant.icNameSpace, 'publishEvent');
oThis.subscribeEvent = instanceComposer.getInstanceFor(coreConstant.icNameSpace, 'subscribeEvent');
};
// Instance Map to ensure that only one object is created per config strategy.
const instanceMap = {};
const OSTNotificationFactory = function() {};
OSTNotificationFactory.prototype = {
/**
* Get an instance of OSTNotification
*
* @param configStrategy
* @returns {OSTNotification}
*/
getInstance: function(configStrategy) {
const oThis = this,
rabbitMqMandatoryParams = ['username', 'password', 'host', 'port', 'heartbeats'];
if (!configStrategy.hasOwnProperty('rabbitmq')) {
throw 'RabbitMQ one or more mandatory connection parameters missing.';
}
// Check if all the mandatory connection parameters for RabbitMQ are available or not.
for (let key = 0; key < rabbitMqMandatoryParams.length; key++) {
if (!configStrategy.rabbitmq.hasOwnProperty(rabbitMqMandatoryParams[key])) {
throw 'RabbitMQ one or more mandatory connection parameters missing.';
}
}
// Check if instance already present.
let instanceKey = rabbitmqHelper.getInstanceKey(configStrategy),
_instance = instanceMap[instanceKey];
if (!_instance) {
_instance = new OSTNotification(configStrategy);
instanceMap[instanceKey] = _instance;
}
_instance.connection.get();
return _instance;
}
};
const factory = new OSTNotificationFactory();
OSTNotification.getInstance = function() {
return factory.getInstance.apply(factory, arguments);
};
module.exports = OSTNotification;