queue-events-producer.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.QueueEventsProducer = void 0;
  4. const tslib_1 = require("tslib");
  5. const queue_base_1 = require("./queue-base");
  6. /**
  7. * The QueueEventsProducer class is used for publishing custom events.
  8. */
  9. class QueueEventsProducer extends queue_base_1.QueueBase {
  10. constructor(name, opts = {
  11. connection: {},
  12. }, Connection) {
  13. super(name, Object.assign({ blockingConnection: false }, opts), Connection);
  14. this.opts = opts;
  15. }
  16. /**
  17. * Publish custom event to be processed in QueueEvents.
  18. * @param argsObj - Event payload
  19. * @param maxEvents - Max quantity of events to be saved
  20. */
  21. async publishEvent(argsObj, maxEvents = 1000) {
  22. const client = await this.client;
  23. const key = this.keys.events;
  24. const { eventName } = argsObj, restArgs = tslib_1.__rest(argsObj, ["eventName"]);
  25. const args = ['MAXLEN', '~', maxEvents, '*', 'event', eventName];
  26. for (const [key, value] of Object.entries(restArgs)) {
  27. args.push(key, value);
  28. }
  29. await client.xadd(key, ...args);
  30. }
  31. /**
  32. * Closes the connection and returns a promise that resolves when the connection is closed.
  33. */
  34. async close() {
  35. if (!this.closing) {
  36. this.closing = this.connection.close();
  37. }
  38. await this.closing;
  39. }
  40. }
  41. exports.QueueEventsProducer = QueueEventsProducer;
  42. //# sourceMappingURL=queue-events-producer.js.map