瀏覽代碼

Improvements for Plugin, PluginCollection and Editor classes. See details.

Added default implementation for `Plugin.destroy()`.

Introduced `PluginCollection.destroy()` method which calls `Plugin.destroy()` for each loaded plugin.

`Editor.destroy()` calls `PluginCollection.destroy()` method too.
Kamil Piechaczek 8 年之前
父節點
當前提交
409bf0e894

+ 1 - 0
packages/ckeditor5-core/src/editor/editor.js

@@ -149,6 +149,7 @@ export default class Editor {
 
 		return Promise.resolve()
 			.then( () => {
+				this.plugins.destroy();
 				this.document.destroy();
 				this.data.destroy();
 			} );

+ 1 - 0
packages/ckeditor5-core/src/plugin.js

@@ -34,6 +34,7 @@ export default class Plugin {
 	 * @inheritDoc
 	 */
 	destroy() {
+		this.stopListening();
 	}
 }
 

+ 15 - 0
packages/ckeditor5-core/src/plugincollection.js

@@ -208,6 +208,21 @@ export default class PluginCollection {
 	}
 
 	/**
+	 * Destroys each loaded plugin.
+	 *
+	 * @returns {Promise}
+	 */
+	destroy() {
+		const promises = [];
+
+		for ( const [ , pluginInstance ] of this ) {
+			promises.push( pluginInstance.destroy() );
+		}
+
+		return Promise.all( promises );
+	}
+
+	/**
 	 * Adds the plugin to the collection. Exposed mainly for testing purposes.
 	 *
 	 * @protected

+ 2 - 0
packages/ckeditor5-core/tests/editor/editor-base.js

@@ -41,11 +41,13 @@ describe( 'Editor', () => {
 
 			const spy1 = sinon.spy( editor.data, 'destroy' );
 			const spy2 = sinon.spy( editor.document, 'destroy' );
+			const spy3 = sinon.spy( editor.plugins, 'destroy' );
 
 			return editor.destroy()
 				.then( () => {
 					expect( spy1.calledOnce ).to.be.true;
 					expect( spy2.calledOnce ).to.be.true;
+					expect( spy3.calledOnce ).to.be.true;
 				} );
 		} );
 	} );

+ 9 - 0
packages/ckeditor5-core/tests/plugin.js

@@ -25,5 +25,14 @@ describe( 'constructor()', () => {
 
 			expect( plugin.destroy ).to.be.a( 'function' );
 		} );
+
+		it( 'should stop listening', () => {
+			const plugin = new Plugin( editor );
+			const stopListeningSpy = sinon.spy( plugin, 'stopListening' );
+
+			plugin.destroy();
+
+			expect( stopListeningSpy.calledOnce ).to.equal( true );
+		} );
 	} );
 } );

+ 20 - 0
packages/ckeditor5-core/tests/plugincollection.js

@@ -381,6 +381,26 @@ describe( 'PluginCollection', () => {
 		} );
 	} );
 
+	describe( 'destroy()', () => {
+		it( 'calls "destroy" method on each loaded plugin', () => {
+			let destroySpyForPluginA, destroySpyForPluginB;
+
+			const plugins = new PluginCollection( editor, [] );
+
+			return plugins.load( [ PluginA, PluginB ] )
+				.then( () => {
+					destroySpyForPluginA = testUtils.sinon.spy( plugins.get( PluginA ), 'destroy' );
+					destroySpyForPluginB = testUtils.sinon.spy( plugins.get( PluginB ), 'destroy' );
+
+					return plugins.destroy();
+				} )
+				.then( () => {
+					expect( destroySpyForPluginA.calledOnce ).to.equal( true );
+					expect( destroySpyForPluginB.calledOnce ).to.equal( true );
+				} );
+		} );
+	} );
+
 	describe( 'iterator', () => {
 		it( 'exists', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );