@@ -27,6 +27,14 @@ including echo and linefeed from the REPL so you may want to send
27
27
alert(d);
28
28
});
29
29
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
+
30
38
Or more advanced usage with control of the connection
31
39
- allows multiple connections
32
40
@@ -111,8 +119,7 @@ Or more advanced usage with control of the connection
111
119
if ( ! queue . length ) return ;
112
120
var q = queue . shift ( ) ;
113
121
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 ) ;
116
123
else log ( 1 , "Unknown queue item " + JSON . stringify ( q ) ) ;
117
124
}
118
125
@@ -287,10 +294,21 @@ Or more advanced usage with control of the connection
287
294
function write ( data , callback , callbackNewline ) {
288
295
if ( ! checkIfSupported ( ) ) return ;
289
296
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
+
290
308
if ( isBusy ) {
291
309
log ( 3 , "Busy - adding Puck.write to queue" ) ;
292
310
queue . push ( { type :"write" , data :data , callback :callback , callbackNewline :callbackNewline } ) ;
293
- return ;
311
+ return result ;
294
312
}
295
313
296
314
var cbTimeout ;
@@ -337,7 +355,8 @@ Or more advanced usage with control of the connection
337
355
if ( connection && ( connection . isOpen || connection . isOpening ) ) {
338
356
if ( ! connection . txInProgress ) connection . received = "" ;
339
357
isBusy = true ;
340
- return connection . write ( data , onWritten ) ;
358
+ connection . write ( data , onWritten ) ;
359
+ return result
341
360
}
342
361
343
362
connection = connect ( function ( puck ) {
@@ -358,6 +377,8 @@ Or more advanced usage with control of the connection
358
377
isBusy = true ;
359
378
connection . write ( data , onWritten ) ;
360
379
} ) ;
380
+
381
+ return result
361
382
}
362
383
363
384
// ----------------------------------------------------------
@@ -380,21 +401,24 @@ Or more advanced usage with control of the connection
380
401
write : write ,
381
402
/// Evaluate an expression and call cb with the result. Creates a connection if it doesn't exist
382
403
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 ;
388
420
}
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
+
398
422
} ,
399
423
/// Write the current time to the Puck
400
424
setTime : function ( cb ) {
0 commit comments