Skip to content

Commit 3f15c5f

Browse files
committed
new puck lib
1 parent e9fdce3 commit 3f15c5f

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

Diff for: js/puck.js

+42-18
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ including echo and linefeed from the REPL so you may want to send
2727
alert(d);
2828
});
2929
30+
Both `eval` and `write` will return a promise if no callback
31+
function is given as an argument.
32+
33+
alert( await Puck.eval("BTN.read()") )
34+
35+
alert( await Puck.write("1+2\n") )
36+
37+
3038
Or more advanced usage with control of the connection
3139
- allows multiple connections
3240
@@ -111,8 +119,7 @@ Or more advanced usage with control of the connection
111119
if (!queue.length) return;
112120
var q = queue.shift();
113121
log(3,"Executing "+JSON.stringify(q)+" from queue");
114-
if (q.type=="eval") puck.eval(q.expr, q.cb);
115-
else if (q.type=="write") puck.write(q.data, q.callback, q.callbackNewline);
122+
if (q.type == "write") puck.write(q.data, q.callback, q.callbackNewline);
116123
else log(1,"Unknown queue item "+JSON.stringify(q));
117124
}
118125

@@ -287,10 +294,21 @@ Or more advanced usage with control of the connection
287294
function write(data, callback, callbackNewline) {
288295
if (!checkIfSupported()) return;
289296

297+
let result;
298+
/// If there wasn't a callback function, then promisify
299+
if (typeof callback !== 'function') {
300+
callbackNewline = callback;
301+
302+
result = new Promise((resolve, reject) => callback = (value, err) => {
303+
if (err) reject(err);
304+
else resolve(value);
305+
});
306+
}
307+
290308
if (isBusy) {
291309
log(3, "Busy - adding Puck.write to queue");
292310
queue.push({type:"write", data:data, callback:callback, callbackNewline:callbackNewline});
293-
return;
311+
return result;
294312
}
295313

296314
var cbTimeout;
@@ -337,7 +355,8 @@ Or more advanced usage with control of the connection
337355
if (connection && (connection.isOpen || connection.isOpening)) {
338356
if (!connection.txInProgress) connection.received = "";
339357
isBusy = true;
340-
return connection.write(data, onWritten);
358+
connection.write(data, onWritten);
359+
return result
341360
}
342361

343362
connection = connect(function(puck) {
@@ -358,6 +377,8 @@ Or more advanced usage with control of the connection
358377
isBusy = true;
359378
connection.write(data, onWritten);
360379
});
380+
381+
return result
361382
}
362383

363384
// ----------------------------------------------------------
@@ -380,21 +401,24 @@ Or more advanced usage with control of the connection
380401
write : write,
381402
/// Evaluate an expression and call cb with the result. Creates a connection if it doesn't exist
382403
eval : function(expr, cb) {
383-
if (!checkIfSupported()) return;
384-
if (isBusy) {
385-
log(3, "Busy - adding Puck.eval to queue");
386-
queue.push({type:"eval", expr:expr, cb:cb});
387-
return;
404+
405+
const response = write('\x10Bluetooth.println(JSON.stringify(' + expr + '))\n', true)
406+
.then(function (d) {
407+
try {
408+
return JSON.parse(d);
409+
} catch (e) {
410+
log(1, "Unable to decode " + JSON.stringify(d) + ", got " + e.toString());
411+
return Promise.reject(d);
412+
}
413+
});
414+
415+
416+
if (cb) {
417+
return void response.then(cb, (err) => cb(null, err));
418+
} else {
419+
return response;
388420
}
389-
write('\x10Bluetooth.println(JSON.stringify('+expr+'))\n', function(d) {
390-
try {
391-
var json = JSON.parse(d);
392-
cb(json);
393-
} catch (e) {
394-
log(1, "Unable to decode "+JSON.stringify(d)+", got "+e.toString());
395-
cb(null, "Unable to decode "+JSON.stringify(d)+", got "+e.toString());
396-
}
397-
}, true);
421+
398422
},
399423
/// Write the current time to the Puck
400424
setTime : function(cb) {

0 commit comments

Comments
 (0)