@@ -435,11 +435,11 @@ To do:
435
435
this . rxDataHandlerLastCh = ch ;
436
436
}
437
437
}
438
+ this . hadData = true ;
438
439
if ( data . length > 0 ) {
439
440
// keep track of received data
440
441
if ( this . received . length < 100000 ) // ensure we're not creating a memory leak
441
442
this . received += data ;
442
- this . hadData = true ;
443
443
// forward any data
444
444
if ( this . cb ) this . cb ( data ) ;
445
445
this . emit ( 'data' , data ) ;
@@ -532,39 +532,41 @@ To do:
532
532
return connection . espruinoSendPacket ( "DATA" , packet , packetOptions ) . then ( sendData ) ;
533
533
}
534
534
}
535
- /* Send a JS expression to be evaluated on Espruino using using 2v25 packets. */
536
- espruinoEval ( expr ) {
535
+ /* Send a JS expression to be evaluated on Espruino using using 2v25 packets.
536
+ options = {
537
+ timeout : int // milliseconds timeout
538
+ }*/
539
+ espruinoEval ( expr , options ) {
540
+ options = options || { } ;
537
541
if ( "string" != typeof expr ) throw new Error ( "'expr' must be a String" ) ;
538
542
let connection = this ;
539
543
let resolve , reject , timeout ;
540
- let promise = new Promise ( ( _resolve , _reject ) => {
541
- resolve = _resolve ;
542
- reject = _reject ;
543
- } ) ;
544
- function cleanup ( ) {
545
- connection . removeListener ( "packet" , onPacket ) ;
546
- if ( timeout ) {
547
- clearTimeout ( timeout ) ;
548
- timeout = undefined ;
544
+ return new Promise ( ( resolve , reject ) => {
545
+ function cleanup ( ) {
546
+ connection . removeListener ( "packet" , onPacket ) ;
547
+ if ( timeout ) {
548
+ clearTimeout ( timeout ) ;
549
+ timeout = undefined ;
550
+ }
549
551
}
550
- }
551
- function onPacket ( type , data ) {
552
- if ( type != 0 ) return ; // ignore things that are not a response
553
- resolve ( parseRJSON ( data ) ) ;
554
- cleanup ( ) ;
555
- }
556
- connection . parsePackets = true ;
557
- connection . on ( "packet" , onPacket ) ;
558
- timeout = setTimeout ( ( ) => {
559
- timeout = undefined ;
560
- cleanup ( ) ;
561
- reject ( "espruinoEval Timeout" ) ;
562
- } , 1000 ) ;
563
- return connection . espruinoSendPacket ( "EVAL" , expr ) . then ( ( ) => {
564
- return promise ;
565
- } , err => {
566
- cleanup ( ) ;
567
- return Promise . reject ( err ) ;
552
+ function onPacket ( type , data ) {
553
+ if ( type != 0 ) return ; // ignore things that are not a response
554
+ cleanup ( ) ;
555
+ resolve ( parseRJSON ( data ) ) ;
556
+ }
557
+ connection . parsePackets = true ;
558
+ connection . on ( "packet" , onPacket ) ;
559
+ timeout = setTimeout ( ( ) => {
560
+ timeout = undefined ;
561
+ cleanup ( ) ;
562
+ reject ( "espruinoEval Timeout" ) ;
563
+ } , options . timeout || 1000 ) ;
564
+ connection . espruinoSendPacket ( "EVAL" , expr ) . then ( ( ) => {
565
+ // resolved/rejected with 'packet' event or timeout
566
+ } , err => {
567
+ cleanup ( ) ;
568
+ reject ( err ) ;
569
+ } ) ;
568
570
} ) ;
569
571
}
570
572
} ;
@@ -779,12 +781,14 @@ To do:
779
781
writer . releaseLock ( ) ;
780
782
writer = undefined ;
781
783
log ( 3 , "Sent" ) ;
782
- callback ( ) ;
784
+ if ( callback ) callback ( ) ;
783
785
} ) . catch ( function ( error ) {
786
+ if ( writer ) {
784
787
writer . releaseLock ( ) ;
788
+ writer . close ( ) ;
789
+ }
785
790
writer = undefined ;
786
791
log ( 0 , 'SEND ERROR: ' + error ) ;
787
- closeSerial ( ) ;
788
792
} ) ;
789
793
} ;
790
794
0 commit comments