浏览代码

Merge pull request #78 from ckeditor/t/76

Merged EmitterMixin additional parameters into options object.
Piotrek Koszuliński 9 年之前
父节点
当前提交
0a3d996752
共有 2 个文件被更改,包括 36 次插入33 次删除
  1. 25 22
      packages/ckeditor5-utils/src/emittermixin.js
  2. 11 11
      packages/ckeditor5-utils/tests/emittermixin.js

+ 25 - 22
packages/ckeditor5-utils/src/emittermixin.js

@@ -26,22 +26,21 @@ 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
-	 * event.
+	 * @param {Object} [options.context] 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 = {} ) {
 		createEventNamespace( this, event );
 		const lists = getCallbacksListsForNamespace( this, event );
-
-		priority = priorities.get( priority );
+		const priority = priorities.get( options.priority );
 
 		callback = {
 			callback: callback,
-			ctx: ctx || this,
+			context: options.context || this,
 			priority: priority
 		};
 
@@ -72,12 +71,14 @@ 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
-	 * event.
+	 * @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.context] 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 +88,7 @@ const EmitterMixin = {
 		};
 
 		// Make a similar on() call, simply replacing the callback.
-		this.on( event, onceCallback, priority, ctx );
+		this.on( event, onceCallback, options );
 	},
 
 	/**
@@ -95,17 +96,17 @@ const EmitterMixin = {
 	 *
 	 * @param {String} event The name of the event.
 	 * @param {Function} callback The function to stop being called.
-	 * @param {Object} [ctx] The context object to be removed, pared with the given callback. To handle cases where
+	 * @param {Object} [context] The context object to be removed, pared with the given callback. To handle cases where
 	 * the same callback is used several times with different contexts.
 	 * @method utils.EmitterMixin#off
 	 */
-	off( event, callback, ctx ) {
+	off( event, callback, context ) {
 		const lists = getCallbacksListsForNamespace( this, event );
 
 		for ( let callbacks of lists ) {
 			for ( let i = 0; i < callbacks.length; i++ ) {
 				if ( callbacks[ i ].callback == callback ) {
-					if ( !ctx || ctx == callbacks[ i ].ctx ) {
+					if ( !context || context == callbacks[ i ].context ) {
 						// Remove the callback from the list (fixing the next index).
 						callbacks.splice( i, 1 );
 						i--;
@@ -121,12 +122,14 @@ 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
-	 * event.
+	 * @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.context] 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 +168,7 @@ const EmitterMixin = {
 		eventCallbacks.push( callback );
 
 		// Finally register the callback to the event.
-		emitter.on( event, callback, priority, ctx );
+		emitter.on( event, callback, options );
 	},
 
 	/**
@@ -252,14 +255,14 @@ const EmitterMixin = {
 			callbacks = Array.from( callbacks );
 
 			for ( let i = 0; i < callbacks.length; i++ ) {
-				callbacks[ i ].callback.apply( callbacks[ i ].ctx, callbackArgs );
+				callbacks[ i ].callback.apply( callbacks[ i ].context, callbackArgs );
 
 				// Remove the callback from future requests if off() has been called.
 				if ( eventInfo.off.called ) {
 					// Remove the called mark for the next calls.
 					delete eventInfo.off.called;
 
-					this.off( event, callbacks[ i ].callback, callbacks[ i ].ctx );
+					this.off( event, callbacks[ i ].callback, callbacks[ i ].context );
 				}
 
 				// Do not execute next callbacks if stop() was called.

+ 11 - 11
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, { context: ctx1 } );
+		emitter.on( 'test', spy2, { context: 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, { context: ctx } );
 		emitter.once( 'test', spy2 );
 
 		emitter.fire( 'test' );
@@ -320,11 +320,11 @@ describe( 'off', () => {
 	it( 'should remove the callback for given context only', () => {
 		let spy = sinon.spy().named( 1 );
 
-		let ctx1 = { ctx: 1 };
-		let ctx2 = { ctx: 2 };
+		let ctx1 = { context: 1 };
+		let ctx2 = { context: 2 };
 
-		emitter.on( 'test', spy, 'normal', ctx1 );
-		emitter.on( 'test', spy, 'normal', ctx2 );
+		emitter.on( 'test', spy, { context: ctx1 } );
+		emitter.on( 'test', spy, { context: ctx2 } );
 
 		emitter.fire( 'test' );