Parcourir la source

Merge pull request #135 from ckeditor/t/134

Fix: Editor#destroy waits for the initialization. Closes #134.
Piotr Jasiun il y a 7 ans
Parent
commit
1f97b2ea51

+ 14 - 5
packages/ckeditor5-core/src/editor/editor.js

@@ -239,17 +239,26 @@ export default class Editor {
 	/**
 	 * Destroys the editor instance, releasing all resources used by it.
 	 *
+	 * **Note** The editor can not be destroyed during the initialization phase so
+	 * this methods waits for the editor initialization before destroying.
+	 *
 	 * @fires destroy
 	 * @returns {Promise} A promise that resolves once the editor instance is fully destroyed.
 	 */
 	destroy() {
-		this.fire( 'destroy' );
-
-		this.stopListening();
+		let readyPromise = Promise.resolve();
 
-		this.commands.destroy();
+		if ( this.state == 'initializing' ) {
+			readyPromise = new Promise( resolve => this.once( 'ready', resolve ) );
+		}
 
-		return this.plugins.destroy()
+		return readyPromise
+			.then( () => {
+				this.fire( 'destroy' );
+				this.stopListening();
+				this.commands.destroy();
+			} )
+			.then( () => this.plugins.destroy() )
 			.then( () => {
 				this.model.destroy();
 				this.data.destroy();

+ 6 - 4
packages/ckeditor5-core/tests/_utils-tests/modeltesteditor.js

@@ -29,12 +29,14 @@ describe( 'ModelTestEditor', () => {
 
 		it( 'should disable editing pipeline', () => {
 			const spy = sinon.spy( EditingController.prototype, 'destroy' );
-			const editor = new ModelTestEditor( { foo: 1 } );
 
-			sinon.assert.calledOnce( spy );
+			return ModelTestEditor.create( { foo: 1 } ).then( editor => {
+				sinon.assert.calledOnce( spy );
+
+				spy.restore();
 
-			spy.restore();
-			return editor.destroy();
+				return editor.destroy();
+			} );
 		} );
 
 		it( 'creates main root element', () => {

+ 38 - 22
packages/ckeditor5-core/tests/editor/editor.js

@@ -197,10 +197,10 @@ describe( 'Editor', () => {
 		} );
 
 		it( 'is `destroyed` after editor destroy', () => {
-			const editor = new Editor();
-
-			return editor.destroy().then( () => {
-				expect( editor.state ).to.equal( 'destroyed' );
+			return Editor.create().then( editor => {
+				return editor.destroy().then( () => {
+					expect( editor.state ).to.equal( 'destroyed' );
+				} );
 			} );
 		} );
 
@@ -283,33 +283,49 @@ describe( 'Editor', () => {
 
 	describe( 'destroy()', () => {
 		it( 'should fire "destroy"', () => {
-			const editor = new Editor();
-			const spy = sinon.spy();
+			return Editor.create().then( editor => {
+				const spy = sinon.spy();
 
-			editor.on( 'destroy', spy );
+				editor.on( 'destroy', spy );
 
-			return editor.destroy().then( () => {
-				expect( spy.calledOnce ).to.be.true;
+				return editor.destroy().then( () => {
+					expect( spy.calledOnce ).to.be.true;
+				} );
 			} );
 		} );
 
 		it( 'should destroy all components it initialized', () => {
+			return Editor.create().then( editor => {
+				const dataDestroySpy = sinon.spy( editor.data, 'destroy' );
+				const modelDestroySpy = sinon.spy( editor.model, 'destroy' );
+				const editingDestroySpy = sinon.spy( editor.editing, 'destroy' );
+				const pluginsDestroySpy = sinon.spy( editor.plugins, 'destroy' );
+				const keystrokesDestroySpy = sinon.spy( editor.keystrokes, 'destroy' );
+
+				return editor.destroy()
+					.then( () => {
+						sinon.assert.calledOnce( dataDestroySpy );
+						sinon.assert.calledOnce( modelDestroySpy );
+						sinon.assert.calledOnce( editingDestroySpy );
+						sinon.assert.calledOnce( pluginsDestroySpy );
+						sinon.assert.calledOnce( keystrokesDestroySpy );
+					} );
+			} );
+		} );
+
+		it( 'should wait for the full init before destroying', done => {
+			const spy = sinon.spy();
 			const editor = new Editor();
 
-			const dataDestroySpy = sinon.spy( editor.data, 'destroy' );
-			const modelDestroySpy = sinon.spy( editor.model, 'destroy' );
-			const editingDestroySpy = sinon.spy( editor.editing, 'destroy' );
-			const pluginsDestroySpy = sinon.spy( editor.plugins, 'destroy' );
-			const keystrokesDestroySpy = sinon.spy( editor.keystrokes, 'destroy' );
+			editor.on( 'destroy', () => {
+				done();
+			} );
 
-			return editor.destroy()
-				.then( () => {
-					sinon.assert.calledOnce( dataDestroySpy );
-					sinon.assert.calledOnce( modelDestroySpy );
-					sinon.assert.calledOnce( editingDestroySpy );
-					sinon.assert.calledOnce( pluginsDestroySpy );
-					sinon.assert.calledOnce( keystrokesDestroySpy );
-				} );
+			editor.destroy();
+
+			sinon.assert.notCalled( spy );
+
+			editor.fire( 'ready' );
 		} );
 	} );
 

+ 1 - 0
packages/ckeditor5-core/tests/editor/utils/attachtoform.js

@@ -34,6 +34,7 @@ describe( 'attachToForm()', () => {
 		editor.data.processor = new HtmlDataProcessor();
 		editor.model.document.createRoot();
 		editor.model.schema.extend( '$text', { allowIn: '$root' } );
+		editor.fire( 'ready' );
 
 		editor.data.set( 'foo bar' );
 	} );