forked from briankircho/easy-amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyamqp.js
59 lines (49 loc) · 1.28 KB
/
easyamqp.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
var amqp = require('amqp'),
Chain = require('./chain'),
events = require('events'),
util = require('util');
var easyamqp = module.exports = function(options, implOptions) {
events.EventEmitter.call(this);
if(typeof(options) === "string") {
options = { url : options };
}
this.options = options;
this.implOptions = implOptions;
this.connQueue = [];
return this;
}
util.inherits(easyamqp, events.EventEmitter);
easyamqp.prototype.connection = function(readyCallback) {
if(!this.conn) {
var self = this;
this.conn = amqp.createConnection(this.options, this.implOptions);
this.conn.on('ready', function() {
self.ready = true;
self.connQueue.forEach(function(readyCallback) {
readyCallback(self.conn);;
})
});
this.conn.on('error', function(err) {
if(self.listeners('error').length == 0) {
throw err;
}
self.emit('error', err);
});
}
if(!this.ready) {
this.connQueue.push(readyCallback);
} else {
readyCallback(this.conn);
}
return this.conn;
}
easyamqp.prototype.end = function() {
if(this.conn) {
this.conn.end();
}
};
['queue', 'exchange'].forEach(function(method) {
easyamqp.prototype[method] = function() {
return new Chain(this, method, arguments);
}
});