Skip to content

Commit aabdec3

Browse files
committed
Fix async code handling
1 parent 31353c8 commit aabdec3

File tree

1 file changed

+38
-6
lines changed
  • lib/node_modules/@stdlib/repl/presentation/lib

1 file changed

+38
-6
lines changed

lib/node_modules/@stdlib/repl/presentation/lib/main.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// MODULES //
2424

2525
var logger = require( 'debug' );
26+
var isFunction = require( '@stdlib/assert/is-function' );
2627
var setNonEnumerableReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
2728
var setNonEnumerableReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); // eslint-disable-line id-length
2829
var setNonEnumerable = require( '@stdlib/utils/define-nonenumerable-property' );
@@ -395,24 +396,55 @@ setNonEnumerableReadOnly( Presentation.prototype, 'lastFragment', function lastF
395396
* @name run
396397
* @memberof Presentation.prototype
397398
* @type {Function}
398-
* @returns {Presentation} presentation instance
399+
* @param {Function} clbk - callback
400+
* @throws {TypeError} must provide a function
401+
* @returns {void}
399402
*
400403
* @example
401404
* // TODO
402405
*/
403-
setNonEnumerableReadOnly( Presentation.prototype, 'run', function run() {
406+
setNonEnumerableReadOnly( Presentation.prototype, 'run', function run( clbk ) {
407+
var self;
404408
var code;
409+
var len;
405410
var i;
406411

412+
if ( !isFunction( clbk ) ) {
413+
throw new TypeError( 'invalid argument. Must provide a function. Value: `' + clbk + '`.' );
414+
}
415+
self = this;
407416
code = this._slides[ this._slideCursor ].code;
408417
if ( code && code.length ) {
409418
// Forward each line of code to the REPL readline interface in order to mimic user input...
410-
for ( i = 0; i < code.length; i++ ) {
411-
this._repl._rli.write( code[ i ]+'\n' );
412-
this._repl._displayPrompt( false );
419+
len = code.length;
420+
i = -1;
421+
return next();
422+
}
423+
// TODO: replace with polyfill
424+
process.nextTick( clbk );
425+
426+
/**
427+
* Callback invoked the REPL command queue drains.
428+
*
429+
* @private
430+
* @param {string} cmd - command
431+
* @param {boolean} success - boolean indicating whether the command successfully executed
432+
*/
433+
function next() {
434+
i += 1;
435+
if ( i < len ) {
436+
if ( code[ i ] ) {
437+
self._repl._rli.write( code[ i ]+'\n' );
438+
self._repl.once( 'drain', next );
439+
} else {
440+
// TODO: replace with polyfill
441+
process.nextTick( next );
442+
}
443+
} else {
444+
// TODO: replace with polyfill
445+
process.nextTick( clbk );
413446
}
414447
}
415-
return this;
416448
});
417449

418450
/**

0 commit comments

Comments
 (0)