amqp-client-type-fix
Version:
AMQP 0-9-1 client, both for browsers (WebSocket) and node (TCP Socket)
67 lines • 2.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AMQPConsumer = void 0;
const amqp_error_js_1 = require("./amqp-error.js");
/**
* A consumer, subscribed to a queue
*/
class AMQPConsumer {
/**
* @param channel - the consumer is created on
* @param tag - consumer tag
* @param onMessage - callback executed when a message arrive
*/
constructor(channel, tag, onMessage) {
this.closed = false;
this.channel = channel;
this.tag = tag;
this.onMessage = onMessage;
}
/**
* Wait for the consumer to finish.
* @param [timeout] wait for this many milliseconds and then return regardless
* @return Fulfilled when the consumer/channel/connection is closed by the client. Rejected if the timeout is hit.
*/
wait(timeout) {
if (this.closedError)
return Promise.reject(this.closedError);
if (this.closed)
return Promise.resolve();
return new Promise((resolve, reject) => {
this.resolveWait = resolve;
this.rejectWait = reject;
if (timeout) {
const onTimeout = () => reject(new amqp_error_js_1.AMQPError("Timeout", this.channel.connection));
this.timeoutId = setTimeout(onTimeout, timeout);
}
});
}
/**
* Cancel/abort/stop the consumer. No more messages will be deliviered to the consumer.
* Note that any unacked messages are still unacked as they belong to the channel and not the consumer.
*/
cancel() {
return this.channel.basicCancel(this.tag);
}
/**
* @ignore
* @param [err] - why the consumer was closed
*/
setClosed(err) {
this.closed = true;
if (err)
this.closedError = err;
if (this.timeoutId)
clearTimeout(this.timeoutId);
if (err) {
if (this.rejectWait)
this.rejectWait(err);
}
else {
if (this.resolveWait)
this.resolveWait();
}
}
}
exports.AMQPConsumer = AMQPConsumer;
//# sourceMappingURL=amqp-consumer.js.map