Przeglądaj źródła

Added add and remove events to PendingActions plugin.

Oskar Wróbel 7 lat temu
rodzic
commit
bdb54b56a2

+ 18 - 0
packages/ckeditor5-core/src/pendingactions.js

@@ -61,6 +61,7 @@ export default class PendingActions extends Plugin {
 		 * @type {module:utils/collection~Collection}
 		 */
 		this._actions = new Collection( { idProperty: '_id' } );
+		this._actions.delegate( 'add', 'remove' ).to( this );
 
 		// It's not possible to easy test it because karma uses `beforeunload` event
 		// to warn before full page reload and this event cannot be dispatched manually.
@@ -110,8 +111,25 @@ export default class PendingActions extends Plugin {
 		return this._actions[ Symbol.iterator ]();
 	}
 
+	/**
+	 * @inheritDoc
+	 */
 	destroy() {
 		super.destroy();
 		this._domEmitter.stopListening();
 	}
+
+	/**
+	 * Fired when an action is added to the list.
+	 *
+	 * @event add
+	 * @param {Object} action The added action.
+	 */
+
+	/**
+	 * Fired when an action is removed from the list.
+	 *
+	 * @event remove
+	 * @param {Object} action The removed action.
+	 */
 }

+ 22 - 0
packages/ckeditor5-core/tests/pendingactions.js

@@ -73,6 +73,16 @@ describe( 'PendingActions', () => {
 				pendingActions.add( {} );
 			} ).to.throw( CKEditorError, /^pendingactions-add-invalid-message/ );
 		} );
+
+		it( 'should fire add event with added item', () => {
+			const spy = sinon.spy();
+
+			pendingActions.on( 'add', spy );
+
+			const action = pendingActions.add( 'Some action' );
+
+			sinon.assert.calledWith( spy, sinon.match.any, action );
+		} );
 	} );
 
 	describe( 'remove()', () => {
@@ -90,6 +100,18 @@ describe( 'PendingActions', () => {
 
 			expect( pendingActions ).to.have.property( 'isPending', false );
 		} );
+
+		it( 'should fire remove event with removed item', () => {
+			const spy = sinon.spy();
+
+			pendingActions.on( 'remove', spy );
+
+			const action = pendingActions.add( 'Some action' );
+
+			pendingActions.remove( action );
+
+			sinon.assert.calledWith( spy, sinon.match.any, action );
+		} );
 	} );
 
 	describe( 'iterator', () => {