@@ -15,6 +15,7 @@ import { UpdateOneMessage } from "./Messages/UpdateOneMessage";
1515import { DeleteOneMessage } from "./Messages/DeleteOneMessage" ;
1616import { Base } from "./base" ;
1717import { UpdateManyMessage } from "./Messages/UpdateManyMessage" ;
18+ import { Util } from "./Util" ;
1819
1920interface IHashTable < T > {
2021 [ key : string ] : T ;
@@ -90,7 +91,8 @@ export class WebSocketClient {
9091 }
9192 public async CreateConsumer ( queuename : string ) : Promise < void > {
9293 var autoDelete : boolean = false ;
93- if ( queuename === null || queuename === undefined || queuename === "" ) { queuename = "web." + Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) ; autoDelete = true ; }
94+
95+ if ( Util . IsNullEmpty ( queuename ) ) { queuename = "web." + Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) ; autoDelete = true ; }
9496 var consumer = new amqp_consumer ( this . _logger , Config . amqp_url , queuename ) ;
9597 consumer . OnMessage = this . OnMessage . bind ( this ) ;
9698 this . consumers . push ( consumer ) ;
@@ -120,7 +122,7 @@ export class WebSocketClient {
120122 }
121123 }
122124 public async sendToQueue ( msg : QueueMessage ) {
123- if ( msg . queuename === null || msg . queuename === undefined || msg . queuename === "" ) { throw new Error ( "sendToQueue, queuename is mandatory" ) }
125+ if ( Util . IsNullEmpty ( msg . queuename ) ) { throw new Error ( "sendToQueue, queuename is mandatory" ) }
124126 if ( this . consumers . length === 0 ) { throw new Error ( "No consumers for client available to send message through" ) }
125127 var result = this . consumers [ 0 ] . sendToQueue ( msg . queuename , msg . correlationId , { payload : msg . data , jwt : this . jwt } ) ;
126128 }
@@ -155,6 +157,8 @@ export class WebSocketClient {
155157 return true ;
156158 } catch ( error ) {
157159 this . _logger . error ( "WebSocketclient::WebSocket error encountered " + error ) ;
160+ this . _receiveQueue = [ ] ;
161+ this . _sendQueue = [ ] ;
158162 this . CloseConsumers ( ) ;
159163 return false ;
160164 }
@@ -180,7 +184,7 @@ export class WebSocketClient {
180184 } else {
181185 var buffer : string = "" ;
182186 msgs . forEach ( msg => {
183- if ( msg . data !== null && msg . data !== undefined ) { buffer += msg . data ; }
187+ if ( ! Util . IsNullUndefinded ( msg . data ) ) { buffer += msg . data ; }
184188 } ) ;
185189 this . _receiveQueue = this . _receiveQueue . filter ( function ( msg : SocketMessage ) : boolean { return msg . id !== id ; } ) ;
186190 var result : Message = Message . frommessage ( first , buffer ) ;
@@ -201,35 +205,35 @@ export class WebSocketClient {
201205 this . _sendQueue = this . _sendQueue . filter ( function ( msg : SocketMessage ) : boolean { return msg . id !== id ; } ) ;
202206 } ) ;
203207 if ( this . _receiveQueue . length > 25 || this . _sendQueue . length > 25 ) {
204- if ( this . user !== null && this . user !== undefined ) {
208+ if ( ! Util . IsNullUndefinded ( this . user ) ) {
205209 this . _logger . debug ( "[" + this . user . username + "] WebSocketclient::ProcessQueue receiveQueue: " + this . _receiveQueue . length + " sendQueue: " + this . _sendQueue . length ) ;
206210 }
207211 }
208212 }
209213 public async Send < T > ( message : Message ) : Promise < T > {
210214 return new Promise < T > ( async ( resolve , reject ) => {
211215 this . _Send ( message , ( ( msg ) => {
212- if ( msg . error !== null && msg . error !== undefined ) { return reject ( msg . error ) ; }
216+ if ( ! Util . IsNullUndefinded ( msg . error ) ) { return reject ( msg . error ) ; }
213217 resolve ( msg ) ;
214218 } ) . bind ( this ) ) ;
215219 } ) ;
216220 }
217221 private _Send ( message : Message , cb : QueuedMessageCallback ) : void {
218222 var messages : string [ ] = this . chunkString ( message . data , 500 ) ;
219- if ( messages === null || messages === undefined || messages . length === 0 ) {
223+ if ( Util . IsNullUndefinded ( messages ) || messages . length === 0 ) {
220224 var singlemessage : SocketMessage = SocketMessage . frommessage ( message , "" , 1 , 0 ) ;
221- if ( message . replyto === null || message . replyto === undefined ) {
225+ if ( Util . IsNullEmpty ( message . replyto ) ) {
222226 this . messageQueue [ singlemessage . id ] = new QueuedMessage ( singlemessage , cb ) ;
223227 }
224228 this . _sendQueue . push ( singlemessage ) ;
225229 return ;
226230 }
227- if ( message . id === null || message . id === undefined ) { message . id = Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) ; }
231+ if ( Util . IsNullEmpty ( message . id ) ) { message . id = Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) ; }
228232 for ( let i : number = 0 ; i < messages . length ; i ++ ) {
229233 var _message : SocketMessage = SocketMessage . frommessage ( message , messages [ i ] , messages . length , i ) ;
230234 this . _sendQueue . push ( _message ) ;
231235 }
232- if ( message . replyto === null || message . replyto === undefined ) {
236+ if ( Util . IsNullEmpty ( message . replyto ) ) {
233237 this . messageQueue [ message . id ] = new QueuedMessage ( message , cb ) ;
234238 }
235239 // setTimeout(() => {
@@ -238,33 +242,24 @@ export class WebSocketClient {
238242 this . ProcessQueue ( ) ;
239243 }
240244 public chunkString ( str : string , length : number ) : string [ ] {
241- if ( str === null || str === undefined ) { return null ; }
245+ if ( Util . IsNullEmpty ( str ) ) { return null ; }
242246 // tslint:disable-next-line: quotemark
243247 return str . match ( new RegExp ( '.{1,' + length + '}' , 'g' ) ) ;
244248 }
245-
246-
247-
248-
249249 async Queue ( data : string , replyTo : string , correlationId : string , queuename : string ) : Promise < any [ ] > {
250250 var d : any = JSON . parse ( data ) ;
251251 var q : QueueMessage = new QueueMessage ( ) ;
252252 q . data = d . payload ; q . replyto = replyTo ;
253253 q . error = d . error ;
254254 q . correlationId = correlationId ; q . queuename = queuename ;
255255 let m : Message = Message . fromcommand ( "queuemessage" ) ;
256- if ( q . correlationId === undefined || q . correlationId === null || q . correlationId === "" ) { q . correlationId = m . id ; }
256+ if ( Util . IsNullEmpty ( q . correlationId ) ) { q . correlationId = m . id ; }
257257 m . data = JSON . stringify ( q ) ;
258258 q = await this . Send < QueueMessage > ( m ) ;
259259 if ( ( q as any ) . command == "error" ) throw new Error ( q . data ) ;
260260 return q . data ;
261261 }
262262
263-
264-
265-
266-
267-
268263 async Query < T extends Base > ( collection : string , query : any , projection : any = null , orderby : any = { _created : - 1 } , top : number = 500 , skip : number = 0 ) : Promise < any [ ] > {
269264 var q : QueryMessage < T > = new QueryMessage < T > ( ) ;
270265 q . collectionname = collection ; q . query = query ;
0 commit comments