From d52a9d43f439bbda539609760c911c72dd9f836b Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Tue, 11 Oct 2016 14:27:21 +0800 Subject: [PATCH 1/3] optimize operation for changing enabled --- cocos2d/core/components/CCComponent.js | 43 ++++++++++------------ cocos2d/core/event/event-target.js | 17 +++++++++ cocos2d/core/platform/callbacks-invoker.js | 30 ++++++--------- test/qunit/unit/_callback-tester.js | 2 +- 4 files changed, 49 insertions(+), 43 deletions(-) diff --git a/cocos2d/core/components/CCComponent.js b/cocos2d/core/components/CCComponent.js index f278c7ca24..7611ec7216 100644 --- a/cocos2d/core/components/CCComponent.js +++ b/cocos2d/core/components/CCComponent.js @@ -115,10 +115,10 @@ function _registerEvent (self, on) { if (self.start && !(self._objFlags & IsStartCalled)) { if (on) { - cc.director.on(cc.Director.EVENT_BEFORE_UPDATE, _callStart, self); + cc.director.__fastOn(cc.Director.EVENT_BEFORE_UPDATE, _callStart, self, self.__eventTargets); } else { - cc.director.off(cc.Director.EVENT_BEFORE_UPDATE, _callStart, self); + cc.director.__fastOff(cc.Director.EVENT_BEFORE_UPDATE, _callStart, self, self.__eventTargets); } } @@ -127,33 +127,33 @@ function _registerEvent (self, on) { } if (on) { - cc.director.on(cc.Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, self); + cc.director.__fastOn(cc.Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, self, self.__eventTargets); } else { - cc.director.off(cc.Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, self); - cc.director.off(cc.Director.EVENT_COMPONENT_UPDATE, _callUpdate, self); - cc.director.off(cc.Director.EVENT_COMPONENT_LATE_UPDATE, _callLateUpdate, self); + cc.director.__fastOff(cc.Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, self, self.__eventTargets); + cc.director.__fastOff(cc.Director.EVENT_COMPONENT_UPDATE, _callUpdate, self, self.__eventTargets); + cc.director.__fastOff(cc.Director.EVENT_COMPONENT_LATE_UPDATE, _callLateUpdate, self, self.__eventTargets); } } var _registerUpdateEvent = function () { - cc.director.off(cc.Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, this); + cc.director.__fastOff(cc.Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, this, this.__eventTargets); if (this.update) { - cc.director.on(cc.Director.EVENT_COMPONENT_UPDATE, _callUpdate, this); + cc.director.__fastOn(cc.Director.EVENT_COMPONENT_UPDATE, _callUpdate, this, this.__eventTargets); } if (this.lateUpdate) { - cc.director.on(cc.Director.EVENT_COMPONENT_LATE_UPDATE, _callLateUpdate, this); + cc.director.__fastOn(cc.Director.EVENT_COMPONENT_LATE_UPDATE, _callLateUpdate, this, this.__eventTargets); } }; var _callStart = CC_EDITOR ? function () { - cc.director.off(cc.Director.EVENT_BEFORE_UPDATE, _callStart, this); + cc.director.__fastOff(cc.Director.EVENT_BEFORE_UPDATE, _callStart, this, this.__eventTargets); callStartInTryCatch(this); this._objFlags |= IsStartCalled; } : function () { - cc.director.off(cc.Director.EVENT_BEFORE_UPDATE, _callStart, this); + cc.director.__fastOff(cc.Director.EVENT_BEFORE_UPDATE, _callStart, this, this.__eventTargets); this.start(); this._objFlags |= IsStartCalled; }; @@ -242,9 +242,18 @@ var Component = cc.Class({ } // Support for Scheduler this.__instanceId = cc.ClassManager.getNewInstanceId(); + + /** + * Register all related EventTargets, + * all event callbacks will be removed in _onPreDestroy + * @property {Array} __eventTargets + * @private + */ + this.__eventTargets = []; } : function () { // Support for Scheduler this.__instanceId = cc.ClassManager.getNewInstanceId(); + this.__eventTargets = []; }, properties: { @@ -394,18 +403,6 @@ var Component = cc.Class({ return this._objFlags & IsOnLoadCalled; } }, - - /** - * Register all related EventTargets, - * all event callbacks will be removed in _onPreDestroy - * @property __eventTargets - * @type {Array} - * @private - */ - __eventTargets: { - default: [], - serializable: false - } }, // LIFECYCLE METHODS diff --git a/cocos2d/core/event/event-target.js b/cocos2d/core/event/event-target.js index 265040f88d..a3eb9e4663 100644 --- a/cocos2d/core/event/event-target.js +++ b/cocos2d/core/event/event-target.js @@ -200,6 +200,15 @@ JS.mixin(EventTarget.prototype, { return callback; }, + __fastOn: function (type, callback, target, eventTargets) { + var listeners = this._bubblingListeners; + if (!listeners) { + listeners = this._bubblingListeners = new EventListeners(); + } + listeners.add(type, callback, target); + eventTargets.push(this); + }, + /** * !#en * Removes the callback previously registered with the same type, callback, target and or useCapture. @@ -244,6 +253,14 @@ JS.mixin(EventTarget.prototype, { } }, + __fastOff: function (type, callback, target, eventTargets) { + var listeners = this._bubblingListeners; + if (listeners) { + listeners.remove(type, callback, target); + eventTargets.splice(eventTargets.indexOf(this), 1); + } + }, + /** * !#en Removes all callbacks previously registered with the same target. * !#zh 删除指定目标上的所有注册回调。 diff --git a/cocos2d/core/platform/callbacks-invoker.js b/cocos2d/core/platform/callbacks-invoker.js index a325e3e7a8..b9cb6a8188 100644 --- a/cocos2d/core/platform/callbacks-invoker.js +++ b/cocos2d/core/platform/callbacks-invoker.js @@ -58,7 +58,7 @@ CallbacksHandler.prototype._clearToRemove = function (key) { i--; } } - delete this._toRemove[key]; + this._toRemove[key] = false; } if (this._toRemoveAll) { this.removeAll(this._toRemoveAll); @@ -76,10 +76,12 @@ CallbacksHandler.prototype._clearToRemove = function (key) { CallbacksHandler.prototype.add = function (key, callback, target) { var list = this._callbackTable[key]; if (typeof list !== 'undefined') { - list.push(callback); - // Just append the target after callback if (typeof target === 'object') { - list.push(target); + // append the target after callback + list.push(callback, target); + } + else { + list.push(callback); } return false; } @@ -108,28 +110,16 @@ CallbacksHandler.prototype.add = function (key, callback, target) { */ CallbacksHandler.prototype.has = function (key, callback, target) { var list = this._callbackTable[key], callbackTarget, index; - if (!list) { return false; } - - var empty = true; - for (var i = 0; i < list.length; ++i) { - if (list[i] && list[i] !== REMOVE_PLACEHOLDER) { - empty = false; - break; - } - } - if (empty) { - return false; - } - // callback not given, but key found if (!callback) return true; // wrong callback type, can't found anything else if (typeof callback !== 'function') return false; + // Search callback, target pair in the list index = list.indexOf(callback); while (index !== -1) { @@ -173,8 +163,10 @@ CallbacksHandler.prototype.removeAll = function (key) { } } } - else + else { delete this._callbackTable[key]; + delete this._toRemove[key]; + } }; /** @@ -307,7 +299,7 @@ CallbacksInvoker.prototype.invokeAndRemove = function (key, p1, p2, p3, p4, p5) } } this._invoking[key] = false; - delete this._toRemove[key]; + this._toRemove[key] = false; this.removeAll(key); }; diff --git a/test/qunit/unit/_callback-tester.js b/test/qunit/unit/_callback-tester.js index fbefd624ef..0976af93c7 100644 --- a/test/qunit/unit/_callback-tester.js +++ b/test/qunit/unit/_callback-tester.js @@ -82,7 +82,7 @@ CallbackTester.prototype.notExpect = function (notExpect, message) { */ CallbackTester.prototype.stopTest = function () { if (this._expects && this._expects.length > 0) { - var last = this._expects.splice(0, 1)[0]; + var last = this._expects.shift(); var expect = last.expect; var message = last.message; ok(false, 'The last expected "' + expect + '" not called yet: ' + message); From 0ed6bcb952a7988f03a0012cc66455c221de1e78 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Thu, 13 Oct 2016 12:02:10 +0800 Subject: [PATCH 2/3] optimize enabled by removing splice --- cocos2d/core/components/CCComponent.js | 66 +++-- cocos2d/core/event/event-listeners.js | 3 - cocos2d/core/event/event-target.js | 6 +- cocos2d/core/platform/CCObject.js | 5 +- cocos2d/core/platform/callbacks-invoker.js | 32 +-- cocos2d/core/platform/js.js | 254 ++++++++++-------- .../core/utils/mutable-forward-iterator.js | 4 +- 7 files changed, 205 insertions(+), 165 deletions(-) diff --git a/cocos2d/core/components/CCComponent.js b/cocos2d/core/components/CCComponent.js index 7611ec7216..0999c910bc 100644 --- a/cocos2d/core/components/CCComponent.js +++ b/cocos2d/core/components/CCComponent.js @@ -72,7 +72,7 @@ function callOnEnable (self, enable) { } cc.director.getScheduler().resumeTarget(self); - _registerEvent(self, true); + _registerEvent(self); self._objFlags |= IsOnEnableCalled; } @@ -88,7 +88,7 @@ function callOnEnable (self, enable) { } cc.director.getScheduler().pauseTarget(self); - _registerEvent(self, false); + _unregisterEvent(self); self._objFlags &= ~IsOnEnableCalled; } @@ -110,50 +110,58 @@ function callOnEnable (self, enable) { } } -function _registerEvent (self, on) { - if (CC_EDITOR && !(self.constructor._executeInEditMode || cc.engine._isPlaying)) return; +var Director = cc.Director; - if (self.start && !(self._objFlags & IsStartCalled)) { - if (on) { - cc.director.__fastOn(cc.Director.EVENT_BEFORE_UPDATE, _callStart, self, self.__eventTargets); - } - else { - cc.director.__fastOff(cc.Director.EVENT_BEFORE_UPDATE, _callStart, self, self.__eventTargets); - } - } - - if (!self.update && !self.lateUpdate) { +function _registerEvent (self) { + if (CC_EDITOR && !(self.constructor._executeInEditMode || cc.engine._isPlaying)) { return; } - - if (on) { - cc.director.__fastOn(cc.Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, self, self.__eventTargets); + if (self.start && !(self._objFlags & IsStartCalled)) { + cc.director.__fastOn(Director.EVENT_BEFORE_UPDATE, _callStart, self, self.__eventTargets); } - else { - cc.director.__fastOff(cc.Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, self, self.__eventTargets); - cc.director.__fastOff(cc.Director.EVENT_COMPONENT_UPDATE, _callUpdate, self, self.__eventTargets); - cc.director.__fastOff(cc.Director.EVENT_COMPONENT_LATE_UPDATE, _callLateUpdate, self, self.__eventTargets); + if (self.update || self.lateUpdate) { + cc.director.__fastOn(Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, self, self.__eventTargets); } } -var _registerUpdateEvent = function () { - cc.director.__fastOff(cc.Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, this, this.__eventTargets); +function _unregisterEvent (self) { + if (CC_EDITOR && !(self.constructor._executeInEditMode || cc.engine._isPlaying)) { + return; + } + if (self.start && !(self._objFlags & IsStartCalled)) { + cc.director.__fastOff(Director.EVENT_BEFORE_UPDATE, _callStart, self, self.__eventTargets); + } + var hasUpdate = self.update; + var hasLateUpdate = self.lateUpdate; + if (hasUpdate || hasLateUpdate) { + cc.director.__fastOff(Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, self, self.__eventTargets); + if (hasUpdate) { + cc.director.__fastOff(Director.EVENT_COMPONENT_UPDATE, _callUpdate, self, self.__eventTargets); + } + if (hasLateUpdate) { + cc.director.__fastOff(Director.EVENT_COMPONENT_LATE_UPDATE, _callLateUpdate, self, self.__eventTargets); + } + } +} +function _registerUpdateEvent () { + var eventTargets = this.__eventTargets; + var director = cc.director; + director.__fastOff(Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, this, eventTargets); if (this.update) { - cc.director.__fastOn(cc.Director.EVENT_COMPONENT_UPDATE, _callUpdate, this, this.__eventTargets); + director.__fastOn(Director.EVENT_COMPONENT_UPDATE, _callUpdate, this, eventTargets); } - if (this.lateUpdate) { - cc.director.__fastOn(cc.Director.EVENT_COMPONENT_LATE_UPDATE, _callLateUpdate, this, this.__eventTargets); + director.__fastOn(Director.EVENT_COMPONENT_LATE_UPDATE, _callLateUpdate, this, eventTargets); } -}; +} var _callStart = CC_EDITOR ? function () { - cc.director.__fastOff(cc.Director.EVENT_BEFORE_UPDATE, _callStart, this, this.__eventTargets); + cc.director.__fastOff(Director.EVENT_BEFORE_UPDATE, _callStart, this, this.__eventTargets); callStartInTryCatch(this); this._objFlags |= IsStartCalled; } : function () { - cc.director.__fastOff(cc.Director.EVENT_BEFORE_UPDATE, _callStart, this, this.__eventTargets); + cc.director.__fastOff(Director.EVENT_BEFORE_UPDATE, _callStart, this, this.__eventTargets); this.start(); this._objFlags |= IsStartCalled; }; diff --git a/cocos2d/core/event/event-listeners.js b/cocos2d/core/event/event-listeners.js index b8ca24c1fe..2bc7acdf8b 100644 --- a/cocos2d/core/event/event-listeners.js +++ b/cocos2d/core/event/event-listeners.js @@ -31,9 +31,6 @@ var REMOVE_PLACEHOLDER = CallbacksHandler.REMOVE_PLACEHOLDER; // Extends CallbacksHandler to handle and invoke event callbacks. function EventListeners () { CallbacksHandler.call(this); - this._invoking = {}; - this._toRemove = {}; - this._toRemoveAll = null; } JS.extend(EventListeners, CallbacksHandler); diff --git a/cocos2d/core/event/event-target.js b/cocos2d/core/event/event-target.js index a3eb9e4663..5e357992bc 100644 --- a/cocos2d/core/event/event-target.js +++ b/cocos2d/core/event/event-target.js @@ -25,6 +25,7 @@ var EventListeners = require('./event-listeners'); require('./event'); var JS = cc.js; +var fastRemove = JS.array.fastRemove; var cachedArray = new Array(16); cachedArray.length = 0; @@ -247,8 +248,7 @@ JS.mixin(EventTarget.prototype, { listeners.remove(type, callback, target); if (target && target.__eventTargets) { - var index = target.__eventTargets.indexOf(this); - target.__eventTargets.splice(index, 1); + fastRemove(target.__eventTargets, this); } } }, @@ -257,7 +257,7 @@ JS.mixin(EventTarget.prototype, { var listeners = this._bubblingListeners; if (listeners) { listeners.remove(type, callback, target); - eventTargets.splice(eventTargets.indexOf(this), 1); + fastRemove(eventTargets, this); } }, diff --git a/cocos2d/core/platform/CCObject.js b/cocos2d/core/platform/CCObject.js index 7abf85d342..f77a8065ea 100644 --- a/cocos2d/core/platform/CCObject.js +++ b/cocos2d/core/platform/CCObject.js @@ -401,10 +401,7 @@ if (CC_EDITOR || CC_TEST) { Object.defineProperty(CCObject, '_cancelDestroy', { value: function (obj) { obj._objFlags &= ~ToDestroy; - var index = objectsToDestroy.indexOf(obj); - if (index !== -1) { - objectsToDestroy.splice(index, 1); - } + JS.array.fastRemove(objectsToDestroy, obj); } }); } diff --git a/cocos2d/core/platform/callbacks-invoker.js b/cocos2d/core/platform/callbacks-invoker.js index b9cb6a8188..843cb46314 100644 --- a/cocos2d/core/platform/callbacks-invoker.js +++ b/cocos2d/core/platform/callbacks-invoker.js @@ -34,30 +34,29 @@ var JS = require('./js'); */ var CallbacksHandler = (function () { this._callbackTable = {}; + this._invoking = {}; + this._toRemove = {}; + this._toRemoveAll = null; }); // Avoid to equal to user set target (null for example) -var REMOVE_PLACEHOLDER = Number.POSITIVE_INFINITY; +var REMOVE_PLACEHOLDER = {}; CallbacksHandler.REMOVE_PLACEHOLDER = REMOVE_PLACEHOLDER; CallbacksHandler.prototype._clearToRemove = function (key) { var list = this._callbackTable[key]; - if (this._toRemove[key] && list) { - var count, i; - for (i = list.length-1; i >= 0;) { - count = 0; - while (list[i] === REMOVE_PLACEHOLDER) { - i--; - count++; - } - if (count > 0) { - list.splice(i+1, count); - } - else { - i--; + // filter all REMOVE_PLACEHOLDER and compact array + var firstRemovedIndex = list.indexOf(REMOVE_PLACEHOLDER); + var nextIndex = firstRemovedIndex; + for (var i = firstRemovedIndex + 1; i < list.length; ++i) { + var item = list[i]; + if (item !== REMOVE_PLACEHOLDER) { + list[nextIndex] = item; + ++nextIndex; } } + list.length = nextIndex; this._toRemove[key] = false; } if (this._toRemoveAll) { @@ -187,7 +186,7 @@ CallbacksHandler.prototype.remove = function (key, callback, target) { } if (callbackTarget === target) { // Delay removing - if (this._invoking[key]) { + if (CC_JSB || this._invoking[key]) { list[index] = REMOVE_PLACEHOLDER; callbackTarget && (list[index+1] = REMOVE_PLACEHOLDER); this._toRemove[key] = true; @@ -216,9 +215,6 @@ CallbacksHandler.prototype.remove = function (key, callback, target) { */ var CallbacksInvoker = function () { CallbacksHandler.call(this); - this._invoking = {}; - this._toRemove = {}; - this._toRemoveAll = null; }; JS.extend(CallbacksInvoker, CallbacksHandler); diff --git a/cocos2d/core/platform/js.js b/cocos2d/core/platform/js.js index 8168722f7d..793a610801 100644 --- a/cocos2d/core/platform/js.js +++ b/cocos2d/core/platform/js.js @@ -546,120 +546,164 @@ js.formatStr = function () { * @class array * @static */ -js.array = { - /** - * Removes the first occurrence of a specific object from the array. - * @method remove - * @param {any[]} array - * @param {any} value - * @return {Boolean} - */ - remove: function (array, value) { - var index = array.indexOf(value); - if (index !== -1) { - array.splice(index, 1); - return true; - } - else { - return false; - } - }, - /** - * Removes the array item at the specified index. - * @method removeAt - * @param {any[]} array - * @param {Number} index - */ - removeAt: function (array, index) { - array.splice(index, 1); - }, +/** + * Removes the array item at the specified index. + * @method removeAt + * @param {any[]} array + * @param {Number} index + */ +function removeAt (array, index) { + array.splice(index, 1); +} - /** - * Determines whether the array contains a specific value. - * @method contains - * @param {any[]} array - * @param {any} value - * @return {Boolean} - */ - contains: function (array, value) { - return array.indexOf(value) !== -1; - }, +/** + * Removes the array item at the specified index. + * It's faster but the order of the array will be changed. + * @method fastRemoveAt + * @param {any[]} array + * @param {Number} index + */ +function fastRemoveAt (array, index) { + var length = array.length; + if (index < 0 || index >= length) { + return; + } + array[index] = array[length - 1]; + array.length = length - 1; +} - /** - * Verify array's Type - * @method verifyType - * @param {array} array - * @param {Function} type - * @return {Boolean} - */ - verifyType: function (array, type) { - if (array && array.length > 0) { - for (var i = 0; i < array.length; i++) { - if (!(array[i] instanceof type)) { - cc.log(cc._LogInfos.Array.verifyType); - return false; - } +/** + * Removes the first occurrence of a specific object from the array. + * @method remove + * @param {any[]} array + * @param {any} value + * @return {Boolean} + */ +function remove (array, value) { + var index = array.indexOf(value); + if (index !== -1) { + removeAt(array, index); + return true; + } + else { + return false; + } +} + +/** + * Removes the first occurrence of a specific object from the array. + * It's faster but the order of the array will be changed. + * @method fastRemove + * @param {any[]} array + * @param {Number} value + */ +function fastRemove (array, value) { + var index = array.indexOf(value); + if (index !== -1) { + array[index] = array[array.length - 1]; + --array.length; + } +} + +/** + * Verify array's Type + * @method verifyType + * @param {array} array + * @param {Function} type + * @return {Boolean} + */ +function verifyType (array, type) { + if (array && array.length > 0) { + for (var i = 0; i < array.length; i++) { + if (!(array[i] instanceof type)) { + cc.log(cc._LogInfos.Array.verifyType); + return false; } } - return true; - }, + } + return true; +} - /** - * Removes from array all values in minusArr. For each Value in minusArr, the first matching instance in array will be removed. - * @method removeArray - * @param {Array} array Source Array - * @param {Array} minusArr minus Array - */ - removeArray: function (array, minusArr) { - for (var i = 0, l = minusArr.length; i < l; i++) { - remove(array, minusArr[i]); - } - }, +/** + * Removes from array all values in minusArr. For each Value in minusArr, the first matching instance in array will be removed. + * @method removeArray + * @param {Array} array Source Array + * @param {Array} minusArr minus Array + */ +function removeArray (array, minusArr) { + for (var i = 0, l = minusArr.length; i < l; i++) { + remove(array, minusArr[i]); + } +} - /** - * Inserts some objects at index - * @method appendObjectsAt - * @param {Array} array - * @param {Array} addObjs - * @param {Number} index - * @return {Array} - */ - appendObjectsAt: function(array, addObjs, index) { - array.splice.apply(array, [index, 0].concat(addObjs)); - return array; - }, +/** + * Inserts some objects at index + * @method appendObjectsAt + * @param {Array} array + * @param {Array} addObjs + * @param {Number} index + * @return {Array} + */ +function appendObjectsAt (array, addObjs, index) { + array.splice.apply(array, [index, 0].concat(addObjs)); + return array; +} - /** - * Copy an array's item to a new array (its performance is better than Array.slice) - * @method copy - * @param {Array} array - * @return {Array} - */ - copy: function(array) { - var i, len = array.length, arr_clone = new Array(len); - for (i = 0; i < len; i += 1) - arr_clone[i] = array[i]; - return arr_clone; - }, +/** + * Exact same function as Array.prototype.indexOf. + * HACK: ugliy hack for Baidu mobile browser compatibility, + * stupid Baidu guys modify Array.prototype.indexOf for all pages loaded, + * their version changes strict comparison to non-strict comparison, + * it also ignores the second parameter of the original API, + * and this will cause event handler enter infinite loop. + * Baidu developers, if you ever see this documentation, + * here is the standard: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf + * Seriously ! + * + * @method indexOf + * @param {any} searchElement Element to locate in the array. + * @param {Number} [fromIndex=0] The index to start the search at + * @return {Number} returns the first index at which a given element can be found in the array, or -1 if it is not present. + */ +var indexOf = Array.prototype.indexOf; - /** - * Exact same function as Array.prototype.indexOf. - * HACK: ugliy hack for Baidu mobile browser compatibility, - * stupid Baidu guys modify Array.prototype.indexOf for all pages loaded, - * their version changes strict comparison to non-strict comparison, - * it also ignores the second parameter of the original API, - * and this will cause event handler enter infinite loop. - * Baidu developers, if you ever see this documentation, - * here is the standard: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf - * Seriously ! - * - * @method indexOf - * @param {any} searchElement Element to locate in the array. - * @param {Number} [fromIndex=0] The index to start the search at - * @return {Number} returns the first index at which a given element can be found in the array, or -1 if it is not present. - */ - indexOf: Array.prototype.indexOf, +/** + * Determines whether the array contains a specific value. + * @method contains + * @param {any[]} array + * @param {any} value + * @return {Boolean} + */ +function contains (array, value) { + return indexOf.call(array, value) !== -1; +} + +/** + * Copy an array's item to a new array (its performance is better than Array.slice) + * @method copy + * @param {Array} array + * @return {Array} + */ +function copy (array) { + var i, len = array.length, arr_clone = new Array(len); + for (i = 0; i < len; i += 1) + arr_clone[i] = array[i]; + return arr_clone; +} + +js.array = { + remove: remove, + fastRemove: fastRemove, + removeAt: removeAt, + fastRemoveAt: fastRemoveAt, + contains: contains, + verifyType: verifyType, + removeArray: removeArray, + appendObjectsAt: appendObjectsAt, + copy: copy, + indexOf: indexOf, + MutableForwardIterator: require('../utils/mutable-forward-iterator') }; cc.js = js; diff --git a/cocos2d/core/utils/mutable-forward-iterator.js b/cocos2d/core/utils/mutable-forward-iterator.js index 68f8f531a7..475a313b08 100644 --- a/cocos2d/core/utils/mutable-forward-iterator.js +++ b/cocos2d/core/utils/mutable-forward-iterator.js @@ -23,8 +23,6 @@ THE SOFTWARE. ****************************************************************************/ -var js = require('../platform/js'); - /** * @example * var array = [0, 1, 2, 3, 4]; @@ -64,4 +62,4 @@ proto.push = function (item) { // } //); -js.array.MutableForwardIterator = module.exports = MutableForwardIterator; +module.exports = MutableForwardIterator; From e5c7775903f557c2b79165f67ed8046e804e8e66 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 19 Oct 2016 21:34:00 +0800 Subject: [PATCH 3/3] fix CallbacksHandle.has --- cocos2d/core/platform/callbacks-invoker.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/platform/callbacks-invoker.js b/cocos2d/core/platform/callbacks-invoker.js index 843cb46314..b6790eed77 100644 --- a/cocos2d/core/platform/callbacks-invoker.js +++ b/cocos2d/core/platform/callbacks-invoker.js @@ -108,13 +108,27 @@ CallbacksHandler.prototype.add = function (key, callback, target) { * @return {Boolean} */ CallbacksHandler.prototype.has = function (key, callback, target) { + if (this._toRemoveAll === key) { + return false; + } var list = this._callbackTable[key], callbackTarget, index; if (!list) { return false; } // callback not given, but key found - if (!callback) - return true; + if (!callback) { + if (this._toRemove[key]) { + for (index = 0; index < list.length; index++) { + if (list[index] !== REMOVE_PLACEHOLDER) { + return true; + } + } + return false; + } + else { + return list.length > 0; + } + } // wrong callback type, can't found anything else if (typeof callback !== 'function') return false;