Przeglądaj źródła

Merged EmitterMixin additional parameters into options object.

Oskar Wrobel 9 lat temu
rodzic
commit
5cf9e80f68

+ 30 - 15
packages/ckeditor5-utils/src/emittermixin.js

@@ -26,23 +26,32 @@ const EmitterMixin = {
 	 *
 	 * @param {String} event The name of the event.
 	 * @param {Function} callback The function to be called on event.
-	 * @param {utils.PriorityString|Number} [priority='normal'] The priority of this event callback. The higher
+	 * @param {Object} [options={}] Additional options.
+	 * @param {utils.PriorityString|Number} [options.priority='normal'] The priority of this event callback. The higher
 	 * the priority value the sooner the callback will be fired. Events having the same priority are called in the
 	 * order they were added.
-	 * @param {Object} [ctx] The object that represents `this` in the callback. Defaults to the object firing the
+	 * @param {Object} [options.ctx] The object that represents `this` in the callback. Defaults to the object firing the
 	 * event.
 	 * @method utils.EmitterMixin#on
 	 */
-	on( event, callback, priority = 'normal', ctx = null ) {
+	on( event, callback, options = {} ) {
+		if ( options.priority === undefined ) {
+			options.priority = 'normal';
+		}
+
+		if ( options.ctx === undefined ) {
+			options.ctx = null;
+		}
+
 		createEventNamespace( this, event );
 		const lists = getCallbacksListsForNamespace( this, event );
 
-		priority = priorities.get( priority );
+		options.priority = priorities.get( options.priority );
 
 		callback = {
 			callback: callback,
-			ctx: ctx || this,
-			priority: priority
+			ctx: options.ctx || this,
+			priority: options.priority
 		};
 
 		// Add the callback to all callbacks list.
@@ -51,7 +60,7 @@ const EmitterMixin = {
 			let added = false;
 
 			for ( let i = 0; i < callbacks.length; i++ ) {
-				if ( callbacks[ i ].priority < priority ) {
+				if ( callbacks[ i ].priority < options.priority ) {
 					callbacks.splice( i, 0, callback );
 					added = true;
 
@@ -72,12 +81,15 @@ const EmitterMixin = {
 	 *
 	 * @param {String} event The name of the event.
 	 * @param {Function} callback The function to be called on event.
-	 * @param {utils.EventPriority} [priority='normal'] The priority of this event callback.
-	 * @param {Object} [ctx] The object that represents `this` in the callback. Defaults to the object firing the
+	 * @param {Object} [options={}] Additional options.
+	 * @param {utils.PriorityString|Number} [options.priority='normal'] The priority of this event callback. The higher
+	 * the priority value the sooner the callback will be fired. Events having the same priority are called in the
+	 * order they were added.
+	 * @param {Object} [options.ctx] The object that represents `this` in the callback. Defaults to the object firing the
 	 * event.
 	 * @method utils.EmitterMixin#once
 	 */
-	once( event, callback, priority, ctx ) {
+	once( event, callback, options ) {
 		const onceCallback = function( event ) {
 			// Go off() at the first call.
 			event.off();
@@ -87,7 +99,7 @@ const EmitterMixin = {
 		};
 
 		// Make a similar on() call, simply replacing the callback.
-		this.on( event, onceCallback, priority, ctx );
+		this.on( event, onceCallback, options );
 	},
 
 	/**
@@ -121,12 +133,15 @@ const EmitterMixin = {
 	 * @param {utils.Emitter} emitter The object that fires the event.
 	 * @param {String} event The name of the event.
 	 * @param {Function} callback The function to be called on event.
-	 * @param {utils.EventPriority} [priority='normal'] The priority of this event callback.
-	 * @param {Object} [ctx] The object that represents `this` in the callback. Defaults to the object firing the
+	 * @param {Object} [options={}] Additional options.
+	 * @param {utils.PriorityString|Number} [options.priority='normal'] The priority of this event callback. The higher
+	 * the priority value the sooner the callback will be fired. Events having the same priority are called in the
+	 * order they were added.
+	 * @param {Object} [options.ctx] The object that represents `this` in the callback. Defaults to the object firing the
 	 * event.
 	 * @method utils.EmitterMixin#listenTo
 	 */
-	listenTo( emitter, event, callback, priority, ctx ) {
+	listenTo( emitter, event, callback, options ) {
 		let emitters, emitterId, emitterInfo, eventCallbacks;
 
 		// _listeningTo contains a list of emitters that this object is listening to.
@@ -165,7 +180,7 @@ const EmitterMixin = {
 		eventCallbacks.push( callback );
 
 		// Finally register the callback to the event.
-		emitter.on( event, callback, priority, ctx );
+		emitter.on( event, callback, options );
 	},
 
 	/**

+ 9 - 9
packages/ckeditor5-utils/tests/emittermixin.js

@@ -32,11 +32,11 @@ describe( 'fire', () => {
 		let spy4 = sinon.spy().named( 4 );
 		let spy5 = sinon.spy().named( 5 );
 
-		emitter.on( 'test', spy2, 'high' );
+		emitter.on( 'test', spy2, { priority: 'high' } );
 		emitter.on( 'test', spy3 ); // Defaults to 'normal'.
-		emitter.on( 'test', spy4, 'low' );
-		emitter.on( 'test', spy1, 'highest' );
-		emitter.on( 'test', spy5, 'lowest' );
+		emitter.on( 'test', spy4, { priority: 'low' } );
+		emitter.on( 'test', spy1, { priority: 'highest' } );
+		emitter.on( 'test', spy5, { priority: 'lowest' } );
 
 		emitter.fire( 'test' );
 
@@ -64,8 +64,8 @@ describe( 'fire', () => {
 		let spy2 = sinon.spy();
 		let spy3 = sinon.spy();
 
-		emitter.on( 'test', spy1, 'normal', ctx1 );
-		emitter.on( 'test', spy2, 'normal', ctx2 );
+		emitter.on( 'test', spy1, { ctx: ctx1 } );
+		emitter.on( 'test', spy2, { ctx: ctx2 } );
 		emitter.on( 'test', spy3 );
 
 		emitter.fire( 'test' );
@@ -252,7 +252,7 @@ describe( 'once', () => {
 		let spy1 = sinon.spy();
 		let spy2 = sinon.spy();
 
-		emitter.once( 'test', spy1, 'normal', ctx );
+		emitter.once( 'test', spy1, { ctx } );
 		emitter.once( 'test', spy2 );
 
 		emitter.fire( 'test' );
@@ -323,8 +323,8 @@ describe( 'off', () => {
 		let ctx1 = { ctx: 1 };
 		let ctx2 = { ctx: 2 };
 
-		emitter.on( 'test', spy, 'normal', ctx1 );
-		emitter.on( 'test', spy, 'normal', ctx2 );
+		emitter.on( 'test', spy, { ctx: ctx1 } );
+		emitter.on( 'test', spy, { ctx: ctx2 } );
 
 		emitter.fire( 'test' );