@autofleet/rabbit 1.3.1 → 1.3.2
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.
- package/dist/index.d.ts +5 -1
- package/dist/index.js +10 -2
- package/package.json +1 -1
- package/src/index.ts +15 -2
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,9 @@ import { EventEmitter } from 'events';
|
|
|
4
4
|
interface ExchangesCache {
|
|
5
5
|
[key: string]: any;
|
|
6
6
|
}
|
|
7
|
+
interface QueuesCache {
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}
|
|
7
10
|
declare class RabbitMq {
|
|
8
11
|
static parseMsg(msg: any): any;
|
|
9
12
|
channel: ChannelWrapper | null;
|
|
@@ -11,13 +14,14 @@ declare class RabbitMq {
|
|
|
11
14
|
em: EventEmitter;
|
|
12
15
|
creatingConnection: Boolean;
|
|
13
16
|
exchanges: ExchangesCache;
|
|
17
|
+
queues: QueuesCache;
|
|
14
18
|
constructor();
|
|
15
19
|
ack: (msg: any) => Promise<void>;
|
|
16
20
|
nack: (queue: string) => (msg: any, retries: number) => Promise<void>;
|
|
17
21
|
getConnection(): Promise<AmqpConnectionManager>;
|
|
18
22
|
assertChannel(): Promise<ChannelWrapper>;
|
|
19
23
|
assertExchange(exchangeName: string, options?: any): Promise<any>;
|
|
20
|
-
assertQueue(
|
|
24
|
+
assertQueue(queueName: string, options?: any): Promise<any>;
|
|
21
25
|
consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
|
|
22
26
|
consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
|
|
23
27
|
publish(exchange: string, content: any): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -29,6 +29,7 @@ class RabbitMq {
|
|
|
29
29
|
this.connection = null;
|
|
30
30
|
this.creatingConnection = false;
|
|
31
31
|
this.exchanges = {};
|
|
32
|
+
this.queues = {};
|
|
32
33
|
}
|
|
33
34
|
static parseMsg(msg) {
|
|
34
35
|
let { content } = msg;
|
|
@@ -85,9 +86,16 @@ class RabbitMq {
|
|
|
85
86
|
return exchange;
|
|
86
87
|
});
|
|
87
88
|
}
|
|
88
|
-
async assertQueue(
|
|
89
|
+
async assertQueue(queueName, options) {
|
|
89
90
|
const channel = await this.assertChannel();
|
|
90
|
-
|
|
91
|
+
if (this.queues[queueName]) {
|
|
92
|
+
return this.queues[queueName];
|
|
93
|
+
}
|
|
94
|
+
return channel.addSetup(async (c) => {
|
|
95
|
+
const queue = await c.assertQueue(queueName);
|
|
96
|
+
this.queues[queueName] = queue;
|
|
97
|
+
return queue;
|
|
98
|
+
});
|
|
91
99
|
}
|
|
92
100
|
async consume(queue, callback, options) {
|
|
93
101
|
const { limit } = (options && options.limit) ? options : { limit: 1 };
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -6,6 +6,10 @@ interface ExchangesCache {
|
|
|
6
6
|
[key: string]: any
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
interface QueuesCache {
|
|
10
|
+
[key: string]: any
|
|
11
|
+
}
|
|
12
|
+
|
|
9
13
|
const connectionCreatedConst = 'connectionCreated';
|
|
10
14
|
class RabbitMq {
|
|
11
15
|
static parseMsg(msg: any) {
|
|
@@ -27,6 +31,7 @@ class RabbitMq {
|
|
|
27
31
|
em: EventEmitter;
|
|
28
32
|
creatingConnection: Boolean;
|
|
29
33
|
exchanges: ExchangesCache;
|
|
34
|
+
queues: QueuesCache;
|
|
30
35
|
|
|
31
36
|
constructor() {
|
|
32
37
|
this.em = new EventEmitter;
|
|
@@ -34,6 +39,7 @@ class RabbitMq {
|
|
|
34
39
|
this.connection = null;
|
|
35
40
|
this.creatingConnection = false;
|
|
36
41
|
this.exchanges = {};
|
|
42
|
+
this.queues = {};
|
|
37
43
|
}
|
|
38
44
|
|
|
39
45
|
public ack = async (msg: any) => {
|
|
@@ -106,9 +112,16 @@ class RabbitMq {
|
|
|
106
112
|
});
|
|
107
113
|
}
|
|
108
114
|
|
|
109
|
-
async assertQueue(
|
|
115
|
+
async assertQueue(queueName: string, options?: any) {
|
|
110
116
|
const channel: ChannelWrapper = await this.assertChannel();
|
|
111
|
-
|
|
117
|
+
if(this.queues[queueName]) {
|
|
118
|
+
return this.queues[queueName]
|
|
119
|
+
}
|
|
120
|
+
return channel.addSetup(async (c: ConfirmChannel) => {
|
|
121
|
+
const queue = await c.assertQueue(queueName)
|
|
122
|
+
this.queues[queueName] = queue;
|
|
123
|
+
return queue;
|
|
124
|
+
});
|
|
112
125
|
}
|
|
113
126
|
|
|
114
127
|
async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
|