@er3tickets/common 0.0.39 → 0.0.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
- import { Kafka, Message } from "kafkajs";
1
+ import { Kafka, Message, Producer } from "kafkajs";
2
2
  import { Subjects } from "./subjects";
3
3
  import { Topics } from "./topics";
4
- import { OnModuleDestroy, OnModuleInit } from "@nestjs/common";
4
+ import { OnModuleDestroy } from "@nestjs/common";
5
5
  interface Event {
6
6
  topic: Topics;
7
7
  subject: Subjects;
@@ -11,13 +11,15 @@ export interface ProducerMessage<T extends Event> {
11
11
  key?: Message['key'];
12
12
  value: T['data'];
13
13
  }
14
- export declare abstract class BaseProducer<T extends Event> implements OnModuleInit, OnModuleDestroy {
14
+ export declare abstract class BaseProducer<T extends Event> implements OnModuleDestroy {
15
15
  protected abstract topic: T['topic'];
16
16
  protected abstract subject: T['subject'];
17
- private producer;
17
+ private readonly client;
18
+ private producer?;
19
+ private connectPromise?;
18
20
  private readonly logger;
19
21
  constructor(client: Kafka);
20
- onModuleInit(): Promise<void>;
22
+ protected getProducer(): Promise<Producer>;
21
23
  onModuleDestroy(): Promise<void>;
22
24
  send(messages: ProducerMessage<T>[]): Promise<void>;
23
25
  serializeMessage(messages: ProducerMessage<T>[]): {
@@ -3,23 +3,37 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseProducer = void 0;
4
4
  const common_1 = require("@nestjs/common");
5
5
  class BaseProducer {
6
+ client;
6
7
  producer;
8
+ connectPromise;
7
9
  logger = new common_1.Logger(BaseProducer.name);
8
10
  constructor(client) {
9
- this.producer = client.producer({});
11
+ this.client = client;
10
12
  }
11
- async onModuleInit() {
12
- await this.producer.connect();
13
- this.logger.log(`Producer connected: ${this.topic}`);
13
+ async getProducer() {
14
+ if (!this.producer) {
15
+ this.producer = this.client.producer({});
16
+ }
17
+ if (!this.connectPromise) {
18
+ this.connectPromise = this.producer.connect().catch((err) => {
19
+ this.connectPromise = undefined;
20
+ throw err;
21
+ });
22
+ }
23
+ await this.connectPromise;
24
+ return this.producer;
14
25
  }
15
26
  async onModuleDestroy() {
27
+ if (!this.producer)
28
+ return;
16
29
  await this.producer.disconnect();
17
30
  this.logger.log(`Producer disconnected: ${this.topic}`);
18
31
  }
19
32
  async send(messages) {
33
+ const producer = await this.getProducer();
20
34
  const serializedMessages = this.serializeMessage(messages);
21
35
  try {
22
- await this.producer.send({
36
+ await producer.send({
23
37
  topic: this.topic,
24
38
  messages: serializedMessages
25
39
  });
@@ -1 +1 @@
1
- {"version":3,"file":"base-producer.js","sourceRoot":"","sources":["../../src/events/base-producer.ts"],"names":[],"mappings":";;;AAGA,2CAAuE;AAavE,MAAsB,YAAY;IAGxB,QAAQ,CAAU;IACT,MAAM,GAAG,IAAI,eAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAA;IAEvD,YAAY,MAAa;QACvB,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC,EAG/B,CAAC,CAAA;IACJ,CAAC;IAED,KAAK,CAAC,YAAY;QAChB,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAA;QAC7B,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,uBAAuB,IAAI,CAAC,KAAK,EAAE,CAAC,CAAA;IACtD,CAAC;IAED,KAAK,CAAC,eAAe;QACnB,MAAM,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAA;QAChC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,0BAA0B,IAAI,CAAC,KAAK,EAAE,CAAC,CAAA;IACzD,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,QAA8B;QACvC,MAAM,kBAAkB,GAAG,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAA;QAE1D,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;gBACvB,KAAK,EAAE,IAAI,CAAC,KAAK;gBAE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