浏览代码

Remove `ready` event. Merge `plugins.load()` and `plugins.init()` methods.

Krzysztof Krztoń 6 年之前
父节点
当前提交
77ccae9056

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

@@ -230,10 +230,7 @@ export default class Editor {
 				const removePlugins = config.get( 'removePlugins' ) || [];
 				const extraPlugins = config.get( 'extraPlugins' ) || [];
 
-				return this.plugins.load( plugins.concat( extraPlugins ), removePlugins );
-			} )
-			.then( loadedPlugins => {
-				return this.plugins.init( loadedPlugins );
+				return this.plugins.init( plugins.concat( extraPlugins ), removePlugins );
 			} );
 	}
 

+ 14 - 31
packages/ckeditor5-core/src/plugincollection.js

@@ -144,7 +144,7 @@ export default class PluginCollection {
 	}
 
 	/**
-	 * Loads a set of plugins and adds them to the collection.
+	 * Initializes a set of plugins and adds them to the collection.
 	 *
 	 * @param {Array.<Function|String>} plugins An array of {@link module:core/plugin~PluginInterface plugin constructors}
 	 * or {@link module:core/plugin~PluginInterface.pluginName plugin names}. The second option (names) works only if
@@ -155,7 +155,7 @@ export default class PluginCollection {
 	 * collection.
 	 * @returns {Promise.<Array.<module:core/plugin~PluginInterface>>} returns.loadedPlugins The array of loaded plugins.
 	 */
-	load( plugins, removePlugins = [] ) {
+	init( plugins, removePlugins = [] ) {
 		const that = this;
 		const editor = this._editor;
 		const loading = new Set();
@@ -196,6 +196,8 @@ export default class PluginCollection {
 		}
 
 		return Promise.all( pluginConstructors.map( loadPlugin ) )
+			.then( () => initPlugins( loaded, 'init' ) )
+			.then( () => initPlugins( loaded, 'afterInit' ) )
 			.then( () => loaded );
 
 		function loadPlugin( PluginConstructor ) {
@@ -236,6 +238,16 @@ export default class PluginCollection {
 				} );
 		}
 
+		function initPlugins( loadedPlugins, method ) {
+			return loadedPlugins.reduce( ( promise, plugin ) => {
+				if ( !plugin[ method ] ) {
+					return promise;
+				}
+
+				return promise.then( plugin[ method ].bind( plugin ) );
+			}, Promise.resolve() );
+		}
+
 		function instantiatePlugin( PluginConstructor ) {
 			return new Promise( resolve => {
 				loading.add( PluginConstructor );
@@ -299,29 +311,6 @@ export default class PluginCollection {
 	}
 
 	/**
-	 * Runs the initialisation process ({@link module:core/plugin~Plugin#init `init()`}
-	 * and {@link module:core/plugin~Plugin#afterInit `afterInit()`} methods) for the given set of plugins.
-	 *
-	 * @param {Array.<module:core/plugin~PluginInterface>} plugins The array of plugins to initialise.
-	 * @returns {Promise} A promise which resolves after all given plugins have been initialized.
-	 */
-	init( plugins ) {
-		return initPlugins( plugins, 'init' )
-			.then( () => initPlugins( plugins, 'afterInit' ) )
-			.then( () => { this.fire( 'ready' ); } );
-
-		function initPlugins( loadedPlugins, method ) {
-			return loadedPlugins.reduce( ( promise, plugin ) => {
-				if ( !plugin[ method ] ) {
-					return promise;
-				}
-
-				return promise.then( plugin[ method ].bind( plugin ) );
-			}, Promise.resolve() );
-		}
-	}
-
-	/**
 	 * Destroys all loaded plugins.
 	 *
 	 * @returns {Promise}
@@ -393,9 +382,3 @@ export default class PluginCollection {
 }
 
 mix( PluginCollection, EmitterMixin );
-
-/**
- * Fired after {@link #init plugins are initialized}.
- *
- * @event ready
- */

+ 0 - 2
packages/ckeditor5-core/tests/_utils-tests/classictesteditor.js

@@ -121,7 +121,6 @@ describe( 'ClassicTestEditor', () => {
 
 			class EventWatcher extends Plugin {
 				init() {
-					this.editor.plugins.on( 'ready', spy );
 					this.editor.ui.on( 'ready', spy );
 					this.editor.data.on( 'ready', spy );
 					this.editor.on( 'ready', spy );
@@ -134,7 +133,6 @@ describe( 'ClassicTestEditor', () => {
 				} )
 				.then( editor => {
 					expect( fired ).to.deep.equal( [
-						'ready-plugincollection',
 						'ready-classictesteditorui',
 						'ready-datacontroller',
 						'ready-classictesteditor'

+ 3 - 15
packages/ckeditor5-core/tests/editor/editor.js

@@ -433,9 +433,6 @@ describe( 'Editor', () => {
 				plugins: [ PluginA, PluginD ]
 			} );
 
-			const pluginsReadySpy = sinon.spy().named( 'ready' );
-			editor.plugins.on( 'ready', pluginsReadySpy );
-
 			return editor.initPlugins()
 				.then( () => {
 					sinon.assert.callOrder(
@@ -446,8 +443,7 @@ describe( 'Editor', () => {
 						editor.plugins.get( PluginA ).afterInit,
 						editor.plugins.get( PluginB ).afterInit,
 						editor.plugins.get( PluginC ).afterInit,
-						editor.plugins.get( PluginD ).afterInit,
-						pluginsReadySpy
+						editor.plugins.get( PluginD ).afterInit
 					);
 				} );
 		} );
@@ -752,16 +748,12 @@ describe( 'Editor', () => {
 				plugins: [ PluginA, PluginE ]
 			} );
 
-			const pluginsReadySpy = sinon.spy().named( 'ready' );
-			editor.plugins.on( 'ready', pluginsReadySpy );
-
 			return editor.initPlugins()
 				.then( () => {
 					sinon.assert.callOrder(
 						editor.plugins.get( PluginA ).init,
 						editor.plugins.get( PluginE ).init,
-						editor.plugins.get( PluginA ).afterInit,
-						pluginsReadySpy
+						editor.plugins.get( PluginA ).afterInit
 					);
 				} );
 		} );
@@ -771,16 +763,12 @@ describe( 'Editor', () => {
 				plugins: [ PluginA, PluginF ]
 			} );
 
-			const pluginsReadySpy = sinon.spy().named( 'ready' );
-			editor.plugins.on( 'ready', pluginsReadySpy );
-
 			return editor.initPlugins()
 				.then( () => {
 					sinon.assert.callOrder(
 						editor.plugins.get( PluginA ).init,
 						editor.plugins.get( PluginA ).afterInit,
-						editor.plugins.get( PluginF ).afterInit,
-						pluginsReadySpy
+						editor.plugins.get( PluginF ).afterInit
 					);
 				} );
 		} );

+ 37 - 37
packages/ckeditor5-core/tests/plugincollection.js

@@ -77,7 +77,7 @@ describe( 'PluginCollection', () => {
 		it( 'should not fail when trying to load 0 plugins (empty array)', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [] )
+			return plugins.init( [] )
 				.then( () => {
 					expect( getPlugins( plugins ) ).to.be.empty;
 				} );
@@ -86,7 +86,7 @@ describe( 'PluginCollection', () => {
 		it( 'should add collection items for loaded plugins', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ PluginA, PluginB ] )
+			return plugins.init( [ PluginA, PluginB ] )
 				.then( () => {
 					expect( getPlugins( plugins ).length ).to.equal( 2 );
 
@@ -98,7 +98,7 @@ describe( 'PluginCollection', () => {
 		it( 'should add collection items for loaded plugins using plugin names', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ 'A', 'B' ] )
+			return plugins.init( [ 'A', 'B' ] )
 				.then( () => {
 					expect( getPlugins( plugins ).length ).to.equal( 2 );
 
@@ -111,7 +111,7 @@ describe( 'PluginCollection', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 			const spy = sinon.spy( plugins, '_add' );
 
-			return plugins.load( [ PluginA, PluginC ] )
+			return plugins.init( [ PluginA, PluginC ] )
 				.then( loadedPlugins => {
 					expect( getPlugins( plugins ).length ).to.equal( 3 );
 
@@ -126,7 +126,7 @@ describe( 'PluginCollection', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 			const spy = sinon.spy( plugins, '_add' );
 
-			return plugins.load( [ 'J' ] )
+			return plugins.init( [ 'J' ] )
 				.then( loadedPlugins => {
 					expect( getPlugins( plugins ).length ).to.equal( 3 );
 
@@ -141,7 +141,7 @@ describe( 'PluginCollection', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 			const spy = sinon.spy( plugins, '_add' );
 
-			return plugins.load( [ PluginA, PluginB, PluginC ] )
+			return plugins.init( [ PluginA, PluginB, PluginC ] )
 				.then( loadedPlugins => {
 					expect( getPlugins( plugins ).length ).to.equal( 3 );
 
@@ -156,7 +156,7 @@ describe( 'PluginCollection', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 			const spy = sinon.spy( plugins, '_add' );
 
-			return plugins.load( [ PluginD ] )
+			return plugins.init( [ PluginD ] )
 				.then( loadedPlugins => {
 					expect( getPlugins( plugins ).length ).to.equal( 4 );
 
@@ -172,7 +172,7 @@ describe( 'PluginCollection', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 			const spy = sinon.spy( plugins, '_add' );
 
-			return plugins.load( [ PluginA, PluginE ] )
+			return plugins.init( [ PluginA, PluginE ] )
 				.then( loadedPlugins => {
 					expect( getPlugins( plugins ).length ).to.equal( 3 );
 
@@ -187,7 +187,7 @@ describe( 'PluginCollection', () => {
 		it( 'should load grand child classes', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ PluginG ] )
+			return plugins.init( [ PluginG ] )
 				.then( () => {
 					expect( getPlugins( plugins ).length ).to.equal( 1 );
 				} );
@@ -198,7 +198,7 @@ describe( 'PluginCollection', () => {
 
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ Y ] )
+			return plugins.init( [ Y ] )
 				.then( () => {
 					expect( getPlugins( plugins ).length ).to.equal( 1 );
 				} );
@@ -211,7 +211,7 @@ describe( 'PluginCollection', () => {
 
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ pluginAsFunction ] )
+			return plugins.init( [ pluginAsFunction ] )
 				.then( () => {
 					expect( getPlugins( plugins ).length ).to.equal( 1 );
 				} );
@@ -230,7 +230,7 @@ describe( 'PluginCollection', () => {
 				}
 			}
 
-			return plugins.load( [ PluginA, PluginB, pluginAsFunction, Y ] )
+			return plugins.init( [ PluginA, PluginB, pluginAsFunction, Y ] )
 				.then( () => {
 					expect( plugins.get( PluginA ).editor ).to.equal( editor );
 					expect( plugins.get( PluginB ).editor ).to.equal( editor );
@@ -244,8 +244,8 @@ describe( 'PluginCollection', () => {
 
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ PluginA, PluginX, PluginB ] )
-				// Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
+			return plugins.init( [ PluginA, PluginX, PluginB ] )
+				// Throw here, so if by any chance plugins.init() was resolved correctly catch() will be stil executed.
 				.then( () => {
 					throw new Error( 'Test error: this promise should not be resolved successfully' );
 				} )
@@ -263,8 +263,8 @@ describe( 'PluginCollection', () => {
 
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ 'NonExistentPlugin' ] )
-				// Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
+			return plugins.init( [ 'NonExistentPlugin' ] )
+				// Throw here, so if by any chance plugins.init() was resolved correctly catch() will be stil executed.
 				.then( () => {
 					throw new Error( 'Test error: this promise should not be resolved successfully' );
 				} )
@@ -280,7 +280,7 @@ describe( 'PluginCollection', () => {
 		it( 'should load chosen plugins (plugins and removePlugins are constructors)', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ PluginA, PluginB, PluginC ], [ PluginA ] )
+			return plugins.init( [ PluginA, PluginB, PluginC ], [ PluginA ] )
 				.then( () => {
 					expect( getPlugins( plugins ).length ).to.equal( 2 );
 
@@ -292,7 +292,7 @@ describe( 'PluginCollection', () => {
 		it( 'should load chosen plugins (plugins are constructors, removePlugins are names)', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ PluginA, PluginB, PluginC ], [ 'A' ] )
+			return plugins.init( [ PluginA, PluginB, PluginC ], [ 'A' ] )
 				.then( () => {
 					expect( getPlugins( plugins ).length ).to.equal( 2 );
 
@@ -304,7 +304,7 @@ describe( 'PluginCollection', () => {
 		it( 'should load chosen plugins (plugins and removePlugins are names)', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ 'A', 'B', 'C' ], [ 'A' ] )
+			return plugins.init( [ 'A', 'B', 'C' ], [ 'A' ] )
 				.then( () => {
 					expect( getPlugins( plugins ).length ).to.equal( 2 );
 
@@ -316,7 +316,7 @@ describe( 'PluginCollection', () => {
 		it( 'should load chosen plugins (plugins are names, removePlugins are constructors)', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ 'A', 'B', 'C' ], [ PluginA ] )
+			return plugins.init( [ 'A', 'B', 'C' ], [ PluginA ] )
 				.then( () => {
 					expect( getPlugins( plugins ).length ).to.equal( 2 );
 
@@ -330,7 +330,7 @@ describe( 'PluginCollection', () => {
 
 			const plugins = new PluginCollection( editor, [ AnonymousPlugin ].concat( availablePlugins ) );
 
-			return plugins.load( [ AnonymousPlugin, 'A', 'B' ], [ AnonymousPlugin ] )
+			return plugins.init( [ AnonymousPlugin, 'A', 'B' ], [ AnonymousPlugin ] )
 				.then( () => {
 					expect( getPlugins( plugins ).length ).to.equal( 2 );
 
@@ -343,8 +343,8 @@ describe( 'PluginCollection', () => {
 			const logSpy = testUtils.sinon.stub( log, 'error' );
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ PluginA, PluginB, PluginC, PluginD ], [ PluginA, PluginB ] )
-				// Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
+			return plugins.init( [ PluginA, PluginB, PluginC, PluginD ], [ PluginA, PluginB ] )
+				// Throw here, so if by any chance plugins.init() was resolved correctly catch() will be stil executed.
 				.then( () => {
 					throw new Error( 'Test error: this promise should not be resolved successfully' );
 				} )
@@ -360,7 +360,7 @@ describe( 'PluginCollection', () => {
 			const logSpy = testUtils.sinon.stub( log, 'warn' );
 			const plugins = new PluginCollection( editor );
 
-			return plugins.load( [ PluginFoo, AnotherPluginFoo ] )
+			return plugins.init( [ PluginFoo, AnotherPluginFoo ] )
 				.then( () => {
 					expect( getPlugins( plugins ).length ).to.equal( 2 );
 
@@ -384,7 +384,7 @@ describe( 'PluginCollection', () => {
 			const logSpy = testUtils.sinon.stub( log, 'warn' );
 			const plugins = new PluginCollection( editor );
 
-			return plugins.load( [ PluginFoo ] )
+			return plugins.init( [ PluginFoo ] )
 				.then( () => {
 					expect( getPlugins( plugins ).length ).to.equal( 2 );
 
@@ -405,7 +405,7 @@ describe( 'PluginCollection', () => {
 				const logSpy = testUtils.sinon.stub( log, 'warn' );
 				const plugins = new PluginCollection( editor, availablePlugins );
 
-				return plugins.load( [ 'Foo', AnotherPluginFoo ] )
+				return plugins.init( [ 'Foo', AnotherPluginFoo ] )
 					.then( () => {
 						expect( getPlugins( plugins ).length ).to.equal( 2 );
 
@@ -428,7 +428,7 @@ describe( 'PluginCollection', () => {
 
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ SomePlugin ] )
+			return plugins.init( [ SomePlugin ] )
 				.then( () => {
 					expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
 				} );
@@ -442,7 +442,7 @@ describe( 'PluginCollection', () => {
 
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ SomePlugin ] )
+			return plugins.init( [ SomePlugin ] )
 				.then( () => {
 					expect( plugins.get( 'foo/bar' ) ).to.be.instanceOf( SomePlugin );
 					expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
@@ -452,7 +452,7 @@ describe( 'PluginCollection', () => {
 		it( 'throws if plugin cannot be retrieved by name', () => {
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [] ).then( () => {
+			return plugins.init( [] ).then( () => {
 				expect( () => plugins.get( 'foo' ) )
 					.to.throw( CKEditorError, /^plugincollection-plugin-not-loaded:/ )
 					.with.deep.property( 'data', { plugin: 'foo' } );
@@ -465,7 +465,7 @@ describe( 'PluginCollection', () => {
 
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [] ).then( () => {
+			return plugins.init( [] ).then( () => {
 				expect( () => plugins.get( SomePlugin ) )
 					.to.throw( CKEditorError, /^plugincollection-plugin-not-loaded:/ )
 					.with.deep.property( 'data', { plugin: 'foo' } );
@@ -477,7 +477,7 @@ describe( 'PluginCollection', () => {
 
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [] ).then( () => {
+			return plugins.init( [] ).then( () => {
 				expect( () => plugins.get( SomePlugin ) )
 					.to.throw( CKEditorError, /^plugincollection-plugin-not-loaded:/ )
 					.with.deep.property( 'data', { plugin: 'SomePlugin' } );
@@ -504,13 +504,13 @@ describe( 'PluginCollection', () => {
 		} );
 
 		it( 'returns true if plugins is loaded (retrieved by name)', () => {
-			return plugins.load( [ PluginA ] ).then( () => {
+			return plugins.init( [ PluginA ] ).then( () => {
 				expect( plugins.has( 'A' ) ).to.be.true;
 			} );
 		} );
 
 		it( 'returns true if plugins is loaded (retrieved by class)', () => {
-			return plugins.load( [ PluginA ] ).then( () => {
+			return plugins.init( [ PluginA ] ).then( () => {
 				expect( plugins.has( PluginA ) ).to.be.true;
 			} );
 		} );
@@ -522,7 +522,7 @@ describe( 'PluginCollection', () => {
 
 			const plugins = new PluginCollection( editor, [] );
 
-			return plugins.load( [ PluginA, PluginB ] )
+			return plugins.init( [ PluginA, PluginB ] )
 				.then( () => {
 					destroySpyForPluginA = sinon.spy( plugins.get( PluginA ), 'destroy' );
 					destroySpyForPluginB = sinon.spy( plugins.get( PluginB ), 'destroy' );
@@ -566,7 +566,7 @@ describe( 'PluginCollection', () => {
 
 			const plugins = new PluginCollection( editor, [] );
 
-			return plugins.load( [ AsynchronousPluginA, AsynchronousPluginB ] )
+			return plugins.init( [ AsynchronousPluginA, AsynchronousPluginB ] )
 				.then( () => plugins.destroy() )
 				.then( () => {
 					expect( destroyedPlugins ).to.contain( 'AsynchronousPluginB.destroy()' );
@@ -583,7 +583,7 @@ describe( 'PluginCollection', () => {
 
 			const plugins = new PluginCollection( editor, [] );
 
-			return plugins.load( [ PluginA, FooPlugin ] )
+			return plugins.init( [ PluginA, FooPlugin ] )
 				.then( () => plugins.destroy() )
 				.then( destroyedPlugins => {
 					expect( destroyedPlugins.length ).to.equal( 1 );
@@ -608,7 +608,7 @@ describe( 'PluginCollection', () => {
 
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ SomePlugin1, SomePlugin2 ] )
+			return plugins.init( [ SomePlugin1, SomePlugin2 ] )
 				.then( () => {
 					const pluginConstructors = Array.from( plugins )
 						.map( entry => entry[ 0 ] );