module AliMNS { export class MQBatch extends MQ implements IMQBatch , INotifyRecvBatch { constructor ( name , account , region ? ) { super ( name , account , region ) ; } public sendP ( msg , priority ? , delaySeconds ? ) { if ( typeof msg === "string" ) { return super . sendP ( msg , priority , delaySeconds ) ; } else { var body = { Messages : { '#list' : [ ] } } ; for ( var i = 0 ; i < msg . length ; i ++ ) { var m = msg [ i ] ; var b64 = this . utf8ToBase64 ( m . getMsg ( ) ) ; var xMsg = { Message : { MessageBody : b64 } } ; xMsg . Message . Priority = m . getPriority ( ) ; xMsg . Message . DelaySeconds = m . getDelaySeconds ( ) ; body . Messages [ '#list' ] . push ( xMsg ) ; } debug ( "POST " + this . _url , body ) ; this . _openStack . accumulateNextGASend ( "MQBatch.sendP" ) ; return this . _openStack . sendP ( "POST" , this . _url , body ) ; } } public recvP ( waitSeconds ? , numOfMessages ? ) { if ( numOfMessages === undefined ) numOfMessages = 16 ; if ( numOfMessages && numOfMessages > 1 ) { var self = this ; var url = this . _url ; url += "?numOfMessages=" + numOfMessages ; if ( waitSeconds ) url += "&waitseconds=" + waitSeconds ; debug ( "GET " + url ) ; return new Promise ( function ( resolve , reject ) { var options = { timeout : 1000 * self . _recvTolerance } ; if ( waitSeconds ) options . timeout += ( 1000 * waitSeconds ) ; self . _openStack . accumulateNextGASend ( "MQBatch.recvP" ) ; self . _openStack . sendP ( "GET" , url , null , null , options ) . done ( function ( data ) { debug ( data ) ; self . decodeB64Messages ( data ) ; resolve ( data ) ; } , function ( ex ) { if ( ex . code === "ETIMEDOUT" ) { var exTimeout = new Error ( "timeout" ) ; exTimeout . innerException = ex ; exTimeout . code = ex . code ; reject ( exTimeout ) ; } else { reject ( ex ) ; } } ) ; } ) ; } else { return super . recvP ( waitSeconds ) ; } } public peekP ( numOfMessages ? ) { if ( numOfMessages === undefined ) numOfMessages = 16 ; if ( numOfMessages && numOfMessages > 1 ) { var self = this ; var url = this . _url + "?peekonly=true" ; url += "&numOfMessages=" + numOfMessages ; debug ( "GET " + url ) ; this . _openStack . accumulateNextGASend ( "MQBatch.peekP" ) ; return this . _openStack . sendP ( "GET" , url ) . then ( function ( data ) { debug ( data ) ; self . decodeB64Messages ( data ) ; return data ; } ) ; } else { return super . peekP ( ) ; } } public deleteP ( receiptHandle ) { if ( typeof receiptHandle === "string" ) { super . deleteP ( receiptHandle ) ; } else { debug ( "DELETE " + this . _url , receiptHandle ) ; var body = { ReceiptHandles : { '#list' : [ ] } } ; for ( var i = 0 ; i < receiptHandle . length ; i ++ ) { var r = { ReceiptHandle : receiptHandle [ i ] } ; body . ReceiptHandles [ '#list' ] . push ( r ) ; } this . _openStack . accumulateNextGASend ( "MQBatch.deleteP" ) ; return this . _openStack . sendP ( "DELETE" , this . _url , body ) ; } } public notifyRecv ( cb , waitSeconds ? , numOfMessages ? ) { if ( this . _notifyRecv === null ) this . _notifyRecv = new NotifyRecv ( this ) ; return this . _notifyRecv . notifyRecv ( cb , waitSeconds || 5 , numOfMessages || 16 ) ; } protected decodeB64Messages ( data ) { if ( data && data . Messages && data . Messages . Message ) { if ( ! Util . isArray ( data . Messages . Message ) ) { var msg = data . Messages . Message ; data . Messages . Message = [ msg ] ; } for ( var i = 0 ; i < data . Messages . Message . length ; i ++ ) { var msg = data . Messages . Message [ i ] ; if ( msg . MessageBody ) msg . MessageBody = this . base64ToUtf8 ( msg . MessageBody ) ; } } else { super . decodeB64Messages ( data ) ; } } protected _notifyRecv : INotifyRecvBatch = null ; } }