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

Made the `Watchdog#state` observable.

Maciej Bukowski 6 лет назад
Родитель
Сommit
cf4785d953
2 измененных файлов с 83 добавлено и 10 удалено
  1. 6 6
      packages/ckeditor5-watchdog/src/watchdog.js
  2. 77 4
      packages/ckeditor5-watchdog/tests/watchdog.js

+ 6 - 6
packages/ckeditor5-watchdog/src/watchdog.js

@@ -10,7 +10,7 @@
 /* globals console, window */
 /* globals console, window */
 
 
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
-import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
+import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
 import { throttle, cloneDeepWith, isElement } from 'lodash-es';
 import { throttle, cloneDeepWith, isElement } from 'lodash-es';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import areConnectedThroughProperties from '@ckeditor/ckeditor5-utils/src/areconnectedthroughproperties';
 import areConnectedThroughProperties from '@ckeditor/ckeditor5-utils/src/areconnectedthroughproperties';
@@ -53,9 +53,9 @@ export default class Watchdog {
 		 *
 		 *
 		 * @public
 		 * @public
 		 * @observable
 		 * @observable
-		 * @type {'initializing'|'ready'|'crashed'|'crashedPermanently'}
+		 * @member {'initializing'|'ready'|'crashed'|'crashedPermanently'} #state
 		 */
 		 */
-		this.state = 'initializing';
+		this.set( 'state', 'initializing' );
 
 
 		/**
 		/**
 		 * Crash number limit (defaults to `3`). After this limit is reached and the {@link #_minNonErrorTimePeriod}
 		 * Crash number limit (defaults to `3`). After this limit is reached and the {@link #_minNonErrorTimePeriod}
@@ -307,9 +307,9 @@ export default class Watchdog {
 			} );
 			} );
 
 
 			this.fire( 'error', { error: evt.error } );
 			this.fire( 'error', { error: evt.error } );
+			this.state = 'crashed';
 
 
 			if ( this._shouldRestartEditor() ) {
 			if ( this._shouldRestartEditor() ) {
-				this.state = 'crashed';
 				this._restart();
 				this._restart();
 			} else {
 			} else {
 				this.state = 'crashedPermanently';
 				this.state = 'crashedPermanently';
@@ -341,7 +341,7 @@ export default class Watchdog {
 	}
 	}
 
 
 	/**
 	/**
-	 * Checks if the editor should be restared.
+	 * Checks if the editor should be restared or if it should be marked as crashed.
 	 */
 	 */
 	_shouldRestartEditor() {
 	_shouldRestartEditor() {
 		if ( this.crashes.length <= this._crashNumberLimit ) {
 		if ( this.crashes.length <= this._crashNumberLimit ) {
@@ -431,4 +431,4 @@ export default class Watchdog {
 	 */
 	 */
 }
 }
 
 
-mix( Watchdog, EmitterMixin );
+mix( Watchdog, ObservableMixin );

+ 77 - 4
packages/ckeditor5-watchdog/tests/watchdog.js

@@ -729,10 +729,7 @@ describe( 'Watchdog', () => {
 
 
 	describe( 'crashes', () => {
 	describe( 'crashes', () => {
 		it( 'should be an array of caught errors by the watchdog', () => {
 		it( 'should be an array of caught errors by the watchdog', () => {
-			const watchdog = new Watchdog();
-
-			watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
-			watchdog.setDestructor( editor => editor.destroy() );
+			const watchdog = Watchdog.for( ClassicTestEditor );
 
 
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
 			const originalErrorHandler = window.onerror;
 			const originalErrorHandler = window.onerror;
@@ -755,6 +752,82 @@ describe( 'Watchdog', () => {
 			} );
 			} );
 		} );
 		} );
 	} );
 	} );
+
+	describe( 'state', () => {
+		it( 'should reflect the state of the watchdog', () => {
+			const watchdog = Watchdog.for( ClassicTestEditor );
+
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
+			const originalErrorHandler = window.onerror;
+			window.onerror = undefined;
+
+			expect( watchdog.state ).to.equal( 'initializing' );
+
+			return watchdog.create( element ).then( () => {
+				expect( watchdog.state ).to.equal( 'ready' );
+
+				return watchdog.create( element ).then( () => {
+					setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
+					setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
+
+					return new Promise( res => {
+						setTimeout( () => {
+							window.onerror = originalErrorHandler;
+
+							expect( watchdog.state ).to.equal( 'ready' );
+
+							watchdog.destroy().then( res );
+						} );
+					} );
+				} );
+			} );
+		} );
+
+		it( 'should be observable', () => {
+			const watchdog = Watchdog.for( ClassicTestEditor );
+			const states = [];
+
+			watchdog.on( 'change:state', ( evt, propName, newValue ) => {
+				states.push( newValue );
+			} );
+
+			// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
+			const originalErrorHandler = window.onerror;
+			window.onerror = undefined;
+
+			return watchdog.create( element ).then( () => {
+				return watchdog.create( element ).then( () => {
+					setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
+					setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
+					setTimeout( () => throwCKEditorError( 'baz', watchdog.editor ) );
+					setTimeout( () => throwCKEditorError( 'biz', watchdog.editor ) );
+
+					return new Promise( res => {
+						setTimeout( () => {
+							window.onerror = originalErrorHandler;
+
+							expect( states ).to.deep.equal( [
+								'ready',
+								'crashed',
+								'initializing',
+								'ready',
+								'crashed',
+								'initializing',
+								'ready',
+								'crashed',
+								'initializing',
+								'ready',
+								'crashed',
+								'crashedPermanently'
+							] );
+
+							watchdog.destroy().then( res );
+						} );
+					} );
+				} );
+			} );
+		} );
+	} );
 } );
 } );
 
 
 function throwCKEditorError( name, context ) {
 function throwCKEditorError( name, context ) {