@@ -313,14 +313,14 @@ let api = function Binance() {
313313 */
314314 const handleSocketClose = function ( reconnect , code , reason ) {
315315 delete Binance . subscriptions [ this . endpoint ] ;
316- if ( Object . keys ( Binance . subscriptions ) . length === 0 ) {
316+ if ( Binance . subscriptions && Object . keys ( Binance . subscriptions ) . length === 0 ) {
317317 clearInterval ( Binance . socketHeartbeatInterval ) ;
318318 }
319319 Binance . options . log ( 'WebSocket closed: ' + this . endpoint +
320320 ( code ? ' (' + code + ')' : '' ) +
321321 ( reason ? ' ' + reason : '' ) ) ;
322- if ( Binance . options . reconnect && this . reconnect && reconnect ) {
323- if ( parseInt ( this . endpoint . length , 10 ) === 60 ) Binance . options . log ( 'Account data WebSocket reconnecting...' ) ;
322+ if ( Binance . options . reconnect && this . reconnect && reconnect ) {
323+ if ( this . endpoint && parseInt ( this . endpoint . length , 10 ) === 60 ) Binance . options . log ( 'Account data WebSocket reconnecting...' ) ;
324324 else Binance . options . log ( 'WebSocket reconnecting: ' + this . endpoint + '...' ) ;
325325 try {
326326 reconnect ( ) ;
@@ -336,7 +336,6 @@ let api = function Binance() {
336336 * @return {undefined }
337337 */
338338 const handleSocketError = function ( error ) {
339-
340339 /* Errors ultimately result in a `close` event.
341340 see: https://github.com/websockets/ws/blob/828194044bf247af852b31c49e2800d557fedeff/lib/websocket.js#L126 */
342341 Binance . options . log ( 'WebSocket error: ' + this . endpoint +
@@ -814,26 +813,26 @@ let api = function Binance() {
814813
815814 /**
816815 * rounds number with given step
817- * @param {float } number - quantity to round
816+ * @param {float } qty - quantity to round
818817 * @param {float } stepSize - stepSize as specified by exchangeInfo
819818 * @return {float } - number
820819 */
821- roundStep : function ( number , stepSize ) {
820+ roundStep : function ( qty , stepSize ) {
822821 const precision = stepSize . toString ( ) . split ( '.' ) [ 1 ] . length || 0 ;
823- return ( ( ( number / stepSize ) | 0 ) * stepSize ) . toFixed ( precision ) ;
822+ return ( ( ( qty / stepSize ) | 0 ) * stepSize ) . toFixed ( precision ) ;
824823 } ,
825-
824+
826825 /**
827826 * rounds price to required precision
828- * @param {float } number - price to round
827+ * @param {float } price - price to round
829828 * @param {float } tickSize - tickSize as specified by exchangeInfo
830829 * @return {float } - number
831830 */
832- roundTicks : function ( number , tickSize ) {
831+ roundTicks : function ( price , tickSize ) {
833832 const formatter = new Intl . NumberFormat ( 'en-US' , { style : 'decimal' , minimumFractionDigits : 0 , maximumFractionDigits : 8 } ) ;
834833 const precision = formatter . format ( tickSize ) . split ( '.' ) [ 1 ] . length || 0 ;
835- if ( typeof number === 'string' ) number = parseFloat ( number ) ;
836- return number . toFixed ( precision ) ;
834+ if ( typeof price === 'string' ) price = parseFloat ( price ) ;
835+ return price . toFixed ( precision ) ;
837836 } ,
838837
839838 /**
@@ -1703,7 +1702,7 @@ let api = function Binance() {
17031702 let context = Binance . depthCacheContext [ symbol ] ;
17041703 context . endpointId = endpointId ;
17051704 }
1706- }
1705+ } ;
17071706
17081707 let handleDepthStreamData = function ( depth ) {
17091708 let symbol = depth . s ;
@@ -1788,6 +1787,29 @@ let api = function Binance() {
17881787 return subscription . endpoint ;
17891788 } ,
17901789
1790+ /**
1791+ * Websocket staggered depth cache
1792+ * @param {array/string } symbols - an array of symbols to query
1793+ * @param {function } callback - callback function
1794+ * @param {int } limit - the number of entries
1795+ * @param {int } stagger - ms between each depth cache
1796+ * @return {Promise } the websocket endpoint
1797+ */
1798+ depthCacheStaggered : function ( symbols , callback , limit = 100 , stagger = 200 ) {
1799+ if ( ! Array . isArray ( symbols ) ) symbols = [ symbols ] ;
1800+ let chain = null ;
1801+
1802+ symbols . forEach ( symbol => {
1803+ let promise = ( ) => new Promise ( resolve => {
1804+ this . depthCache ( symbol , callback , limit ) ;
1805+ setTimeout ( resolve , stagger ) ;
1806+ } ) ;
1807+ chain = chain ? chain . then ( promise ) : promise ( ) ;
1808+ } ) ;
1809+
1810+ return chain ;
1811+ } ,
1812+
17911813 /**
17921814 * Websocket aggregated trades
17931815 * @param {array/string } symbols - an array or string of symbols to query
0 commit comments