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

Introduced the `getState()` method.

Maciej Bukowski 6 лет назад
Родитель
Сommit
33d19d8462

+ 29 - 5
packages/ckeditor5-watchdog/src/contextwatchdog.js

@@ -106,17 +106,26 @@ export default class ContextWatchdog extends Watchdog {
 	 * 	const editor1 = contextWatchdog.get( 'editor1' );
 	 *
 	 * @param {String} itemName The item name (the key under which the item config was passed to the add() method).
+	 * @returns {*} The item instance.
 	 */
 	get( itemName ) {
-		const watchdog = this._watchdogs.get( itemName );
-
-		if ( !watchdog ) {
-			throw new Error( `Item with the given name was not registered: ${ itemName }.` );
-		}
+		const watchdog = this._getWatchdog( itemName );
 
 		return watchdog._instance;
 	}
 
+	/**
+	 * Gets state of the given item. For the list of available state see {@link #state}.
+	 *
+	 * @param {String} itemName The item name (the key under which the item config was passed to the add() method).
+	 * @returns {'initializing'|'ready'|'crashed'|'crashedPermanently'|'destroyed'} The state of the item.
+	 */
+	getState( itemName ) {
+		const watchdog = this._getWatchdog( itemName );
+
+		return watchdog.state;
+	}
+
 	/**
 	 * Adds items to the watchdog. Instances of these items once created will be available using the {@link #get} method.
 	 *
@@ -314,6 +323,21 @@ export default class ContextWatchdog extends Watchdog {
 			} );
 	}
 
+	/**
+	 * @protected
+	 * @param {String} itemName
+	 * @returns {module:watchdog/watchdog~Watchdog} Watchdog
+	 */
+	_getWatchdog( itemName ) {
+		const watchdog = this._watchdogs.get( itemName );
+
+		if ( !watchdog ) {
+			throw new Error( `Item with the given name was not registered: ${ itemName }.` );
+		}
+
+		return watchdog;
+	}
+
 	/**
 	 * Checks whether the error comes from the Context and not from Editor or ContextItem instances.
 	 *

+ 8 - 10
packages/ckeditor5-watchdog/tests/contextwatchdog.js

@@ -413,7 +413,7 @@ describe( 'ContextWatchdog', () => {
 
 				sinon.assert.calledOnce( restartSpy );
 
-				expect( watchdog._watchdogs.get( 'editor1' ).state ).to.equal( 'ready' );
+				expect( watchdog.getState( 'editor1' ) ).to.equal( 'ready' );
 				expect( watchdog.context ).to.not.equal( oldContext );
 
 				await watchdog.destroy();
@@ -497,8 +497,8 @@ describe( 'ContextWatchdog', () => {
 				sinon.assert.notCalled( mainWatchdogRestartSpy );
 				sinon.assert.notCalled( editorWatchdog2RestartSpy );
 
-				expect( editorWatchdog1.state ).to.equal( 'ready' );
-				expect( editorWatchdog2.state ).to.equal( 'ready' );
+				expect( watchdog.getState( 'editor1' ) ).to.equal( 'ready' );
+				expect( watchdog.getState( 'editor2' ) ).to.equal( 'ready' );
 				expect( watchdog.state ).to.equal( 'ready' );
 
 				expect( oldEditor1 ).to.not.equal( editorWatchdog1.editor );
@@ -564,15 +564,13 @@ describe( 'ContextWatchdog', () => {
 
 				await watchdog.waitForReady();
 
-				const editorWatchdog = watchdog._watchdogs.get( 'editor1' );
-
-				setTimeout( () => throwCKEditorError( 'foo', editorWatchdog.editor ) );
-				setTimeout( () => throwCKEditorError( 'foo', editorWatchdog.editor ) );
-				setTimeout( () => throwCKEditorError( 'foo', editorWatchdog.editor ) );
-				setTimeout( () => throwCKEditorError( 'foo', editorWatchdog.editor ) );
+				setTimeout( () => throwCKEditorError( 'foo', watchdog.get( 'editor1' ) ) );
+				setTimeout( () => throwCKEditorError( 'foo', watchdog.get( 'editor1' ) ) );
+				setTimeout( () => throwCKEditorError( 'foo', watchdog.get( 'editor1' ) ) );
+				setTimeout( () => throwCKEditorError( 'foo', watchdog.get( 'editor1' ) ) );
 				await waitCycle();
 
-				expect( editorWatchdog.state ).to.equal( 'crashedPermanently' );
+				expect( watchdog.getState( 'editor1' ) ).to.equal( 'crashedPermanently' );
 				expect( watchdog.state ).to.equal( 'ready' );
 
 				await watchdog.destroy();