UNPKG

amqp-client-type-fix

Version:

AMQP 0-9-1 client, both for browsers (WebSocket) and node (TCP Socket)

63 lines 2.01 kB
import { AMQPError } from './amqp-error.js'; /** * A consumer, subscribed to a queue */ export 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 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(); } } } //# sourceMappingURL=amqp-consumer.js.map