@@ -374,6 +374,32 @@ const MSP = {
374374 serial . send ( bufferOut ) ;
375375 } ,
376376 send_message ( code , data , callback_sent , callback_msp , doCallbackOnError ) {
377+ // Early validation
378+ if ( ! this . _validateSendMessage ( code , callback_msp ) ) {
379+ return false ;
380+ }
381+
382+ const isDuplicateRequest = this . _isDuplicateRequest ( code ) ;
383+ const bufferOut = this . _encodeMessage ( code , data ) ;
384+
385+ const requestObj = this . _createRequestObject ( code , bufferOut , callback_msp , doCallbackOnError ) ;
386+
387+ // Set up timeout only for new requests
388+ if ( ! isDuplicateRequest ) {
389+ this . _setupTimeout ( requestObj , bufferOut ) ;
390+ }
391+
392+ this . callbacks . push ( requestObj ) ;
393+
394+ // Send message if it has data or is a new request
395+ if ( this . _shouldSendMessage ( data , isDuplicateRequest ) ) {
396+ this . _sendBuffer ( bufferOut , callback_sent ) ;
397+ }
398+
399+ return true ;
400+ } ,
401+
402+ _validateSendMessage ( code , callback_msp ) {
377403 const connected = serial . connected ;
378404
379405 if ( code === undefined || ! connected || CONFIGURATOR . virtualMode ) {
@@ -383,58 +409,62 @@ const MSP = {
383409 return false ;
384410 }
385411
386- let requestExists = false ;
387- for ( const instance of this . callbacks ) {
388- if ( instance . code === code ) {
389- requestExists = true ;
412+ return true ;
413+ } ,
390414
391- break ;
392- }
393- }
415+ _isDuplicateRequest ( code ) {
416+ return this . callbacks . some ( ( instance ) => instance . code === code ) ;
417+ } ,
394418
395- const bufferOut = code <= 254 ? this . encode_message_v1 ( code , data ) : this . encode_message_v2 ( code , data ) ;
419+ _encodeMessage ( code , data ) {
420+ return code <= 254 ? this . encode_message_v1 ( code , data ) : this . encode_message_v2 ( code , data ) ;
421+ } ,
396422
397- const obj = {
398- code : code ,
423+ _createRequestObject ( code , bufferOut , callback_msp , doCallbackOnError ) {
424+ return {
425+ code,
399426 requestBuffer : bufferOut ,
400427 callback : callback_msp ,
401428 callbackOnError : doCallbackOnError ,
402429 start : performance . now ( ) ,
403430 } ;
431+ } ,
404432
405- if ( ! requestExists ) {
406- obj . timer = setTimeout ( ( ) => {
407- console . warn (
408- `MSP: data request timed-out: ${ code } ID: ${ serial . connectionId } TAB: ${ GUI . active_tab } TIMEOUT: ${
409- this . timeout
410- } QUEUE: ${ this . callbacks . length } (${ this . callbacks . map ( ( e ) => e . code ) } )`,
411- ) ;
412- serial . send ( bufferOut , ( _sendInfo ) => {
413- obj . stop = performance . now ( ) ;
414- const executionTime = Math . round ( obj . stop - obj . start ) ;
415- this . timeout = Math . max ( this . MIN_TIMEOUT , Math . min ( executionTime , this . MAX_TIMEOUT ) ) ;
416- } ) ;
417- } , this . timeout ) ;
418- }
433+ _setupTimeout ( requestObj , bufferOut ) {
434+ requestObj . timer = setTimeout ( ( ) => {
435+ this . _handleTimeout ( requestObj , bufferOut ) ;
436+ } , this . timeout ) ;
437+ } ,
419438
420- this . callbacks . push ( obj ) ;
439+ _handleTimeout ( requestObj , bufferOut ) {
440+ console . warn (
441+ `MSP: data request timed-out: ${ requestObj . code } ID: ${ serial . connectionId } ` +
442+ `TAB: ${ GUI . active_tab } TIMEOUT: ${ this . timeout } ` +
443+ `QUEUE: ${ this . callbacks . length } (${ this . callbacks . map ( ( e ) => e . code ) } )` ,
444+ ) ;
445+
446+ serial . send ( bufferOut , ( _sendInfo ) => {
447+ requestObj . stop = performance . now ( ) ;
448+ const executionTime = Math . round ( requestObj . stop - requestObj . start ) ;
449+ this . timeout = Math . max ( this . MIN_TIMEOUT , Math . min ( executionTime , this . MAX_TIMEOUT ) ) ;
450+ } ) ;
451+ } ,
421452
422- // always send messages with data payload (even when there is a message already in the queue)
423- if ( data || ! requestExists ) {
424- if ( this . timeout > this . MIN_TIMEOUT ) {
425- this . timeout -- ;
426- }
453+ _shouldSendMessage ( data , isDuplicateRequest ) {
454+ return data || ! isDuplicateRequest ;
455+ } ,
427456
428- serial . send ( bufferOut , ( sendInfo ) => {
429- if ( sendInfo . bytesSent === bufferOut . byteLength ) {
430- if ( callback_sent ) {
431- callback_sent ( ) ;
432- }
433- }
434- } ) ;
457+ _sendBuffer ( bufferOut , callback_sent ) {
458+ // Optimize timeout for frequent requests
459+ if ( this . timeout > this . MIN_TIMEOUT ) {
460+ this . timeout -- ;
435461 }
436462
437- return true ;
463+ serial . send ( bufferOut , ( sendInfo ) => {
464+ if ( sendInfo . bytesSent === bufferOut . byteLength && callback_sent ) {
465+ callback_sent ( ) ;
466+ }
467+ } ) ;
438468 } ,
439469
440470 /**
0 commit comments