Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformManager.js |
— | — | @@ -406,7 +406,7 @@ |
407 | 407 | AsyncTokenTransformManager.prototype._reset = function ( args, env ) { |
408 | 408 | // Note: Much of this is frame-like. |
409 | 409 | this.tailAccumulator = undefined; |
410 | | - // eventize: bend to event emitter callback |
| 410 | + // initial top-level callback, emits chunks |
411 | 411 | this.tokenCB = this._returnTokens.bind( this ); |
412 | 412 | this.prevToken = undefined; |
413 | 413 | //console.warn( 'AsyncTokenTransformManager args ' + JSON.stringify( args ) ); |
— | — | @@ -458,12 +458,11 @@ |
459 | 459 | this.tailAccumulator = res.async; |
460 | 460 | this.tokenCB = res.async.getParentCB ( 'sibling' ); |
461 | 461 | } |
462 | | - //this.phase2TailCB( tokens, true ); |
463 | 462 | |
464 | 463 | // The next processed chunk should call back as a sibling to last |
465 | 464 | // accumulator, if any. |
466 | | - if ( res.async ) { |
467 | | - } |
| 465 | + //if ( res.async ) { |
| 466 | + //} |
468 | 467 | }; |
469 | 468 | |
470 | 469 | /** |
— | — | @@ -523,7 +522,7 @@ |
524 | 523 | tokensLength = tokens.length; |
525 | 524 | i--; // continue at first inserted token |
526 | 525 | } else if ( res.token ) { |
527 | | - if ( res.token.rank === 2 ) { |
| 526 | + if ( res.token.rank === this.phaseEndRank ) { |
528 | 527 | // token is done. |
529 | 528 | if ( activeAccum ) { |
530 | 529 | // push to accumulator |
— | — | @@ -557,17 +556,18 @@ |
558 | 557 | }; |
559 | 558 | |
560 | 559 | /** |
561 | | - * Callback from tokens fully processed for phase 0 and 1, which are now ready |
562 | | - * for synchronous and globally in-order phase 2 processing. Thus each async |
563 | | - * transform is responsible for fully processing its returned tokens to the |
564 | | - * end of phase2. |
| 560 | + * Callback from tokens fully processed for phase sync01 and async12, which |
| 561 | + * are now ready for synchronous and globally in-order sync23 processing. Thus |
| 562 | + * each async transform is responsible for fully processing its returned |
| 563 | + * tokens to the end of phase2. |
565 | 564 | * |
566 | 565 | * @method |
567 | 566 | * @param {Array} chunk of tokens |
568 | 567 | * @param {Mixed} Either a falsy value if this is the last callback |
569 | 568 | * (everything is done), or a truish value if not yet done. |
570 | 569 | */ |
571 | | -AsyncTokenTransformManager.prototype._returnTokens = function ( tokens, notYetDone ) { |
| 570 | +AsyncTokenTransformManager.prototype._returnTokens = |
| 571 | + function ( tokens, notYetDone, allTokensProcessed ) { |
572 | 572 | //tokens = this._transformPhase2( this.frame, tokens, this.parentCB ); |
573 | 573 | |
574 | 574 | //if ( tokens.length && tokens[tokens.length - 1].type === 'END' ) { |
— | — | @@ -578,19 +578,42 @@ |
579 | 579 | this.env.dp( 'AsyncTokenTransformManager._returnTokens, emitting chunk: ', |
580 | 580 | tokens ); |
581 | 581 | |
582 | | - this.emit( 'chunk', tokens ); |
583 | 582 | |
584 | | - if ( ! notYetDone ) { |
585 | | - //console.warn('AsyncTokenTransformManager._returnTokens done. tokens:' + |
586 | | - // JSON.stringify( tokens, null, 2 ) + ', listeners: ' + |
587 | | - // JSON.stringify( this.listeners( 'chunk' ), null, 2 ) ); |
588 | | - // signal our done-ness to consumers. |
589 | | - //if ( this.atTopLevel ) { |
590 | | - // this.emit( 'chunk', [{type: 'END'}]); |
591 | | - //} |
592 | | - this.emit( 'end' ); |
593 | | - // and reset internal state. |
594 | | - this._reset(); |
| 583 | + if( !allTokensProcessed ) { |
| 584 | + var res = this.transformTokens( tokens, this._returnTokens.bind(this) ); |
| 585 | + this.emit( 'chunk', res.tokens ); |
| 586 | + if ( res.async ) { |
| 587 | + if ( ! this.tailAccumulator ) { |
| 588 | + this.tailAccumulator = res.async; |
| 589 | + this.tokenCB = res.async.getParentCB ( 'sibling' ); |
| 590 | + } |
| 591 | + if ( notYetDone ) { |
| 592 | + // return sibling callback |
| 593 | + return this.tokenCB; |
| 594 | + } else { |
| 595 | + // signal done-ness to last accum |
| 596 | + res.async.siblingDone(); |
| 597 | + } |
| 598 | + } else if ( !notYetDone ) { |
| 599 | + this.emit( 'end' ); |
| 600 | + // and reset internal state. |
| 601 | + this._reset(); |
| 602 | + } |
| 603 | + } else { |
| 604 | + this.emit( 'chunk', tokens ); |
| 605 | + |
| 606 | + if ( ! notYetDone ) { |
| 607 | + //console.warn('AsyncTokenTransformManager._returnTokens done. tokens:' + |
| 608 | + // JSON.stringify( tokens, null, 2 ) + ', listeners: ' + |
| 609 | + // JSON.stringify( this.listeners( 'chunk' ), null, 2 ) ); |
| 610 | + // signal our done-ness to consumers. |
| 611 | + //if ( this.atTopLevel ) { |
| 612 | + // this.emit( 'chunk', [{type: 'END'}]); |
| 613 | + //} |
| 614 | + this.emit( 'end' ); |
| 615 | + // and reset internal state. |
| 616 | + this._reset(); |
| 617 | + } |
595 | 618 | } |
596 | 619 | }; |
597 | 620 | |
— | — | @@ -927,7 +950,8 @@ |
928 | 951 | * @method |
929 | 952 | * @param {Object} token |
930 | 953 | */ |
931 | | -TokenAccumulator.prototype._returnTokens = function ( reference, tokens, notYetDone ) { |
| 954 | +TokenAccumulator.prototype._returnTokens = |
| 955 | + function ( reference, tokens, notYetDone, allTokensProcessed ) { |
932 | 956 | var res, |
933 | 957 | cb, |
934 | 958 | returnTokens = []; |
— | — | @@ -980,7 +1004,7 @@ |
981 | 1005 | */ |
982 | 1006 | TokenAccumulator.prototype.siblingDone = function () { |
983 | 1007 | //console.warn( 'TokenAccumulator.siblingDone: ' ); |
984 | | - this._returnTokens ( 'sibling', [], false ); |
| 1008 | + this._returnTokens ( 'sibling', [], false, true ); |
985 | 1009 | }; |
986 | 1010 | |
987 | 1011 | |