Преглед на файлове

Better to hide plugincollection implementation.

Piotrek Koszuliński преди 10 години
родител
ревизия
cba22de156
променени са 2 файла, в които са добавени 48 реда и са изтрити 16 реда
  1. 39 7
      packages/ckeditor5-engine/src/plugincollection.js
  2. 9 9
      packages/ckeditor5-engine/tests/plugincollection.js

+ 39 - 7
packages/ckeditor5-engine/src/plugincollection.js

@@ -6,10 +6,9 @@
 'use strict';
 
 /**
- * Manages a list of CKEditor plugins, including loading, initialization and destruction.
+ * Manages a list of CKEditor plugins, including loading, resolving dependencies and initialization.
  *
  * @class PluginCollection
- * @extends Map
  */
 
 import CKEDITOR from '../ckeditor.js';
@@ -18,7 +17,7 @@ import CKEditorError from './ckeditorerror.js';
 import log from './log.js';
 import load from '../ckeditor5/load.js';
 
-export default class PluginCollection extends Map {
+export default class PluginCollection {
 	/**
 	 * Creates an instance of the PluginCollection class, initializing it with a set of plugins.
 	 *
@@ -26,13 +25,35 @@ export default class PluginCollection extends Map {
 	 * @param {core/Editor} editor
 	 */
 	constructor( editor ) {
-		super();
-
 		/**
 		 * @protected
 		 * @property {core/Editor}
 		 */
 		this._editor = editor;
+
+		/**
+		 * @protected
+		 * @property {Map}
+		 */
+		this._plugins = new Map();
+	}
+
+	/**
+	 * Collection iterator. Returns `[ key, plugin ]` pairs. Plugins which are
+	 * kept in the collection twice (under their name and class) will be returned twice.
+	 */
+	[ Symbol.iterator ]() {
+		return this._plugins[ Symbol.iterator ]();
+	}
+
+	/**
+	 * Gets the plugin instance by its name or class.
+	 *
+	 * @param {String/Function} key The name of the plugin or the class.
+	 * @returns {Plugin}
+	 */
+	get( key ) {
+		return this._plugins.get( key );
 	}
 
 	/**
@@ -94,12 +115,12 @@ export default class PluginCollection extends Map {
 				}
 
 				const plugin = new PluginClass( editor );
-				that.set( PluginClass, plugin );
+				that._add( PluginClass, plugin );
 				loaded.push( plugin );
 
 				// Expose the plugin also by its name if loaded through load() by name.
 				if ( pluginName ) {
-					that.set( pluginName, plugin );
+					that._add( pluginName, plugin );
 				}
 
 				resolve();
@@ -121,4 +142,15 @@ export default class PluginCollection extends Map {
 			}
 		}
 	}
+
+	/**
+	 * Adds the plugin to the collection. Exposed mainly for testing purposes.
+	 *
+	 * @protected
+	 * @param {String/Function} key The name or the plugin class.
+	 * @param {Plugin} plugin The instance of the plugin.
+	 */
+	_add( key, plugin ) {
+		this._plugins.set( key, plugin );
+	}
 }

+ 9 - 9
packages/ckeditor5-engine/tests/plugincollection.js

@@ -114,14 +114,14 @@ describe( 'load', () => {
 
 	it( 'should load dependency plugins', () => {
 		let plugins = new PluginCollection( editor );
-		let spy = sinon.spy( plugins, 'set' );
+		let spy = sinon.spy( plugins, '_add' );
 
 		return plugins.load( [ 'A', 'C' ] )
 			.then( ( loadedPlugins ) => {
 				expect( getPlugins( plugins ).length ).to.equal( 3 );
 
 				expect( getPluginNames( getPluginsFromSpy( spy ) ) )
-					.to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins.set()' );
+					.to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
 				expect( getPluginNames( loadedPlugins ) )
 					.to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
 			} );
@@ -129,14 +129,14 @@ describe( 'load', () => {
 
 	it( 'should be ok when dependencies are loaded first', () => {
 		let plugins = new PluginCollection( editor );
-		let spy = sinon.spy( plugins, 'set' );
+		let spy = sinon.spy( plugins, '_add' );
 
 		return plugins.load( [ 'A', 'B', 'C' ] )
 			.then( ( loadedPlugins ) => {
 				expect( getPlugins( plugins ).length ).to.equal( 3 );
 
 				expect( getPluginNames( getPluginsFromSpy( spy ) ) )
-					.to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins.set()' );
+					.to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
 				expect( getPluginNames( loadedPlugins ) )
 					.to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
 			} );
@@ -144,7 +144,7 @@ describe( 'load', () => {
 
 	it( 'should load deep dependency plugins', () => {
 		let plugins = new PluginCollection( editor );
-		let spy = sinon.spy( plugins, 'set' );
+		let spy = sinon.spy( plugins, '_add' );
 
 		return plugins.load( [ 'D' ] )
 			.then( ( loadedPlugins ) => {
@@ -152,7 +152,7 @@ describe( 'load', () => {
 
 				// The order must have dependencies first.
 				expect( getPluginNames( getPluginsFromSpy( spy ) ) )
-					.to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by plugins.set()' );
+					.to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by plugins._add()' );
 				expect( getPluginNames( loadedPlugins ) )
 					.to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by returned value' );
 			} );
@@ -160,7 +160,7 @@ describe( 'load', () => {
 
 	it( 'should handle cross dependency plugins', () => {
 		let plugins = new PluginCollection( editor );
-		let spy = sinon.spy( plugins, 'set' );
+		let spy = sinon.spy( plugins, '_add' );
 
 		return plugins.load( [ 'A', 'E' ] )
 			.then( ( loadedPlugins ) => {
@@ -168,7 +168,7 @@ describe( 'load', () => {
 
 				// The order must have dependencies first.
 				expect( getPluginNames( getPluginsFromSpy( spy ) ) )
-					.to.deep.equal( [ 'A', 'F', 'E' ], 'order by plugins.set()' );
+					.to.deep.equal( [ 'A', 'F', 'E' ], 'order by plugins._add()' );
 				expect( getPluginNames( loadedPlugins ) )
 					.to.deep.equal( [ 'A', 'F', 'E' ], 'order by returned value' );
 			} );
@@ -268,7 +268,7 @@ function createPlugin( name, baseClass ) {
 function getPlugins( pluginCollection ) {
 	const plugins = [];
 
-	for ( let entry of pluginCollection.entries() ) {
+	for ( let entry of pluginCollection ) {
 		if ( typeof entry[ 0 ] == 'function' ) {
 			plugins.push( entry[ 1 ] );
 		}