Merge pull request #386 from pandamicro/master

Callbacks handler 在 invoke 过程中 delay 删除 callbacks
This commit is contained in:
Jare Guo
2016-02-26 15:00:31 +08:00
4 changed files with 87 additions and 29 deletions
+2 -2
View File
@@ -94,7 +94,7 @@ function loadUuid (item, callback) {
}
var pipeline = this.pipeline;
if (depends.length > 0) {
this.pipeline.flowInDeps(depends, function (items) {
pipeline.flowInDeps(depends, function (items) {
var item;
for (var src in items) {
item = items[src];
@@ -113,7 +113,7 @@ function loadUuid (item, callback) {
obj[dependProp] = value;
}
else {
this.getItems().add(dependSrc, function (item) {
pipeline.getItems().add(dependSrc, function (item) {
var value = item.isRawAsset ? (item.url || item.src) : item.content;
this.obj[this.prop] = value;
}, {
+4 -4
View File
@@ -170,14 +170,14 @@ var AssetLibrary = {
}
if (thisTick) {
callInNextTick(function () {
if (isScene(asset)) {
if (isScene(asset) || CC_EDITOR) {
Loader.removeItem(uuid);
}
callback(error, asset);
});
}
else {
if (isScene(asset)) {
if (isScene(asset) || CC_EDITOR) {
Loader.removeItem(uuid);
}
callback(error, asset);
@@ -208,7 +208,7 @@ var AssetLibrary = {
}
if (thisTick) {
callInNextTick(function () {
if (isScene(asset)) {
if (isScene(asset) || CC_EDITOR) {
Loader.removeItem(randomUuid);
}
asset._uuid = '';
@@ -216,7 +216,7 @@ var AssetLibrary = {
});
}
else {
if (isScene(asset)) {
if (isScene(asset) || CC_EDITOR) {
Loader.removeItem(randomUuid);
}
asset._uuid = '';
+29 -23
View File
@@ -90,6 +90,11 @@ CallbacksHandler.prototype.has = function (key, callback, target) {
* @param {String|Object} key - The event key to be removed or the target to be removed
*/
CallbacksHandler.prototype.removeAll = function (key) {
// Delay removing
if (this._invoking) {
this._toRemoveAll = key;
return;
}
if (typeof key === 'object') {
var target = key, list, index, callback;
// loop for all event types
@@ -120,6 +125,12 @@ CallbacksHandler.prototype.removeAll = function (key) {
CallbacksHandler.prototype.remove = function (key, callback, target) {
var list = this._callbackTable[key], index, callbackTarget;
if (list) {
// Delay removing
if (this._invoking === key) {
this._toRemove.push([callback, target]);
return true;
}
index = list.indexOf(callback);
while (index !== -1) {
callbackTarget = list[index+1];
@@ -148,6 +159,9 @@ CallbacksHandler.prototype.remove = function (key, callback, target) {
*/
var CallbacksInvoker = function () {
CallbacksHandler.call(this);
this._invoking = null;
this._toRemove = [];
this._toRemoveAll = null;
};
JS.extend(CallbacksInvoker, CallbacksHandler);
@@ -165,11 +179,11 @@ if (CC_TEST) {
* @param {any} [p5]
*/
CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) {
var list = this._callbackTable[key];
this._invoking = key;
var list = this._callbackTable[key], i;
if (list) {
var endIndex = list.length - 1;
var lastItem = list[endIndex];
for (var i = 0; i <= endIndex;) {
for (i = 0; i <= endIndex;) {
var callingFunc = list[i];
var target = list[i + 1];
var hasTarget = target && typeof target === 'object';
@@ -183,29 +197,21 @@ CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) {
increment = 1;
}
// TODO: check in remove
// check last one to see if any one removed
if (list[endIndex] !== lastItem && i + increment <= endIndex) { // 如果变短
if (list[endIndex - 1] === lastItem) {
// 只支持删一个
endIndex -= 1;
}
else if (list[endIndex - 2] === lastItem) {
// 只支持删一个 + target
endIndex -= 2;
}
else {
// 只允许在一个回调里面移除一个回调
return cc.error('Can remove only a callback at a time.');
}
if (list[i] !== callingFunc) { // 如果删了前面的回调,索引不加
continue;
}
}
i += increment;
}
}
this._invoking = null;
// Delay removing
for (i = 0; i < this._toRemove.length; ++i) {
var toRemove = this._toRemove[i];
this.remove(this._invoking, toRemove[0], toRemove[1]);
}
this._toRemove.length = 0;
if (this._toRemoveAll) {
this.removeAll(this._toRemoveAll);
this._toRemoveAll = null;
}
};
/**
+52
View File
@@ -137,6 +137,58 @@ test('remove last with target during invoking', function () {
strictEqual(ci.has('eve', cb2, target), false, 'self callback should be removed');
});
test('remove multiple callbacks during invoking', function () {
var ci = new cc._Test.CallbacksInvoker();
var cb1 = new Callback().enable();
var cb2 = new Callback(function () {
ci.remove('eve', cb1);
ci.remove('eve', cb3);
ci.remove('eve', cb3, target);
}).enable();
var cb3 = new Callback(function () {
ci.remove('eve', cb2, target);
}).enable();
var target = {};
ci.add('eve', cb1);
ci.add('eve', cb1, target);
ci.add('eve', cb2, target);
ci.add('eve', cb3);
ci.add('eve', cb3, target);
ci.invoke('eve');
cb1.expect(2, 'first callback should be invoked twice');
cb2.expect(1, 'second callback should be invoked once');
cb3.expect(2, 'third callback should be invoked twice');
strictEqual(ci.has('eve', cb1, target), true, 'first callback with target should not be removed');
strictEqual(ci.has('eve', cb1), false, 'first callback should be removed');
strictEqual(ci.has('eve', cb2, target), false, 'second callback with target should be removed');
strictEqual(ci.has('eve', cb3), false, 'third callback should be removed');
strictEqual(ci.has('eve', cb3, target), false, 'third callback with target should be removed');
});
test('remove all callbacks during invoking', function () {
var ci = new cc._Test.CallbacksInvoker();
var cb1 = new Callback().enable();
var cb2 = new Callback(function () {
ci.removeAll('eve');
}).enable();
var cb3 = new Callback(function () {
ci.remove('eve', cb2, target);
}).enable();
var target = {};
ci.add('eve', cb1);
ci.add('eve', cb1, target);
ci.add('eve', cb2, target);
ci.add('eve', cb3);
ci.add('eve', cb3, target);
ci.invoke('eve');
cb1.expect(2, 'first callback should be invoked twice');
cb2.expect(1, 'second callback should be invoked once');
cb3.expect(2, 'third callback should be invoked twice');
strictEqual(ci.has('eve'), false, 'All callbacks should be removed');
});
test('CallbacksInvoker support target', function () {
var ci = new cc._Test.CallbacksInvoker();
var cb1 = function () {