Skip to content

Commit ca1bea1

Browse files
committed
fix: sinqle queue support
1 parent a129b35 commit ca1bea1

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/IMQClient.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ export abstract class IMQClient extends EventEmitter {
6565
public readonly queueName: string;
6666

6767
private readonly baseName: string;
68-
private imq: IMessageQueue;
69-
private static singleImq: IMessageQueue;
68+
private readonly imq: IMessageQueue;
69+
private readonly subscriptionImq: IMessageQueue;
70+
private static singleImq: IMessageQueue & { name?: string};
7071
private readonly logger: ILogger;
7172
private resolvers: { [id: string]: [
7273
(data: AnyJson, res: IMQRPCResponse) => void,
@@ -101,11 +102,13 @@ export abstract class IMQClient extends EventEmitter {
101102
this.options = { ...DEFAULT_IMQ_CLIENT_OPTIONS, ...options };
102103
this.id = pid(baseName);
103104
this.logger = this.options.logger || /* istanbul ignore next */ console;
104-
this.hostName = `${osUuid()}-${this.id}:client`;
105+
this.hostName = IMQClient.singleImq?.name ||
106+
`${osUuid()}-${this.id}:client`;
105107
this.name = `${baseName}-${this.hostName}`;
106108
this.serviceName = serviceName || baseName.replace(/Client$/, '');
107109
this.queueName = this.options.singleQueue ? this.hostName : this.name;
108110
this.imq = this.createImq();
111+
this.subscriptionImq = this.createSubscriptionImq();
109112

110113
SIGNALS.forEach((signal: any) => process.on(signal, async () => {
111114
this.destroy().catch(this.logger.error);
@@ -126,6 +129,14 @@ export abstract class IMQClient extends EventEmitter {
126129
return IMQClient.singleImq;
127130
}
128131

132+
private createSubscriptionImq(): IMessageQueue {
133+
if (!this.options.singleQueue) {
134+
return this.imq;
135+
}
136+
137+
return IMQ.create(this.name, this.options);
138+
}
139+
129140
/**
130141
* Sends call to remote service method
131142
*
@@ -200,7 +211,7 @@ export abstract class IMQClient extends EventEmitter {
200211
* @return {Promise<void>}
201212
*/
202213
public async subscribe(handler: (data: JsonObject) => any): Promise<void> {
203-
return this.imq.subscribe(this.queueName, handler);
214+
return this.subscriptionImq.subscribe(this.name, handler);
204215
}
205216

206217
// noinspection JSUnusedGlobalSymbols
@@ -210,7 +221,7 @@ export abstract class IMQClient extends EventEmitter {
210221
* @return {Promise<void>}
211222
*/
212223
public async unsubscribe(): Promise<void> {
213-
return this.imq.unsubscribe();
224+
return this.subscriptionImq.unsubscribe();
214225
}
215226

216227
// noinspection JSUnusedGlobalSymbols
@@ -257,7 +268,9 @@ export abstract class IMQClient extends EventEmitter {
257268
resolve && resolve(message.data, message);
258269
});
259270

260-
await this.imq.start();
271+
if (this.imq) {
272+
await this.imq.start();
273+
}
261274
}
262275

263276
// noinspection JSUnusedGlobalSymbols

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy