@@ -4,6 +4,7 @@ const client_proxy_1 = require("./client-proxy");
44const logger_service_1 = require ( "@nestjs/common/services/logger.service" ) ;
55const constants_1 = require ( "./../constants" ) ;
66const load_package_util_1 = require ( "@nestjs/common/utils/load-package.util" ) ;
7+ const constants_2 = require ( "./constants" ) ;
78let natsPackage = { } ;
89class ClientNats extends client_proxy_1 . ClientProxy {
910 constructor ( options ) {
@@ -14,35 +15,6 @@ class ClientNats extends client_proxy_1.ClientProxy {
1415 this . getOptionsProp ( this . options , 'url' ) || constants_1 . NATS_DEFAULT_URL ;
1516 natsPackage = load_package_util_1 . loadPackage ( 'nats' , ClientNats . name ) ;
1617 }
17- async publish ( partialPacket , callback ) {
18- if ( ! this . natsClient ) {
19- await this . init ( callback ) ;
20- }
21- const packet = this . assignPacketId ( partialPacket ) ;
22- const pattern = JSON . stringify ( partialPacket . pattern ) ;
23- const responseChannel = this . getResPatternName ( pattern ) ;
24- const subscriptionHandler = ( message ) => {
25- if ( message . id !== packet . id ) {
26- return undefined ;
27- }
28- const { err, response, isDisposed } = message ;
29- if ( isDisposed || err ) {
30- callback ( {
31- err,
32- response : null ,
33- isDisposed : true ,
34- } ) ;
35- return this . natsClient . unsubscribe ( subscriptionId ) ;
36- }
37- callback ( {
38- err,
39- response,
40- } ) ;
41- } ;
42- const subscriptionId = this . natsClient . subscribe ( responseChannel , subscriptionHandler ) ;
43- this . natsClient . publish ( this . getAckPatternName ( pattern ) , packet ) ;
44- return subscriptionHandler ;
45- }
4618 getAckPatternName ( pattern ) {
4719 return `${ pattern } _ack` ;
4820 }
@@ -53,28 +25,54 @@ class ClientNats extends client_proxy_1.ClientProxy {
5325 this . natsClient && this . natsClient . close ( ) ;
5426 this . natsClient = null ;
5527 }
56- async init ( callback ) {
28+ async connect ( ) {
5729 this . natsClient = await this . createClient ( ) ;
58- this . handleError ( this . natsClient , callback ) ;
30+ return new Promise ( ( resolve , reject ) => {
31+ this . handleError ( this . natsClient ) ;
32+ this . connect$ ( this . natsClient )
33+ . subscribe ( resolve , reject ) ;
34+ } ) ;
5935 }
6036 createClient ( ) {
6137 const options = this . options . options || { } ;
62- const client = natsPackage . connect ( Object . assign ( { } , options , { url : this . url , json : true } ) ) ;
63- return new Promise ( resolve => client . on ( constants_1 . CONNECT_EVENT , resolve ) ) ;
38+ return natsPackage . connect ( Object . assign ( { } , options , { url : this . url , json : true } ) ) ;
6439 }
65- handleError ( client , callback ) {
66- const errorCallback = err => {
67- if ( err . code === 'ECONNREFUSED' ) {
68- callback ( err , null ) ;
69- this . natsClient = null ;
40+ handleError ( client ) {
41+ client . addListener ( constants_1 . ERROR_EVENT , err => err . code !== constants_2 . CONN_ERR && this . logger . error ( err ) ) ;
42+ }
43+ async publish ( partialPacket , callback ) {
44+ try {
45+ if ( ! this . natsClient ) {
46+ await this . connect ( ) ;
7047 }
71- this . logger . error ( err ) ;
72- } ;
73- client . addListener ( constants_1 . ERROR_EVENT , errorCallback ) ;
74- client . on ( constants_1 . CONNECT_EVENT , ( ) => {
75- client . removeListener ( constants_1 . ERROR_EVENT , errorCallback ) ;
76- client . addListener ( constants_1 . ERROR_EVENT , err => this . logger . error ( err ) ) ;
77- } ) ;
48+ const packet = this . assignPacketId ( partialPacket ) ;
49+ const pattern = JSON . stringify ( partialPacket . pattern ) ;
50+ const responseChannel = this . getResPatternName ( pattern ) ;
51+ const subscriptionHandler = ( message ) => {
52+ if ( message . id !== packet . id ) {
53+ return undefined ;
54+ }
55+ const { err, response, isDisposed } = message ;
56+ if ( isDisposed || err ) {
57+ callback ( {
58+ err,
59+ response : null ,
60+ isDisposed : true ,
61+ } ) ;
62+ return this . natsClient . unsubscribe ( subscriptionId ) ;
63+ }
64+ callback ( {
65+ err,
66+ response,
67+ } ) ;
68+ } ;
69+ const subscriptionId = this . natsClient . subscribe ( responseChannel , subscriptionHandler ) ;
70+ this . natsClient . publish ( this . getAckPatternName ( pattern ) , packet ) ;
71+ return subscriptionHandler ;
72+ }
73+ catch ( err ) {
74+ callback ( { err } ) ;
75+ }
7876 }
7977}
8078exports . ClientNats = ClientNats ;
0 commit comments