peanar
Version:
A job queue for Node.js based on RabbitMQ
57 lines (56 loc) • 1.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const stream_1 = require("stream");
class Consumer extends stream_1.Readable {
tag;
queue;
_channel;
constructor(channel, queue) {
super({
objectMode: true
});
this._channel = channel;
this.queue = queue;
}
get channel() {
return this._channel;
}
set channel(ch) {
this._channel = ch;
this.emit('channelChanged', ch);
}
async cancel() {
if (!this.tag) {
throw new Error('Consumer is not yet started or the consumer tag is not yet set!');
}
await this.channel.cancel(this.tag);
this.removeAllListeners('channelChanged');
this.handleCancel(false);
}
handleDelivery(delivery) {
const msg = {
body: delivery.content,
envelope: {
deliveryTag: BigInt(delivery.fields.deliveryTag),
exchange: delivery.fields.exchange,
routingKey: delivery.fields.routingKey,
redeliver: delivery.fields.redelivered
},
properties: delivery.properties
};
if (this.isPaused()) {
this.once('resume', () => {
this.push(msg);
});
}
else {
this.push(msg);
}
}
handleCancel(server) {
this.emit('cancel', { server });
this.destroy();
}
_read() { }
}
exports.default = Consumer;