r104111 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104110‎ | r104111 | r104112 >
Date:23:54, 23 November 2011
Author:tparscal
Status:ok
Tags:
Comment:
Got rid of optimize() - now performing consecutive-type operation merging on the fly
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/models/es.TransactionModel.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js
@@ -894,7 +894,6 @@
895895 tx.pushRetain( this.data.length - offset );
896896 }
897897
898 - tx.optimize();
899898 return tx;
900899
901900 /*
@@ -1036,7 +1035,6 @@
10371036 }
10381037 }
10391038
1040 - tx.optimize();
10411039 return tx;
10421040 };
10431041
@@ -1099,7 +1097,7 @@
11001098 if ( range.end < this.data.length ) {
11011099 tx.pushRetain( this.data.length - range.end );
11021100 }
1103 - tx.optimize();
 1101+
11041102 return tx;
11051103 };
11061104
@@ -1124,7 +1122,7 @@
11251123 if ( offset < this.data.length ) {
11261124 tx.pushRetain( this.data.length - offset );
11271125 }
1128 - tx.optimize();
 1126+
11291127 return tx;
11301128 };
11311129
Index: trunk/extensions/VisualEditor/modules/es/models/es.TransactionModel.js
@@ -33,43 +33,21 @@
3434 };
3535
3636 /**
37 - * Merges consecutive operations of the same type.
38 - *
39 - * @method
40 - */
41 -es.TransactionModel.prototype.optimize = function() {
42 - for ( var i = 0; i < this.operations.length - 1; i++ ) {
43 - var a = this.operations[i];
44 - var b = this.operations[i + 1];
45 - if ( a.type === b.type ) {
46 - switch ( a.type ) {
47 - case 'retain':
48 - a.length += b.length;
49 - this.operations.splice( i + 1, 1 );
50 - i--;
51 - break;
52 - case 'insert':
53 - case 'remove':
54 - a.data = a.data.concat( b.data );
55 - this.operations.splice( i + 1, 1 );
56 - i--;
57 - break;
58 - }
59 - }
60 - }
61 -};
62 -
63 -/**
6437 * Adds a retain operation.
6538 *
6639 * @method
6740 * @param {Integer} length Length of content data to retain
6841 */
6942 es.TransactionModel.prototype.pushRetain = function( length ) {
70 - this.operations.push( {
71 - 'type': 'retain',
72 - 'length': length
73 - } );
 43+ var end = this.operations.length - 1;
 44+ if ( this.operations.length && this.operations[end].type === 'retain' ) {
 45+ this.operations[end].length += length;
 46+ } else {
 47+ this.operations.push( {
 48+ 'type': 'retain',
 49+ 'length': length
 50+ } );
 51+ }
7452 };
7553
7654 /**
@@ -79,10 +57,15 @@
8058 * @param {Array} data Data to retain
8159 */
8260 es.TransactionModel.prototype.pushInsert = function( data ) {
83 - this.operations.push( {
84 - 'type': 'insert',
85 - 'data': data
86 - } );
 61+ var end = this.operations.length - 1;
 62+ if ( this.operations.length && this.operations[end].type === 'insert' ) {
 63+ this.operations[end].data = this.operations[end].data.concat( data );
 64+ } else {
 65+ this.operations.push( {
 66+ 'type': 'insert',
 67+ 'data': data
 68+ } );
 69+ }
8770 this.lengthDifference += data.length;
8871 };
8972
@@ -93,10 +76,15 @@
9477 * @param {Array} data Data to remove
9578 */
9679 es.TransactionModel.prototype.pushRemove = function( data ) {
97 - this.operations.push( {
98 - 'type': 'remove',
99 - 'data': data
100 - } );
 80+ var end = this.operations.length - 1;
 81+ if ( this.operations.length && this.operations[end].type === 'remove' ) {
 82+ this.operations[end].data = this.operations[end].data.concat( data );
 83+ } else {
 84+ this.operations.push( {
 85+ 'type': 'remove',
 86+ 'data': data
 87+ } );
 88+ }
10189 this.lengthDifference -= data.length;
10290 };
10391

Status & tagging log