@@ -182,26 +182,27 @@ export default class Socket extends EventEmitter {
182
182
* @param {() => void } [callback]
183
183
*/
184
184
setTimeout ( timeout , callback ) {
185
- if ( timeout === 0 ) {
185
+ this . _timeoutMsecs = timeout ;
186
+ if ( this . _timeoutMsecs === 0 ) {
186
187
this . _clearTimeout ( ) ;
187
188
} else {
188
- this . _activateTimer ( timeout ) ;
189
+ this . _resetTimeout ( ) ;
189
190
}
190
191
if ( callback ) this . once ( 'timeout' , callback ) ;
191
192
return this ;
192
193
}
193
194
194
195
/**
195
196
* @private
196
- * @param {number } [timeout]
197
197
*/
198
- _activateTimer ( timeout ) {
199
- if ( timeout !== undefined ) this . _timeoutMsecs = timeout ;
200
- this . _clearTimeout ( ) ;
201
- this . _timeout = setTimeout ( ( ) => {
198
+ _resetTimeout ( ) {
199
+ if ( this . _timeoutMsecs !== 0 ) {
202
200
this . _clearTimeout ( ) ;
203
- this . emit ( 'timeout' ) ;
204
- } , this . _timeoutMsecs ) ;
201
+ this . _timeout = setTimeout ( ( ) => {
202
+ this . _clearTimeout ( ) ;
203
+ this . emit ( 'timeout' ) ;
204
+ } , this . _timeoutMsecs ) ;
205
+ }
205
206
}
206
207
207
208
/**
@@ -327,7 +328,6 @@ export default class Socket extends EventEmitter {
327
328
* @return {boolean }
328
329
*/
329
330
write ( buffer , encoding , cb ) {
330
- const self = this ;
331
331
if ( this . _pending || this . _destroyed ) throw new Error ( 'Socket is closed.' ) ;
332
332
333
333
const generatedBuffer = this . _generateSendBuffer ( buffer , encoding ) ;
@@ -340,7 +340,7 @@ export default class Socket extends EventEmitter {
340
340
this . _msgEvtEmitter . removeListener ( 'written' , msgEvtHandler ) ;
341
341
this . _writeBufferSize -= generatedBuffer . byteLength ;
342
342
this . _lastRcvMsgId = msgId ;
343
- if ( self . _timeout ) self . _activateTimer ( ) ;
343
+ this . _resetTimeout ( ) ;
344
344
if ( this . writableNeedDrain && this . _lastSentMsgId === msgId ) {
345
345
this . writableNeedDrain = false ;
346
346
this . emit ( 'drain' ) ;
@@ -434,6 +434,7 @@ export default class Socket extends EventEmitter {
434
434
*/
435
435
_onDeviceDataEvt = ( /** @type {{ id: number; data: string; } } */ evt ) => {
436
436
if ( evt . id !== this . _id ) return ;
437
+ this . _resetTimeout ( ) ;
437
438
if ( ! this . _paused ) {
438
439
const bufferData = Buffer . from ( evt . data , 'base64' ) ;
439
440
this . _bytesRead += bufferData . byteLength ;
0 commit comments