Procházet zdrojové kódy

Removed async/await syntax from the EditorWatchdog.

Maciej Bukowski před 5 roky
rodič
revize
7dff251c94

+ 79 - 64
packages/ckeditor5-watchdog/src/editorwatchdog.js

@@ -125,26 +125,30 @@ export default class EditorWatchdog extends Watchdog {
 	 * @fires restart
 	 * @returns {Promise}
 	 */
-	async _restart() {
-		this.state = 'initializing';
-
-		try {
-			await this._destroy();
-		} catch ( err ) {
-			console.error( 'An error happened during the editor destructing.', err );
-		}
-
-		if ( typeof this._elementOrData === 'string' ) {
-			await this.create( this._data, this._config );
-		} else {
-			const updatedConfig = Object.assign( {}, this._config, {
-				initialData: this._data
+	_restart() {
+		return Promise.resolve()
+			.then( () => {
+				this.state = 'initializing';
+
+				return this._destroy();
+			} )
+			.catch( err => {
+				console.error( 'An error happened during the editor destructing.', err );
+			} )
+			.then( () => {
+				if ( typeof this._elementOrData === 'string' ) {
+					return this.create( this._data, this._config );
+				} else {
+					const updatedConfig = Object.assign( {}, this._config, {
+						initialData: this._data
+					} );
+
+					return this.create( this._elementOrData, updatedConfig );
+				}
+			} )
+			.then( () => {
+				this.fire( 'restart' );
 			} );
-
-			await this.create( this._elementOrData, updatedConfig );
-		}
-
-		this.fire( 'restart' );
 	}
 
 	/**
@@ -157,41 +161,45 @@ export default class EditorWatchdog extends Watchdog {
 	 *
 	 * @returns {Promise}
 	 */
-	async create( elementOrData = this._elementOrData, config = this._config, context ) {
-		if ( !this._creator ) {
-			/**
-			 * The watchdog's editor creator is not defined. Define it by using
-			 * {@link module:watchdog/watchdog~Watchdog#setCreator `Watchdog#setCreator()`} or
-			 * the {@link module:watchdog/watchdog~Watchdog.for `Watchdog.for()`} helper.
-			 *
-			 * @error watchdog-creator-not-defined
-			 */
-			throw new CKEditorError(
-				'watchdog-creator-not-defined: The watchdog\'s editor creator is not defined.',
-				null
-			);
-		}
-
-		super._startErrorHandling();
-
-		this._elementOrData = elementOrData;
-
-		// Clone configuration because it might be shared within multiple watchdog instances. Otherwise,
-		// when an error occurs in one of these editors, the watchdog will restart all of them.
-		this._config = this._cloneEditorConfiguration( config ) || {};
-
-		this._config.context = context;
-
-		const editor = await this._creator( elementOrData, this._config );
-
-		this._editor = editor;
-
-		this.listenTo( editor.model.document, 'change:data', this._throttledSave );
-
-		this._lastDocumentVersion = editor.model.document.version;
-		this._data = this._getData();
-
-		this.state = 'ready';
+	create( elementOrData = this._elementOrData, config = this._config, context ) {
+		return Promise.resolve()
+			.then( () => {
+				if ( !this._creator ) {
+					/**
+					 * The watchdog's editor creator is not defined. Define it by using
+					 * {@link module:watchdog/watchdog~Watchdog#setCreator `Watchdog#setCreator()`} or
+					 * the {@link module:watchdog/watchdog~Watchdog.for `Watchdog.for()`} helper.
+					 *
+					 * @error watchdog-creator-not-defined
+					 */
+					throw new CKEditorError(
+						'watchdog-creator-not-defined: The watchdog\'s editor creator is not defined.',
+						null
+					);
+				}
+
+				super._startErrorHandling();
+
+				this._elementOrData = elementOrData;
+
+				// Clone configuration because it might be shared within multiple watchdog instances. Otherwise,
+				// when an error occurs in one of these editors, the watchdog will restart all of them.
+				this._config = this._cloneEditorConfiguration( config ) || {};
+
+				this._config.context = context;
+
+				return this._creator( elementOrData, this._config );
+			} )
+			.then( editor => {
+				this._editor = editor;
+
+				this.listenTo( editor.model.document, 'change:data', this._throttledSave );
+
+				this._lastDocumentVersion = editor.model.document.version;
+				this._data = this._getData();
+
+				this.state = 'ready';
+			} );
 	}
 
 	/**
@@ -200,26 +208,33 @@ export default class EditorWatchdog extends Watchdog {
 	 *
 	 * @returns {Promise}
 	 */
-	async destroy() {
-		this.state = 'destroyed';
+	destroy() {
+		return Promise.resolve()
+			.then( () => {
+				this.state = 'destroyed';
 
-		return this._destroy();
+				return this._destroy();
+			} );
 	}
 
 	/**
 	 * @private
+	 * @returns {Promise}
 	 */
-	async _destroy() {
-		this._stopErrorHandling();
+	_destroy() {
+		return Promise.resolve()
+			.then( () => {
+				this._stopErrorHandling();
 
-		// Save data if there is a remaining editor data change.
-		this._throttledSave.flush();
+				// Save data if there is a remaining editor data change.
+				this._throttledSave.flush();
 
-		const editor = this._editor;
+				const editor = this._editor;
 
-		this._editor = null;
+				this._editor = null;
 
-		await this._destructor( editor );
+				return this._destructor( editor );
+			} );
 	}
 
 	/**

+ 40 - 37
packages/ckeditor5-watchdog/tests/editorwatchdog.js

@@ -448,7 +448,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'Watchdog should not crash permanently when average time between errors' +
-		' is longer than `minimumNonErrorTimePeriod`', async () => {
+			' is longer than `minimumNonErrorTimePeriod`', async () => {
 			const watchdog = new EditorWatchdog( { crashNumberLimit: 2, minimumNonErrorTimePeriod: 0 } );
 
 			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
@@ -633,7 +633,7 @@ describe( 'EditorWatchdog', () => {
 			} );
 		} );
 
-		it( 'editor should be restarted with the latest available data before the crash', () => {
+		it( 'editor should be restarted with the latest available data before the crash', async () => {
 			const watchdog = new EditorWatchdog();
 
 			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
@@ -644,51 +644,54 @@ describe( 'EditorWatchdog', () => {
 
 			sinon.stub( console, 'error' );
 
-			return watchdog.create( element, {
+			await watchdog.create( element, {
 				initialData: '<p>foo</p>',
 				plugins: [ Paragraph ]
-			} ).then( () => {
-				const editorGetDataError = new Error( 'Some error' );
-				const getDataStub = sinon.stub( watchdog.editor.data, 'get' )
-					.throwsException( editorGetDataError );
-				// Keep the reference to cleanly destroy it at in the end, as during the TC it
-				// throws an exception during destruction.
-				const firstEditor = watchdog.editor;
+			} );
+
+			const editorGetDataError = new Error( 'Some error' );
+			const getDataStub = sinon.stub( watchdog.editor.data, 'get' )
+				.throwsException( editorGetDataError );
+			// Keep the reference to cleanly destroy it at in the end, as during the TC it
+			// throws an exception during destruction.
+			const firstEditor = watchdog.editor;
+
+			await new Promise( res => {
+				const doc = watchdog.editor.model.document;
+
+				watchdog.editor.model.change( writer => {
+					writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
+				} );
 
 				setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
 
-				return new Promise( res => {
-					const doc = watchdog.editor.model.document;
+				watchdog.on( 'restart', async () => {
+					window.onerror = originalErrorHandler;
 
-					watchdog.editor.model.change( writer => {
-						writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
-					} );
+					// It is called second time by during the default editor destruction
+					// to update the source element.
+					sinon.assert.calledTwice( getDataStub );
 
-					watchdog.on( 'restart', () => {
-						window.onerror = originalErrorHandler;
+					expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
 
-						// It is called second time by during the default editor destruction
-						// to update the source element.
-						sinon.assert.calledTwice( getDataStub );
+					sinon.assert.calledWith(
+						console.error,
+						editorGetDataError,
+						'An error happened during restoring editor data. Editor will be restored from the previously saved data.'
+					);
 
-						expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
+					sinon.assert.calledWith(
+						console.error,
+						'An error happened during the editor destructing.'
+					);
 
-						sinon.assert.calledWith(
-							console.error,
-							editorGetDataError,
-							'An error happened during restoring editor data. Editor will be restored from the previously saved data.'
-						);
-
-						sinon.assert.calledWith(
-							console.error,
-							'An error happened during the editor destructing.'
-						);
-
-						watchdog.destroy().then( () => {
-							getDataStub.restore();
-							return firstEditor.destroy();
-						} ).then( res );
-					} );
+					await watchdog.destroy();
+
+					getDataStub.restore();
+
+					await firstEditor.destroy();
+
+					res();
 				} );
 			} );
 		} );