r113237 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113236‎ | r113237 | r113238 >
Date:15:39, 7 March 2012
Author:gwicke
Status:deferred
Tags:
Comment:
Fixes for multi-transformer expansion in AsyncTransformManager. Added argument
to callback which lets transforms indicate if their returned tokens are fully
processed for their phase. If not, the callback re-processes them so that any
remaining transforms are applied.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformManager.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformManager.js
@@ -406,7 +406,7 @@
407407 AsyncTokenTransformManager.prototype._reset = function ( args, env ) {
408408 // Note: Much of this is frame-like.
409409 this.tailAccumulator = undefined;
410 - // eventize: bend to event emitter callback
 410+ // initial top-level callback, emits chunks
411411 this.tokenCB = this._returnTokens.bind( this );
412412 this.prevToken = undefined;
413413 //console.warn( 'AsyncTokenTransformManager args ' + JSON.stringify( args ) );
@@ -458,12 +458,11 @@
459459 this.tailAccumulator = res.async;
460460 this.tokenCB = res.async.getParentCB ( 'sibling' );
461461 }
462 - //this.phase2TailCB( tokens, true );
463462
464463 // The next processed chunk should call back as a sibling to last
465464 // accumulator, if any.
466 - if ( res.async ) {
467 - }
 465+ //if ( res.async ) {
 466+ //}
468467 };
469468
470469 /**
@@ -523,7 +522,7 @@
524523 tokensLength = tokens.length;
525524 i--; // continue at first inserted token
526525 } else if ( res.token ) {
527 - if ( res.token.rank === 2 ) {
 526+ if ( res.token.rank === this.phaseEndRank ) {
528527 // token is done.
529528 if ( activeAccum ) {
530529 // push to accumulator
@@ -557,17 +556,18 @@
558557 };
559558
560559 /**
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.
565564 *
566565 * @method
567566 * @param {Array} chunk of tokens
568567 * @param {Mixed} Either a falsy value if this is the last callback
569568 * (everything is done), or a truish value if not yet done.
570569 */
571 -AsyncTokenTransformManager.prototype._returnTokens = function ( tokens, notYetDone ) {
 570+AsyncTokenTransformManager.prototype._returnTokens =
 571+ function ( tokens, notYetDone, allTokensProcessed ) {
572572 //tokens = this._transformPhase2( this.frame, tokens, this.parentCB );
573573
574574 //if ( tokens.length && tokens[tokens.length - 1].type === 'END' ) {
@@ -578,19 +578,42 @@
579579 this.env.dp( 'AsyncTokenTransformManager._returnTokens, emitting chunk: ',
580580 tokens );
581581
582 - this.emit( 'chunk', tokens );
583582
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+ }
595618 }
596619 };
597620
@@ -927,7 +950,8 @@
928951 * @method
929952 * @param {Object} token
930953 */
931 -TokenAccumulator.prototype._returnTokens = function ( reference, tokens, notYetDone ) {
 954+TokenAccumulator.prototype._returnTokens =
 955+ function ( reference, tokens, notYetDone, allTokensProcessed ) {
932956 var res,
933957 cb,
934958 returnTokens = [];
@@ -980,7 +1004,7 @@
9811005 */
9821006 TokenAccumulator.prototype.siblingDone = function () {
9831007 //console.warn( 'TokenAccumulator.siblingDone: ' );
984 - this._returnTokens ( 'sibling', [], false );
 1008+ this._returnTokens ( 'sibling', [], false, true );
9851009 };
9861010
9871011

Status & tagging log