Merge pull request #563 from jareguo/v1.0

update callbacks invoker
This commit is contained in:
pandamicro
2016-04-04 11:11:19 +08:00
4 changed files with 99 additions and 51 deletions
+3
View File
@@ -89,3 +89,6 @@ EventListeners.prototype.invoke = function (event) {
};
module.exports = EventListeners;
if (CC_TEST) {
cc._Test.EventListeners = EventListeners;
}
+19 -17
View File
@@ -39,28 +39,28 @@ var CallbacksHandler = (function () {
/**
* @method add
* @param {String} key
* @param {Function} callback - can be null
* @param {Object} target - can be null
* @param {Function} callback
* @param {Object} [target] - can be null
* @return {Boolean} whether the key is new
*/
CallbacksHandler.prototype.add = function (key, callback, target) {
var list = this._callbackTable[key];
if (typeof list !== 'undefined') {
if (typeof callback === 'function') {
list.push(callback);
// Just append the target after callback
if (typeof target === 'object') {
list.push(target);
}
list.push(callback);
// Just append the target after callback
if (typeof target === 'object') {
list.push(target);
}
return false;
}
else {
// new key
list = (typeof callback === 'function') ? [callback] : [];
// Just append the target after callback
if (list && typeof target === 'object') {
list.push(target);
if (typeof target === 'object') {
// Just append the target after callback
list = [callback, target];
}
else {
list = [callback];
}
this._callbackTable[key] = list;
return true;
@@ -142,14 +142,17 @@ CallbacksHandler.prototype.removeAll = function (key) {
* @return {Boolean} removed
*/
CallbacksHandler.prototype.remove = function (key, callback, target) {
var list = this._callbackTable[key], index, callbackTarget;
var list = this._callbackTable[key], index, callbackTarget, removeList;
if (list) {
// Delay removing
if (this._invoking[key]) {
if (!this._toRemove[key]) {
this._toRemove[key] = [];
removeList = this._toRemove[key];
if (removeList) {
removeList.push([callback, target]);
}
else {
this._toRemove[key] = [[callback, target]];
}
this._toRemove[key].push([callback, target]);
return true;
}
@@ -201,7 +204,6 @@ if (CC_TEST) {
* @param {any} [p5]
*/
CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) {
this._toRemove.length = 0;
this._invoking[key] = true;
var list = this._callbackTable[key], i;
if (list) {
+33 -34
View File
@@ -59,42 +59,41 @@ if (isPhantomJS) {
}
};
// polyfill
var video = document.createElement("video");
var video = document.createElement('video');
video.constructor.prototype.canPlayType = function () { return ''; };
(function () {
var log = console.log;
console.log = function () {
var str = arguments[0];
str = '' + str;
if (str && str.length > 110) {
log.call(console, "(Can not output this too long log)");
}
else {
log.apply(console, arguments);
}
}
var warn = console.warn;
console.warn = function () {
var str = arguments[0];
str = '' + str;
if (str && str.length > 110) {
warn.call(console, "(Can not output this too long warn)");
}
else {
warn.apply(console, arguments);
}
}
var error = console.error;
console.error = function () {
var str = arguments[0];
str = '' + str;
if (str && str.length > 110) {
error.call(console, "(Can not output this too long error)");
}
else {
error.apply(console, arguments);
}
}
var MAX_SINGLE_LEN = 110;
var MAX_TOTAL_LEN = 1000;
['log', 'warn', 'error'].forEach(function (method) {
var originalMethod = console[method];
console[method] = function () {
var str = '' + arguments[0];
if ( !str || str.length <= MAX_SINGLE_LEN ) {
originalMethod.apply(console, arguments);
return;
}
var tooLong = false;
if (str.length > MAX_TOTAL_LEN) {
tooLong = true;
str = str.slice(0, MAX_TOTAL_LEN);
}
var lines = str.split('\n');
for (var l = 0; l < lines.length; l++) {
var line = lines[l];
if (line.length > MAX_SINGLE_LEN) {
for (var i = 0; i < line.length; i += MAX_SINGLE_LEN) {
originalMethod.call(console, line.substr(i, MAX_SINGLE_LEN));
}
}
else {
originalMethod.call(console, line);
}
}
if (tooLong) {
originalMethod.call(console, '...Can not output this too long ' + method);
}
};
});
})();
}
+44
View File
@@ -189,6 +189,50 @@ test('remove all callbacks during invoking', function () {
strictEqual(ci.has('eve'), false, 'All callbacks should be removed');
});
test('remove and add again during invoking', function () {
var target = new Object();
var ci = new cc._Test.EventListeners();
var cb1 = function () {
ci.remove('eve', cb1, target);
ci.add('eve', cb1, target);
};
ci.add('eve', cb1, target);
ci.invoke(new cc.Event.EventCustom('eve'));
strictEqual(ci.has('eve', cb1, target), true, 'first callback should be added back');
});
//test('remove twise and add again during invoking', function () {
// var target = new Object();
// var ci = new cc._Test.EventListeners();
//
// var cb1 = function () {
// ci.remove('eve', cb1, target);
// ci.remove('eve', cb1, target);
// ci.add('eve', cb1, target);
// };
//
// ci.add('eve', cb1, target);
// ci.invoke(new cc.Event.EventCustom('eve'));
//
// strictEqual(ci.has('eve', cb1, target), true, 'first callback should be added back');
//});
//
//test('remove and check has during invoking', function () {
// var target = new Object();
// var ci = new cc._Test.EventListeners();
//
// var cb1 = function () {
// ci.remove('eve', cb1, target);
// strictEqual(ci.has('eve', cb1, target), false, 'first callback should be removed');
// };
//
// ci.add('eve', cb1, target);
// ci.invoke(new cc.Event.EventCustom('eve'));
//});
test('CallbacksInvoker support target', function () {
var ci = new cc._Test.CallbacksInvoker();
var cb1 = function () {