Explorar o código

Removed the `EditorWatchdog.for()` method.

Maciej Bukowski %!s(int64=6) %!d(string=hai) anos
pai
achega
2a9d172e9d

+ 4 - 4
packages/ckeditor5-watchdog/docs/features/watchdog.md

@@ -43,7 +43,7 @@ import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
 import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
 
 // Create a watchdog for the given editor type.
-const watchdog = EditorWatchdog.for( ClassicEditor );
+const watchdog = new EditorWatchdog( ClassicEditor );
 
 // Create a new editor instance.
 watchdog.create( document.querySelector( '#editor' ), {
@@ -263,21 +263,21 @@ watchdog.on( 'restart', () => {} );
 
 ## Configuration
 
-Both, the {@link module:watchdog/watchdog~Watchdog#constructor `Watchdog#constructor`} and the {@link module:watchdog/watchdog~Watchdog.for `Watchdog.for`} methods accept a {{@link module:watchdog/watchdog~WatchdogConfig configuration object} with the following optional properties:
+Both, {@link module:watchdog/editorwatchdog~EditorWatchdog#constructor `EditorWatchdog`} and {@link module:watchdog/contextwatchdog~ContextWatchdog#constructor `ContextWatchdog`} constructors accept a {{@link module:watchdog/watchdog~WatchdogConfig configuration object} as the second argument with the following optional properties:
 
 * `crashNumberLimit` - A threshold specifying the number of editor errors (defaults to `3`). After this limit is reached and the time between last errors is shorter than `minimumNonErrorTimePeriod` the watchdog changes its state to `crashedPermanently` and it stops restarting the editor. This prevents an infinite restart loop.
 * `minimumNonErrorTimePeriod` - An average amount of milliseconds between last editor errors (defaults to 5000). When the period of time between errors is lower than that and the `crashNumberLimit` is also reached the watchdog changes its state to `crashedPermanently` and it stops restarting the editor. This prevents an infinite restart loop.
 * `saveInterval` - A minimum number of milliseconds between saving editor data internally (defaults to 5000). Note that for large documents this might have an impact on the editor performance.
 
 ```js
-const watchdog = new Watchdog( {
+const editorWatchdog = new EditorWatchdog( ClassicEditor, {
 	minimumNonErrorTimePeriod: 2000,
 	crashNumberLimit: 4,
 	saveInterval: 1000
 } )
 ```
 
-Note that the ContextWatchdog spreads its configuration to the added items.
+Note that the context watchdog passes this configuration to the added editors.
 
 ## Limitations
 

+ 10 - 44
packages/ckeditor5-watchdog/src/editorwatchdog.js

@@ -12,7 +12,6 @@
 import { throttle, cloneDeepWith, isElement } from 'lodash-es';
 import areConnectedThroughProperties from './utils/areconnectedthroughproperties';
 import Watchdog from './watchdog';
-import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 /**
  * A watchdog for CKEditor 5 editors.
@@ -22,10 +21,11 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  */
 export default class EditorWatchdog extends Watchdog {
 	/**
-	 * @param {module:watchdog/watchdog~WatchdogConfig} [config] The watchdog plugin configuration.
+	 * @param {*} Editor The editor class.
+	 * @param {module:watchdog/watchdog~WatchdogConfig} [watchdogConfig] The watchdog plugin configuration.
 	 */
-	constructor( config = {} ) {
-		super( config );
+	constructor( Editor, watchdogConfig = {} ) {
+		super( watchdogConfig );
 
 		/**
 		 * The current editor instance.
@@ -44,7 +44,7 @@ export default class EditorWatchdog extends Watchdog {
 		 */
 		this._throttledSave = throttle(
 			this._save.bind( this ),
-			typeof config.saveInterval === 'number' ? config.saveInterval : 5000
+			typeof watchdogConfig.saveInterval === 'number' ? watchdogConfig.saveInterval : 5000
 		);
 
 		/**
@@ -75,6 +75,8 @@ export default class EditorWatchdog extends Watchdog {
 		 * @member {Object|undefined} #_config
 		 */
 
+		// Set default creator and destructor functions:
+		this._creator = ( ( elementOrData, config ) => Editor.create( elementOrData, config ) );
 		this._destructor = editor => editor.destroy();
 	}
 
@@ -159,11 +161,10 @@ export default class EditorWatchdog extends Watchdog {
 	}
 
 	/**
-	 * Creates a watched editor instance using the creator passed to the {@link #setCreator `setCreator()`} method or
-	 * the {@link module:watchdog/editorwatchdog~EditorWatchdog.for `Watchdog.for()`} helper.
+	 * Creates and keep running the editor instance using the defined creator and destructor.
 	 *
-	 * @param {HTMLElement|String|Object.<String|String>} [elementOrData]
-	 * @param {module:core/editor/editorconfig~EditorConfig} [config]
+	 * @param {HTMLElement|String|Object.<String|String>} [elementOrData] Editor's source element or the editor's data.
+	 * @param {module:core/editor/editorconfig~EditorConfig} [config] Editor configuration.
 	 * @param {Object} [context] A context for the editor.
 	 *
 	 * @returns {Promise}
@@ -171,20 +172,6 @@ export default class EditorWatchdog extends Watchdog {
 	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/editorwatchdog~EditorWatchdog#setCreator `Watchdog#setCreator()`} or
-					 * the {@link module:watchdog/editorwatchdog~EditorWatchdog.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;
@@ -327,27 +314,6 @@ export default class EditorWatchdog extends Watchdog {
 		} );
 	}
 
-	/**
-	 * A shorthand method for creating an instance of the watchdog. For the full usage, see the
-	 * {@link ~Watchdog `Watchdog` class description}.
-	 *
-	 * Usage:
-	 *
-	 *		const watchdog = Watchdog.for( ClassicEditor );
-	 *
-	 *		watchdog.create( elementOrData, config );
-	 *
-	 * @param {*} Editor The editor class.
-	 * @param {module:watchdog/watchdog~WatchdogConfig} [watchdogConfig] The watchdog plugin configuration.
-	 */
-	static for( Editor, watchdogConfig ) {
-		const watchdog = new this( watchdogConfig );
-
-		watchdog.setCreator( ( elementOrData, config ) => Editor.create( elementOrData, config ) );
-
-		return watchdog;
-	}
-
 	/**
 	 * Fired after the watchdog restarts the error in case of a crash.
 	 *

+ 1 - 0
packages/ckeditor5-watchdog/tests/contextwatchdog.js

@@ -553,6 +553,7 @@ describe( 'ContextWatchdog', () => {
 				setTimeout( () => throwCKEditorError( 'foo', watchdog.get( 'editor1' ) ) );
 				setTimeout( () => throwCKEditorError( 'foo', watchdog.get( 'editor1' ) ) );
 				setTimeout( () => throwCKEditorError( 'foo', watchdog.get( 'editor1' ) ) );
+
 				await waitCycle();
 
 				expect( watchdog.getState( 'editor1' ) ).to.equal( 'crashedPermanently' );

+ 28 - 102
packages/ckeditor5-watchdog/tests/editorwatchdog.js

@@ -10,7 +10,6 @@ import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
 import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
-import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
 
 // The error handling testing with mocha & chai is quite broken and hard to test.
@@ -49,16 +48,6 @@ describe( 'EditorWatchdog', () => {
 			sinon.assert.calledOnce( editorDestroySpy );
 		} );
 
-		it( 'should throw an error when the creator is not defined', async () => {
-			const watchdog = new EditorWatchdog();
-
-			try {
-				await watchdog.create();
-			} catch ( err ) {
-				assertCKEditorError( err, /^watchdog-creator-not-defined/, null );
-			}
-		} );
-
 		it( 'should properly copy the config', async () => {
 			const watchdog = new EditorWatchdog();
 			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
@@ -107,7 +96,7 @@ describe( 'EditorWatchdog', () => {
 
 	describe( 'editor', () => {
 		it( 'should be the current editor instance', () => {
-			const watchdog = EditorWatchdog.for( ClassicTestEditor );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
@@ -146,7 +135,7 @@ describe( 'EditorWatchdog', () => {
 
 	describe( 'error handling', () => {
 		it( 'Watchdog should not restart editor during the initialization', () => {
-			const watchdog = new EditorWatchdog();
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 			let editor;
 
 			watchdog.setCreator( async el => {
@@ -166,9 +155,8 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'Watchdog should not restart editor during the destroy', async () => {
-			const watchdog = new EditorWatchdog();
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
-			watchdog.setCreator( el => ClassicTestEditor.create( el ) );
 			watchdog.setDestructor( () => Promise.reject( new Error( 'foo' ) ) );
 
 			await watchdog.create( element );
@@ -192,9 +180,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'Watchdog should not hide intercepted errors', () => {
-			const watchdog = new EditorWatchdog();
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
@@ -220,9 +206,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'Watchdog should intercept editor errors and restart the editor during the runtime', () => {
-			const watchdog = new EditorWatchdog();
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
@@ -242,9 +226,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'Watchdog should not intercept non-editor errors', () => {
-			const watchdog = new EditorWatchdog();
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			const editorErrorSpy = sinon.spy();
 			watchdog.on( 'error', editorErrorSpy );
@@ -287,8 +269,8 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'Watchdog should not intercept other editor errors', () => {
-			const watchdog1 = EditorWatchdog.for( ClassicTestEditor );
-			const watchdog2 = EditorWatchdog.for( ClassicTestEditor );
+			const watchdog1 = new EditorWatchdog( ClassicTestEditor );
+			const watchdog2 = new EditorWatchdog( ClassicTestEditor );
 
 			const config = {
 				plugins: []
@@ -325,9 +307,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context', async () => {
-			const watchdog = new EditorWatchdog();
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
@@ -347,9 +327,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context #2', async () => {
-			const watchdog = new EditorWatchdog();
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
@@ -379,9 +357,7 @@ describe( 'EditorWatchdog', () => {
 
 		it( 'Watchdog should crash permanently if the `crashNumberLimit` is reached' +
 			' and the average time between errors is lower than `minimumNonErrorTimePeriod` (default values)', async () => {
-			const watchdog = new EditorWatchdog();
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			const errorSpy = sinon.spy();
 			watchdog.on( 'error', errorSpy );
@@ -413,9 +389,7 @@ describe( 'EditorWatchdog', () => {
 
 		it( 'Watchdog should crash permanently if the `crashNumberLimit` is reached' +
 			' and the average time between errors is lower than `minimumNonErrorTimePeriod` (custom values)', async () => {
-			const watchdog = new EditorWatchdog( { crashNumberLimit: 2, minimumNonErrorTimePeriod: 1000 } );
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
+			const watchdog = new EditorWatchdog( ClassicTestEditor, { crashNumberLimit: 2, minimumNonErrorTimePeriod: 1000 } );
 
 			const errorSpy = sinon.spy();
 			watchdog.on( 'error', errorSpy );
@@ -447,9 +421,7 @@ describe( 'EditorWatchdog', () => {
 
 		it( 'Watchdog should not crash permanently when average time between errors' +
 			' is longer than `minimumNonErrorTimePeriod`', async () => {
-			const watchdog = new EditorWatchdog( { crashNumberLimit: 2, minimumNonErrorTimePeriod: 0 } );
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
+			const watchdog = new EditorWatchdog( ClassicTestEditor, { crashNumberLimit: 2, minimumNonErrorTimePeriod: 0 } );
 
 			const errorSpy = sinon.spy();
 			watchdog.on( 'error', errorSpy );
@@ -482,10 +454,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'Watchdog should warn if the CKEditorError is missing its context', async () => {
-			const watchdog = new EditorWatchdog();
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
-			watchdog.setDestructor( editor => editor.destroy() );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
@@ -512,9 +481,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'Watchdog should omit error if the CKEditorError context is equal to null', async () => {
-			const watchdog = new EditorWatchdog();
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
@@ -534,9 +501,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'editor should be restarted with the data from before the crash #1', () => {
-			const watchdog = new EditorWatchdog();
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
@@ -561,9 +526,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'editor should be restarted with the data before the crash #2', () => {
-			const watchdog = new EditorWatchdog();
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
@@ -594,9 +557,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'editor should be restarted with the data of the latest document version before the crash', () => {
-			const watchdog = new EditorWatchdog();
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
@@ -632,9 +593,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		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 ) );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
@@ -695,10 +654,9 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'should use the custom destructor if passed', () => {
-			const watchdog = new EditorWatchdog();
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 			const destructionSpy = sinon.spy();
 
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
 			watchdog.setDestructor( editor => {
 				destructionSpy();
 				return editor.destroy();
@@ -739,7 +697,7 @@ describe( 'EditorWatchdog', () => {
 				return;
 			}
 
-			const watchdog = EditorWatchdog.for( ClassicTestEditor );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 			const originalErrorHandler = window.onerror;
 
 			window.onerror = undefined;
@@ -770,7 +728,7 @@ describe( 'EditorWatchdog', () => {
 				return;
 			}
 
-			const watchdog = EditorWatchdog.for( ClassicTestEditor );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 			const originalErrorHandler = window.onerror;
 			const editorErrorSpy = sinon.spy();
 
@@ -805,7 +763,7 @@ describe( 'EditorWatchdog', () => {
 			// This will ensure that the second data save action will be put off in time.
 			const SAVE_INTERVAL = 30;
 
-			const watchdog = EditorWatchdog.for( ClassicTestEditor, {
+			const watchdog = new EditorWatchdog( ClassicTestEditor, {
 				saveInterval: SAVE_INTERVAL,
 			} );
 
@@ -836,41 +794,9 @@ describe( 'EditorWatchdog', () => {
 		} );
 	} );
 
-	describe( 'static for()', () => {
-		it( 'should be a shortcut method for creating the watchdog', () => {
-			const watchdog = EditorWatchdog.for( ClassicTestEditor );
-
-			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
-			const originalErrorHandler = window.onerror;
-			window.onerror = undefined;
-
-			return watchdog.create( element, {
-				initialData: '<p>foo</p>',
-				plugins: [ Paragraph ]
-			} ).then( () => {
-				const oldEditor = watchdog.editor;
-				expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
-
-				setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
-
-				return new Promise( res => {
-					watchdog.on( 'restart', () => {
-						window.onerror = originalErrorHandler;
-
-						expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
-						expect( watchdog.editor ).to.not.equal( oldEditor );
-						expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
-
-						watchdog.destroy().then( res );
-					} );
-				} );
-			} );
-		} );
-	} );
-
 	describe( 'crashes', () => {
 		it( 'should be an array of caught errors by the watchdog', () => {
-			const watchdog = EditorWatchdog.for( ClassicTestEditor );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
@@ -905,7 +831,7 @@ describe( 'EditorWatchdog', () => {
 					return;
 				}
 
-				const watchdog = EditorWatchdog.for( ClassicTestEditor );
+				const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 				// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 				const originalErrorHandler = window.onerror;
@@ -946,7 +872,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'should reflect the state of the watchdog', async () => {
-			const watchdog = EditorWatchdog.for( ClassicTestEditor );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
@@ -976,7 +902,7 @@ describe( 'EditorWatchdog', () => {
 		} );
 
 		it( 'should be observable', async () => {
-			const watchdog = EditorWatchdog.for( ClassicTestEditor );
+			const watchdog = new EditorWatchdog( ClassicTestEditor );
 			const states = [];
 
 			watchdog.on( 'change:state', ( evt, propName, newValue ) => {
@@ -1052,7 +978,7 @@ describe( 'EditorWatchdog', () => {
 				}
 			}
 
-			const watchdog = EditorWatchdog.for( MultiRootEditor );
+			const watchdog = new EditorWatchdog( MultiRootEditor );
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;

+ 1 - 1
packages/ckeditor5-watchdog/tests/manual/watchdog.js

@@ -63,7 +63,7 @@ document.getElementById( 'random-error' ).addEventListener( 'click', () => {
 } );
 
 function createWatchdog( editorElement, stateElement, name ) {
-	const watchdog = EditorWatchdog.for( ClassicEditor );
+	const watchdog = new EditorWatchdog( ClassicEditor );
 
 	watchdog.create( editorElement, editorConfig );