queue-events-producer.js 1.3 KB

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