Selaa lähdekoodia

Introduce `data#ready` event.

Krzysztof Krztoń 6 vuotta sitten
vanhempi
commit
95f909c238

+ 12 - 0
packages/ckeditor5-engine/src/controller/datacontroller.js

@@ -103,6 +103,12 @@ export default class DataController {
 		this.upcastDispatcher.on( 'documentFragment', convertToModelFragment(), { priority: 'lowest' } );
 
 		this.decorate( 'init' );
+
+		// Fire `ready` event when initialisation has completed. Such low level listener gives possibility
+		// to plug into initialisation pipeline without interrupting the initialisation flow.
+		this.on( 'init', () => {
+			this.fire( 'ready' );
+		}, { priority: 'lowest' } );
 	}
 
 	/**
@@ -373,6 +379,12 @@ export default class DataController {
 	}
 
 	/**
+	 * Event fired once data initialisation has finished.
+	 *
+	 * @event ready
+	 */
+
+	/**
 	 * Event fired by decorated {@link #init} method.
 	 * See {@link module:utils/observablemixin~ObservableMixin.decorate} for more information and samples.
 	 *

+ 10 - 0
packages/ckeditor5-engine/tests/controller/datacontroller.js

@@ -160,6 +160,16 @@ describe( 'DataController', () => {
 			sinon.assert.calledWithExactly( spy, sinon.match.any, [ 'foo bar' ] );
 		} );
 
+		it( 'should fire ready event after init', () => {
+			const spy = sinon.spy();
+
+			data.on( 'ready', spy );
+
+			data.init( 'foo bar' );
+
+			sinon.assert.called( spy );
+		} );
+
 		it( 'should throw an error when document data is already initialized', () => {
 			data.init( '<p>Foo</p>' );