瀏覽代碼

Removed observable `Watchdog#state`, fixed tests.

Maciej Bukowski 5 年之前
父節點
當前提交
88ca4024b2

+ 7 - 7
packages/ckeditor5-watchdog/src/contextwatchdog.js

@@ -102,7 +102,7 @@ export default class ContextWatchdog extends Watchdog {
 		this._actionQueue.onEmpty( () => {
 			if ( this.state === 'initializing' ) {
 				this.state = 'ready';
-				this.fire( 'stateChange' );
+				this._fire( 'stateChange' );
 			}
 		} );
 
@@ -269,7 +269,7 @@ export default class ContextWatchdog extends Watchdog {
 					// Enqueue the internal watchdog errors within the main queue.
 					// And propagate the internal `error` events as `itemError` event.
 					watchdog.on( 'error', ( evt, { error, causesRestart } ) => {
-						this.fire( 'itemError', { itemId: item.id, error } );
+						this._fire( 'itemError', { itemId: item.id, error } );
 
 						// Do not enqueue the item restart action if the item will not restart.
 						if ( !causesRestart ) {
@@ -277,12 +277,12 @@ export default class ContextWatchdog extends Watchdog {
 						}
 
 						this._actionQueue.enqueue( () => new Promise( res => {
-							watchdog.on( 'restart', rethrowRestartEventOnce );
+							watchdog.on( 'restart', rethrowRestartEventOnce.bind( this ) );
 
 							function rethrowRestartEventOnce() {
 								watchdog.off( 'restart', rethrowRestartEventOnce );
 
-								this.fire( 'itemRestart', { itemId: item.id } );
+								this._fire( 'itemRestart', { itemId: item.id } );
 
 								res();
 							}
@@ -336,7 +336,7 @@ export default class ContextWatchdog extends Watchdog {
 	destroy() {
 		return this._actionQueue.enqueue( () => {
 			this.state = 'destroyed';
-			this.fire( 'stateChange' );
+			this._fire( 'stateChange' );
 
 			super.destroy();
 
@@ -353,14 +353,14 @@ export default class ContextWatchdog extends Watchdog {
 	_restart() {
 		return this._actionQueue.enqueue( () => {
 			this.state = 'initializing';
-			this.fire( 'stateChange' );
+			this._fire( 'stateChange' );
 
 			return this._destroy()
 				.catch( err => {
 					console.error( 'An error happened during destroying the context or items.', err );
 				} )
 				.then( () => this._create() )
-				.then( () => this.fire( 'restart' ) );
+				.then( () => this._fire( 'restart' ) );
 		} );
 	}
 

+ 4 - 4
packages/ckeditor5-watchdog/src/editorwatchdog.js

@@ -140,7 +140,7 @@ export default class EditorWatchdog extends Watchdog {
 		return Promise.resolve()
 			.then( () => {
 				this.state = 'initializing';
-				this.fire( 'stateChange' );
+				this._fire( 'stateChange' );
 
 				return this._destroy();
 			} )
@@ -159,7 +159,7 @@ export default class EditorWatchdog extends Watchdog {
 				}
 			} )
 			.then( () => {
-				this.fire( 'restart' );
+				this._fire( 'restart' );
 			} );
 	}
 
@@ -196,7 +196,7 @@ export default class EditorWatchdog extends Watchdog {
 				this._data = this._getData();
 
 				this.state = 'ready';
-				this.fire( 'stateChange' );
+				this._fire( 'stateChange' );
 			} );
 	}
 
@@ -210,7 +210,7 @@ export default class EditorWatchdog extends Watchdog {
 		return Promise.resolve()
 			.then( () => {
 				this.state = 'destroyed';
-				this.fire( 'stateChange' );
+				this._fire( 'stateChange' );
 
 				super.destroy();
 

+ 17 - 15
packages/ckeditor5-watchdog/src/simpleeventemitter.js

@@ -8,12 +8,17 @@
  */
 
 /**
+ * A private class that mimics the {@link module:utils/emittermixin~EmitterMixin} and partially implements
+ * the {@link module:utils/emittermixin~Emitter} interface. Though, there are huge differences between both APIs
+ * as `SimpleEventEmitter` implements only the `on()`, `off()` and `_fire()` methods, and does not provide others.
+ * It also passes `null` as the first argument to event listeners instead of the `EventSource` instance.
+ *
  * @private
  */
 export default class SimpleEventEmitter {
 	constructor() {
 		/**
-		 * Simple event emitter listeners
+		 * A dictionary of event emitter listeners.
 		 *
 		 * @private
 		 * @type {Object.<String,Array.<Function>>}
@@ -23,12 +28,12 @@ export default class SimpleEventEmitter {
 
 	/**
 	 * Starts listening to the specific event name by registering a callback that will be executed
-	 * when the event with the given name will be fired.
+	 * whenever an event with given name fires.
 	 *
 	 * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
 	 *
-	 * @param {String} eventName
-	 * @param {Function} callback
+	 * @param {String} eventName  Event name.
+	 * @param {Function} callback A callback which will be added to event listeners.
 	 */
 	on( eventName, callback ) {
 		if ( !this._listeners[ eventName ] ) {
@@ -39,31 +44,28 @@ export default class SimpleEventEmitter {
 	}
 
 	/**
-	 * Stops listening to the specified event name by removing the callback.
+	 * Stops listening to the specified event name by removing the callback from event listeners.
 	 *
 	 * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
 	 *
-	 * @param {String} eventName
-	 * @param {Function} callback
+	 * @param {String} eventName Event name.
+	 * @param {Function} callback A callback which will be removed from event listeners.
 	 */
 	off( eventName, callback ) {
-		if ( !this._listeners[ eventName ] ) {
-			return;
-		}
-
 		this._listeners[ eventName ] = this._listeners[ eventName ]
 			.filter( cb => cb !== callback );
 	}
 
 	/**
-	 * Fires an event with the given event name and arguments.
+	 * Fires an event with given event name and arguments.
 	 *
 	 * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
 	 *
-	 * @param {String} eventName
-	 * @param  {...any} args
+	 * @protected
+	 * @param {String} eventName Event name.
+	 * @param  {...any} args Event arguments.
 	 */
-	fire( eventName, ...args ) {
+	_fire( eventName, ...args ) {
 		const callbacks = this._listeners[ eventName ] || [];
 
 		for ( const callback of callbacks ) {

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

@@ -223,14 +223,14 @@ export default class Watchdog extends SimpleEventEmitter {
 			const causesRestart = this._shouldRestart();
 
 			this.state = 'crashed';
-			this.fire( 'stateChange' );
-			this.fire( 'error', { error, causesRestart } );
+			this._fire( 'stateChange' );
+			this._fire( 'error', { error, causesRestart } );
 
 			if ( causesRestart ) {
 				this._restart();
 			} else {
 				this.state = 'crashedPermanently';
-				this.fire( 'stateChange' );
+				this._fire( 'stateChange' );
 			}
 		}
 	}

+ 18 - 18
packages/ckeditor5-watchdog/tests/editorwatchdog.js

@@ -895,7 +895,7 @@ describe( 'EditorWatchdog', () => {
 			const watchdog = new EditorWatchdog( ClassicTestEditor );
 			const states = [];
 
-			watchdog.on( 'stateChanged', () => {
+			watchdog.on( 'stateChange', () => {
 				states.push( watchdog.state );
 			} );
 
@@ -913,23 +913,23 @@ describe( 'EditorWatchdog', () => {
 
 			window.onerror = originalErrorHandler;
 
-			watchdog.destroy().then( () => {
-				expect( states ).to.deep.equal( [
-					'ready',
-					'crashed',
-					'initializing',
-					'ready',
-					'crashed',
-					'initializing',
-					'ready',
-					'crashed',
-					'initializing',
-					'ready',
-					'crashed',
-					'crashedPermanently',
-					'destroyed'
-				] );
-			} );
+			await watchdog.destroy();
+
+			expect( states ).to.deep.equal( [
+				'ready',
+				'crashed',
+				'initializing',
+				'ready',
+				'crashed',
+				'initializing',
+				'ready',
+				'crashed',
+				'initializing',
+				'ready',
+				'crashed',
+				'crashedPermanently',
+				'destroyed'
+			] );
 		} );
 	} );