forked from marcghorayeb/event-sourcing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchange-publisher.js
73 lines (57 loc) · 2.16 KB
/
exchange-publisher.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
var uuid = require('node-uuid');
var inherits = require('util').inherits;
var Publisher = require('./publisher');
function ExchangePublisher(config) {
if (!this.exchange) throw new Error('Missing exchange property');
if (!this.exchangeType) throw new Error('Missing exchangeType property');
this.domain = 'bleh';
ExchangePublisher.super_.call(this, config);
}
inherits(ExchangePublisher, Publisher);
ExchangePublisher.prototype.connect = function (callback) {
var self = this;
this.storage.initAdapters(function (err) {
if (err) return callback(err);
self.amqp.connect().then(function (channel) {
self.channel = channel;
self.channel.assertExchange(self.exchange, self.exchangeType).then(function (exchangeAssertion) {
return callback(null, channel);;
});
}, callback);
});
};
ExchangePublisher.prototype.askRPC = function (event, routingKey, callback) {
var self = this;
this.persistEvent(event, function (err) {
if (err) return callback(err);
self.emitEvent(event, routingKey, callback);
});
};
ExchangePublisher.prototype.persistEvent = function (event, callback) {
this.storage.persistEvent(event, callback);
};
ExchangePublisher.prototype.assertReplyExchange = function () {
return this.channel.assertExchange(this.exchange, this.exchangeType).then(function (exchangeAssertion) {
return exchangeAssertion.exchange;
});
};
ExchangePublisher.prototype.emitEvent = function (event, routingKey, callback) {
event = new Buffer(JSON.stringify(event));
if (!callback) return this.channel.publish(this.exchange, routingKey, event);
throw new Error('emitEvent code with callback parameter not implemented yet');
// TODO code below
//var self = this;
//var rpcConfig = { correlationId: uuid.v4(), replyTo: null };
//var rpcCallback = function (msg) {
// if (msg.properties.correlationId !== rpcConfig.correlationId) return;
// callback(msg);
//};
//
//this.assertReplyExchange().then(function (exchangeName) {
// self.channel.consume(responseQ, rpcCallback, { noAck: true }).then(function () {
// rpcConfig.replyTo = responseQ;
// self.channel.sendToQueue(queue, event, rpcConfig);
// });
//});
};
module.exports = ExchangePublisher;