8
0
Просмотр исходного кода

Reset ChangeBuffer size also when new batch appears. Improved ChangeBuffer tests.

Piotr Jasiun 9 лет назад
Родитель
Сommit
78b9064b4f

+ 12 - 3
packages/ckeditor5-typing/src/changebuffer.js

@@ -107,8 +107,7 @@ export default class ChangeBuffer {
 		this.size += changeCount;
 		this.size += changeCount;
 
 
 		if ( this.size >= this.limit ) {
 		if ( this.size >= this.limit ) {
-			this._batch = null;
-			this.size = 0;
+			this._reset();
 		}
 		}
 	}
 	}
 
 
@@ -133,7 +132,17 @@ export default class ChangeBuffer {
 	_onBatch( batch ) {
 	_onBatch( batch ) {
 		// 1 operation means a newly created batch.
 		// 1 operation means a newly created batch.
 		if ( batch !== this._batch && count( batch.getOperations() ) <= 1 ) {
 		if ( batch !== this._batch && count( batch.getOperations() ) <= 1 ) {
-			this._batch = null;
+			this._reset();
 		}
 		}
 	}
 	}
+
+	/**
+	 * Resets change buffer.
+	 *
+	 * @private
+	 */
+	_reset() {
+		this._batch = null;
+		this.size = 0;
+	}
 }
 }

+ 6 - 0
packages/ckeditor5-typing/tests/changebuffer.js

@@ -49,6 +49,7 @@ describe( 'ChangeBuffer', () => {
 
 
 			expect( batch2 ).to.be.instanceof( Batch );
 			expect( batch2 ).to.be.instanceof( Batch );
 			expect( batch2 ).to.not.equal( batch1 );
 			expect( batch2 ).to.not.equal( batch1 );
+			expect( buffer.size ).to.equal( 0 );
 		} );
 		} );
 
 
 		it( 'is reset once changes exceedes the limit', () => {
 		it( 'is reset once changes exceedes the limit', () => {
@@ -58,14 +59,19 @@ describe( 'ChangeBuffer', () => {
 			buffer.input( CHANGE_LIMIT + 1 );
 			buffer.input( CHANGE_LIMIT + 1 );
 
 
 			expect( buffer.batch ).to.not.equal( batch1 );
 			expect( buffer.batch ).to.not.equal( batch1 );
+			expect( buffer.size ).to.equal( 0 );
 		} );
 		} );
 
 
 		it( 'is reset once a new batch appears in the document', () => {
 		it( 'is reset once a new batch appears in the document', () => {
 			const batch1 = buffer.batch;
 			const batch1 = buffer.batch;
 
 
+			// Ensure that size is reset too.
+			buffer.input( 1 );
+
 			doc.batch().insert( Position.createAt( root, 0 ), 'a' );
 			doc.batch().insert( Position.createAt( root, 0 ), 'a' );
 
 
 			expect( buffer.batch ).to.not.equal( batch1 );
 			expect( buffer.batch ).to.not.equal( batch1 );
+			expect( buffer.size ).to.equal( 0 );
 		} );
 		} );
 
 
 		it( 'is not reset when changes are added to the buffer\'s batch', () => {
 		it( 'is not reset when changes are added to the buffer\'s batch', () => {