Merge pull request #1240 from jareguo/temp

CherryPick from 1.3
This commit is contained in:
Jare Guo
2016-10-25 17:40:38 +08:00
committed by GitHub
8 changed files with 257 additions and 197 deletions
+46 -41
View File
@@ -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.on(cc.Director.EVENT_BEFORE_UPDATE, _callStart, self);
}
else {
cc.director.off(cc.Director.EVENT_BEFORE_UPDATE, _callStart, self);
}
}
if (!self.update && !self.lateUpdate) {
function _registerEvent (self) {
if (CC_EDITOR && !(self.constructor._executeInEditMode || cc.engine._isPlaying)) {
return;
}
if (on) {
cc.director.on(cc.Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, self);
if (self.start && !(self._objFlags & IsStartCalled)) {
cc.director.__fastOn(Director.EVENT_BEFORE_UPDATE, _callStart, 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);
if (self.update || self.lateUpdate) {
cc.director.__fastOn(Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, self, self.__eventTargets);
}
}
var _registerUpdateEvent = function () {
cc.director.off(cc.Director.EVENT_BEFORE_UPDATE, _registerUpdateEvent, this);
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.on(cc.Director.EVENT_COMPONENT_UPDATE, _callUpdate, this);
director.__fastOn(Director.EVENT_COMPONENT_UPDATE, _callUpdate, this, eventTargets);
}
if (this.lateUpdate) {
cc.director.on(cc.Director.EVENT_COMPONENT_LATE_UPDATE, _callLateUpdate, this);
director.__fastOn(Director.EVENT_COMPONENT_LATE_UPDATE, _callLateUpdate, this, eventTargets);
}
};
}
var _callStart = CC_EDITOR ? function () {
cc.director.off(cc.Director.EVENT_BEFORE_UPDATE, _callStart, this);
cc.director.__fastOff(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(Director.EVENT_BEFORE_UPDATE, _callStart, this, this.__eventTargets);
this.start();
this._objFlags |= IsStartCalled;
};
@@ -242,9 +250,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 +411,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
-3
View File
@@ -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);
+19 -2
View File
@@ -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;
@@ -200,6 +201,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.
@@ -238,12 +248,19 @@ 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);
}
}
},
__fastOff: function (type, callback, target, eventTargets) {
var listeners = this._bubblingListeners;
if (listeners) {
listeners.remove(type, callback, target);
fastRemove(eventTargets, this);
}
},
/**
* !#en Removes all callbacks previously registered with the same target.
* !#zh 删除指定目标上的所有注册回调。
+1 -4
View File
@@ -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);
}
});
}
+40 -38
View File
@@ -34,31 +34,30 @@ 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;
}
}
delete this._toRemove[key];
list.length = nextIndex;
this._toRemove[key] = false;
}
if (this._toRemoveAll) {
this.removeAll(this._toRemoveAll);
@@ -76,10 +75,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;
}
@@ -107,29 +108,31 @@ 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;
}
var empty = true;
for (var i = 0; i < list.length; ++i) {
if (list[i] && list[i] !== REMOVE_PLACEHOLDER) {
empty = false;
break;
// callback not given, but key found
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;
}
}
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 +176,10 @@ CallbacksHandler.prototype.removeAll = function (key) {
}
}
}
else
else {
delete this._callbackTable[key];
delete this._toRemove[key];
}
};
/**
@@ -195,7 +200,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;
@@ -224,9 +229,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);
@@ -307,7 +309,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);
};
+149 -105
View File
@@ -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;
@@ -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;
+1 -1
View File
@@ -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);