Skip to content

Commit 6bda957

Browse files
committed
Tweaks from @rblakesley to allow timeout to be specified, and to fix rejection in case of espruinoEval timeout
1 parent 9810a95 commit 6bda957

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

uart.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -435,11 +435,11 @@ To do:
435435
this.rxDataHandlerLastCh = ch;
436436
}
437437
}
438+
this.hadData = true;
438439
if (data.length>0) {
439440
// keep track of received data
440441
if (this.received.length < 100000) // ensure we're not creating a memory leak
441442
this.received += data;
442-
this.hadData = true;
443443
// forward any data
444444
if (this.cb) this.cb(data);
445445
this.emit('data', data);
@@ -532,39 +532,41 @@ To do:
532532
return connection.espruinoSendPacket("DATA", packet, packetOptions).then(sendData);
533533
}
534534
}
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 || {};
537541
if ("string"!=typeof expr) throw new Error("'expr' must be a String");
538542
let connection = this;
539543
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+
}
549551
}
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+
});
568570
});
569571
}
570572
};
@@ -779,12 +781,14 @@ To do:
779781
writer.releaseLock();
780782
writer = undefined;
781783
log(3, "Sent");
782-
callback();
784+
if (callback) callback();
783785
}).catch(function(error) {
786+
if (writer) {
784787
writer.releaseLock();
788+
writer.close();
789+
}
785790
writer = undefined;
786791
log(0,'SEND ERROR: ' + error);
787-
closeSerial();
788792
});
789793
};
790794

0 commit comments

Comments
 (0)