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

Removed invalid promise catches.

Maciej Bukowski 8 лет назад
Родитель
Сommit
61755679bc

+ 24 - 15
packages/ckeditor5-engine/src/dev-utils/deltareplayer.js

@@ -7,7 +7,7 @@
  * @module engine/dev-utils/deltareplayer
  */
 
-/* global setTimeout, console */
+/* global setTimeout */
 
 import DeltaFactory from '../model/delta/deltafactory';
 
@@ -61,17 +61,19 @@ export default class DeltaReplayer {
 	play( timeInterval = 1000 ) {
 		const deltaReplayer = this;
 
-		return new Promise( ( res ) => {
+		return new Promise( ( res, rej ) => {
 			play();
 
 			function play() {
-				if ( deltaReplayer._deltasToReplay.length === 0 ) {
-					return res();
-				}
+				deltaReplayer.applyNextDelta().then( ( isFinished ) => {
+					if ( isFinished ) {
+						return res();
+					}
 
-				deltaReplayer.applyNextDelta().then( () => {
 					setTimeout( play, timeInterval );
-				}, res );
+				} ).catch( ( err ) => {
+					rej( err );
+				} );
 			}
 		} );
 	}
@@ -88,8 +90,11 @@ export default class DeltaReplayer {
 		}
 
 		return this.applyNextDelta()
-			.then( () => this.applyDeltas( numberOfDeltas - 1 ) )
-			.catch( err => console.warn( err ) );
+			.then( ( isFinished ) => {
+				if ( !isFinished ) {
+					return this.applyDeltas( numberOfDeltas - 1 );
+				}
+			} );
 	}
 
 	/**
@@ -99,24 +104,28 @@ export default class DeltaReplayer {
 	 */
 	applyAllDeltas() {
 		return this.applyNextDelta()
-			.then( () => this.applyAllDeltas() )
-			.catch( () => {} );
+			.then( ( isFinished ) => {
+				if ( !isFinished ) {
+					return this.applyAllDeltas();
+				}
+			} );
 	}
 
 	/**
 	 * Applies the next delta to replay.
+	 * Returns promise with a `isFinished` parameter.
 	 *
-	 * @returns {Promise}
+	 * @returns {Promise.<Boolean>}
 	 */
 	applyNextDelta() {
 		const document = this._document;
 
-		return new Promise( ( res, rej ) => {
+		return new Promise( ( res ) => {
 			document.enqueueChanges( () => {
 				const jsonDelta = this._deltasToReplay.shift();
 
 				if ( !jsonDelta ) {
-					return rej( new Error( 'No deltas to replay' ) );
+					return res( true );
 				}
 
 				const delta = DeltaFactory.fromJSON( jsonDelta, this._document );
@@ -128,7 +137,7 @@ export default class DeltaReplayer {
 					document.applyOperation( operation );
 				}
 
-				res();
+				res( false );
 			} );
 		} );
 	}

+ 25 - 23
packages/ckeditor5-engine/tests/dev-utils/deltareplayer.js

@@ -3,25 +3,10 @@
  * For licensing, see LICENSE.md.
  */
 
-/* global console */
-
 import DeltaReplayer from '../../src/dev-utils/deltareplayer';
 import Document from '../../src/model/document';
 
 describe( 'DeltaReplayer', () => {
-	const sandbox = sinon.sandbox.create();
-	let stubs;
-
-	beforeEach( () => {
-		stubs = {
-			consoleWarn: sandbox.stub( console, 'warn' ),
-		};
-	} );
-
-	afterEach( () => {
-		sandbox.restore();
-	} );
-
 	describe( 'constructor()', () => {
 		it( 'should be able to initialize replayer without deltas', () => {
 			const doc = getDocument();
@@ -48,8 +33,9 @@ describe( 'DeltaReplayer', () => {
 
 			const deltaReplayer = new DeltaReplayer( doc, '---', JSON.stringify( delta ) );
 
-			return deltaReplayer.applyNextDelta().then( () => {
+			return deltaReplayer.applyNextDelta().then( ( isFinished ) => {
 				expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [] );
+				expect( isFinished ).to.equal( false );
 			} );
 		} );
 
@@ -64,15 +50,12 @@ describe( 'DeltaReplayer', () => {
 			} );
 		} );
 
-		it( 'should throw an error if 0 deltas are provided', () => {
+		it( 'should resolve with true if 0 deltas are provided', () => {
 			const doc = getDocument();
 			const deltaReplayer = new DeltaReplayer( doc, '---', '' );
 
-			return deltaReplayer.applyNextDelta().then( () => {
-				throw new Error( 'This should throw an error' );
-			}, ( err ) => {
-				expect( err instanceof Error ).to.equal( true );
-				expect( err.message ).to.equal( 'No deltas to replay' );
+			return deltaReplayer.applyNextDelta().then( ( isFinished ) => {
+				expect( isFinished ).to.equal( true );
 			} );
 		} );
 	} );
@@ -122,7 +105,6 @@ describe( 'DeltaReplayer', () => {
 			return deltaReplayer.applyDeltas( 3 ).then( () => {
 				expect( Array.from( doc.getRoot().getChildren() ).length ).to.equal( 2 );
 				expect( deltaReplayer.getDeltasToReplay().length ).to.equal( 0 );
-				sinon.assert.calledWithExactly( stubs.consoleWarn, new Error( 'No deltas to replay' ) );
 			} );
 		} );
 	} );
@@ -149,6 +131,26 @@ describe( 'DeltaReplayer', () => {
 
 			return deltaReplayer.play();
 		} );
+
+		it( 'should correctly handle errors coming from the engine', () => {
+			const doc = getDocument();
+
+			const invalidDelta = getSecondDelta();
+			invalidDelta.operations[ 0 ].baseVersion = 3;
+
+			const stringifiedDeltas = [ getFirstDelta(), invalidDelta ]
+				.map( d => JSON.stringify( d ) )
+				.join( '---' );
+
+			const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
+
+			return deltaReplayer.play( 1 )
+				.then( () => {
+					throw new Error( 'It should throw an error' );
+				}, ( err ) => {
+					expect( err.name ).to.equal( 'CKEditorError' );
+				} );
+		} );
 	} );
 } );