Skip to content

Commit

Permalink
reimplement CallbacksInvoker
Browse files Browse the repository at this point in the history
  • Loading branch information
jareguo committed Aug 7, 2017
1 parent 7ed02e0 commit e3212cd
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 197 deletions.
6 changes: 3 additions & 3 deletions cocos2d/core/CCNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ var Node = cc.Class({
* It's the recommended way to register touch/mouse event for Node,
* please do not use cc.eventManager directly for Node.
* !#zh
* 在节点上注册指定类型的回调函数,也可以设置 target 用于绑定响应函数的调用者。<br/>
* 在节点上注册指定类型的回调函数,也可以设置 target 用于绑定响应函数的 this 对象。<br/>
* 同时您可以将事件派发到父节点或者通过调用 stopPropagation 拦截它。<br/>
* 推荐使用这种方式来监听节点上的触摸或鼠标事件,请不要在节点上直接使用 cc.eventManager。
* @method on
Expand All @@ -1109,7 +1109,7 @@ var Node = cc.Class({
* @param {Function} callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param {Event} callback.event event
* @param {Object} [target] - The target to invoke the callback, can be null
* @param {Object} [target] - The target (this object) to invoke the callback, can be null
* @param {Boolean} [useCapture=false] - When set to true, the capture argument prevents callback
* from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
* When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE.
Expand Down Expand Up @@ -1184,7 +1184,7 @@ var Node = cc.Class({
* @method off
* @param {String} type - A string representing the event type being removed.
* @param {Function} callback - The callback to remove.
* @param {Object} [target] - The target to invoke the callback, if it's not given, only callback without target will be removed
* @param {Object} [target] - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
* @param {Boolean} [useCapture=false] - Specifies whether the callback being removed was registered as a capturing callback or not.
* If not specified, useCapture defaults to false. If a callback was registered twice,
* one with capture and one without, each must be removed separately. Removal of a capturing callback
Expand Down
4 changes: 2 additions & 2 deletions cocos2d/core/components/CCAnimation.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ var Animation = cc.Class({
* @param {Function} callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param {Event} callback.event event
* @param {Object} [target] - The target to invoke the callback, can be null
* @param {Object} [target] - The target (this object) to invoke the callback, can be null
* @param {Boolean} [useCapture=false] - When set to true, the capture argument prevents callback
* from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
* When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE.
Expand Down Expand Up @@ -556,7 +556,7 @@ var Animation = cc.Class({
* @method off
* @param {String} type - A string representing the event type being removed.
* @param {Function} callback - The callback to remove.
* @param {Object} [target] - The target to invoke the callback, if it's not given, only callback without target will be removed
* @param {Object} [target] - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
* @param {Boolean} [useCapture=false] - Specifies whether the callback being removed was registered as a capturing callback or not.
* If not specified, useCapture defaults to false. If a callback was registered twice,
* one with capture and one without, each must be removed separately. Removal of a capturing callback
Expand Down
58 changes: 19 additions & 39 deletions cocos2d/core/event/event-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,59 +26,39 @@
var JS = cc.js;
var CallbacksHandler = require('../platform/callbacks-invoker').CallbacksHandler;

var REMOVE_PLACEHOLDER = CallbacksHandler.REMOVE_PLACEHOLDER;

// Extends CallbacksHandler to handle and invoke event callbacks.
function EventListeners () {
CallbacksHandler.call(this);
}
JS.extend(EventListeners, CallbacksHandler);

EventListeners.prototype.invoke = function (event, captureListeners) {
var key = event.type,
list = this._callbackTable[key],
i, endIndex,
callingFunc, target, hasTarget;

this._invoking[key] = true;

var key = event.type;
var list = this._callbackTable[key];
if (list) {
if (list.length === 1) {
callingFunc = list[0];
if (callingFunc !== REMOVE_PLACEHOLDER) {
callingFunc.call(event.currentTarget, event, captureListeners);
}
}
else {
endIndex = list.length - 1;
for (i = 0; i <= endIndex;) {
callingFunc = list[i];
var increment = 1;
// cheap detection for function
if (callingFunc !== REMOVE_PLACEHOLDER) {
target = list[i+1];
hasTarget = target && typeof target === 'object';
if (hasTarget) {
callingFunc.call(target, event, captureListeners);
increment = 2;
}
else {
callingFunc.call(event.currentTarget, event, captureListeners);
}
var rootInvoker = !list.isInvoking;
list.isInvoking = true;

if (event._propagationImmediateStopped || i + increment > endIndex) {
break;
}
var callbacks = list.callbacks;
var targets = list.targets;
for (var i = 0, len = callbacks.length; i < len; ++i) {
var callback = callbacks[i];
if (callback) {
var target = targets[i] || event.currentTarget;
callback.call(target, event, captureListeners);
if (event._propagationImmediateStopped) {
break;
}
}
}

i += increment;
if (rootInvoker) {
list.isInvoking = false;
if (list.containRemoved) {
list.purge();
}
}
}
this._invoking[key] = false;

// Delay removing
this._clearToRemove(key);
};

module.exports = EventListeners;
Expand Down
6 changes: 3 additions & 3 deletions cocos2d/core/event/event-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ proto.hasEventListener = function (type, checkCapture) {
* @param {Function} callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param {Event} callback.event event
* @param {Object} [target] - The target to invoke the callback, can be null
* @param {Object} [target] - The target (this object) to invoke the callback, can be null
* @param {Boolean} [useCapture=false] - When set to true, the capture argument prevents callback
* from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
* When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE.
Expand Down Expand Up @@ -213,7 +213,7 @@ proto.on = function (type, callback, target, useCapture) {
* @method off
* @param {String} type - A string representing the event type being removed.
* @param {Function} [callback] - The callback to remove.
* @param {Object} [target] - The target to invoke the callback, if it's not given, only callback without target will be removed
* @param {Object} [target] - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
* @param {Boolean} [useCapture=false] - Specifies whether the callback being removed was registered as a capturing callback or not.
* If not specified, useCapture defaults to false. If a callback was registered twice,
* one with capture and one without, each must be removed separately. Removal of a capturing callback
Expand Down Expand Up @@ -283,7 +283,7 @@ proto.targetOff = function (target) {
* @param {Function} callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param {Event} callback.event event
* @param {Object} [target] - The target to invoke the callback, can be null
* @param {Object} [target] - The target (this object) to invoke the callback, can be null
* @param {Boolean} [useCapture=false] - When set to true, the capture argument prevents callback
* from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
* When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE.
Expand Down
Loading

0 comments on commit e3212cd

Please sign in to comment.