|
23 | 23 | // MODULES //
|
24 | 24 |
|
25 | 25 | var logger = require( 'debug' );
|
| 26 | +var isFunction = require( '@stdlib/assert/is-function' ); |
26 | 27 | var setNonEnumerableReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
|
27 | 28 | var setNonEnumerableReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); // eslint-disable-line id-length
|
28 | 29 | var setNonEnumerable = require( '@stdlib/utils/define-nonenumerable-property' );
|
@@ -395,24 +396,55 @@ setNonEnumerableReadOnly( Presentation.prototype, 'lastFragment', function lastF
|
395 | 396 | * @name run
|
396 | 397 | * @memberof Presentation.prototype
|
397 | 398 | * @type {Function}
|
398 |
| -* @returns {Presentation} presentation instance |
| 399 | +* @param {Function} clbk - callback |
| 400 | +* @throws {TypeError} must provide a function |
| 401 | +* @returns {void} |
399 | 402 | *
|
400 | 403 | * @example
|
401 | 404 | * // TODO
|
402 | 405 | */
|
403 |
| -setNonEnumerableReadOnly( Presentation.prototype, 'run', function run() { |
| 406 | +setNonEnumerableReadOnly( Presentation.prototype, 'run', function run( clbk ) { |
| 407 | + var self; |
404 | 408 | var code;
|
| 409 | + var len; |
405 | 410 | var i;
|
406 | 411 |
|
| 412 | + if ( !isFunction( clbk ) ) { |
| 413 | + throw new TypeError( 'invalid argument. Must provide a function. Value: `' + clbk + '`.' ); |
| 414 | + } |
| 415 | + self = this; |
407 | 416 | code = this._slides[ this._slideCursor ].code;
|
408 | 417 | if ( code && code.length ) {
|
409 | 418 | // 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 ); |
413 | 446 | }
|
414 | 447 | }
|
415 |
| - return this; |
416 | 448 | });
|
417 | 449 |
|
418 | 450 | /**
|
|
0 commit comments