From fa2d62605d166b8c516a8577aea82c8ba5806b22 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 26 Feb 2016 11:42:35 +0800 Subject: [PATCH 1/3] Delay removing in callbacks handler --- cocos2d/core/platform/callbacks-invoker.js | 41 ++++++++++------------ 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/cocos2d/core/platform/callbacks-invoker.js b/cocos2d/core/platform/callbacks-invoker.js index ae304688cc..16d1ac26ba 100644 --- a/cocos2d/core/platform/callbacks-invoker.js +++ b/cocos2d/core/platform/callbacks-invoker.js @@ -120,6 +120,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) { + this._toRemove.push([key, callback, target]); + return true; + } + index = list.indexOf(callback); while (index !== -1) { callbackTarget = list[index+1]; @@ -148,6 +154,8 @@ CallbacksHandler.prototype.remove = function (key, callback, target) { */ var CallbacksInvoker = function () { CallbacksHandler.call(this); + this._invoking = false; + this._toRemove = []; }; JS.extend(CallbacksInvoker, CallbacksHandler); @@ -165,11 +173,11 @@ if (CC_TEST) { * @param {any} [p5] */ CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) { - var list = this._callbackTable[key]; + this._invoking = true; + 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 +191,16 @@ 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 = false; + + for (i = 0; i < this._toRemove.length; ++i) { + var toRemove = this._toRemove[i]; + this.remove(toRemove[0], toRemove[1], toRemove[2]); + } + this._toRemove.length = 0; }; /** From c478ae6b67484a1f8932df85bceebdcf448a91d2 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 26 Feb 2016 11:41:35 +0800 Subject: [PATCH 2/3] Fix AssetLibrary cache issue and uuid loader this issue --- cocos2d/core/load-pipeline/uuid-loader.js | 4 ++-- cocos2d/core/platform/CCAssetLibrary.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/load-pipeline/uuid-loader.js b/cocos2d/core/load-pipeline/uuid-loader.js index 9c7f1c15b7..09e3903a6b 100644 --- a/cocos2d/core/load-pipeline/uuid-loader.js +++ b/cocos2d/core/load-pipeline/uuid-loader.js @@ -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; }, { diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index 10ca3a0341..3440530703 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -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 = ''; From 21d0a264b57a26bcfc057c8301470af9eb2c0b33 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 26 Feb 2016 14:53:01 +0800 Subject: [PATCH 3/3] Improve removing callbacks logic and add test case --- cocos2d/core/platform/callbacks-invoker.js | 23 +++++++--- test/qunit/unit/test-callbacks-invoker.js | 52 ++++++++++++++++++++++ 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/platform/callbacks-invoker.js b/cocos2d/core/platform/callbacks-invoker.js index 16d1ac26ba..214adc972d 100644 --- a/cocos2d/core/platform/callbacks-invoker.js +++ b/cocos2d/core/platform/callbacks-invoker.js @@ -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 @@ -121,8 +126,8 @@ CallbacksHandler.prototype.remove = function (key, callback, target) { var list = this._callbackTable[key], index, callbackTarget; if (list) { // Delay removing - if (this._invoking) { - this._toRemove.push([key, callback, target]); + if (this._invoking === key) { + this._toRemove.push([callback, target]); return true; } @@ -154,8 +159,9 @@ CallbacksHandler.prototype.remove = function (key, callback, target) { */ var CallbacksInvoker = function () { CallbacksHandler.call(this); - this._invoking = false; + this._invoking = null; this._toRemove = []; + this._toRemoveAll = null; }; JS.extend(CallbacksInvoker, CallbacksHandler); @@ -173,7 +179,7 @@ if (CC_TEST) { * @param {any} [p5] */ CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) { - this._invoking = true; + this._invoking = key; var list = this._callbackTable[key], i; if (list) { var endIndex = list.length - 1; @@ -194,13 +200,18 @@ CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) { i += increment; } } - this._invoking = false; + this._invoking = null; + // Delay removing for (i = 0; i < this._toRemove.length; ++i) { var toRemove = this._toRemove[i]; - this.remove(toRemove[0], toRemove[1], toRemove[2]); + this.remove(this._invoking, toRemove[0], toRemove[1]); } this._toRemove.length = 0; + if (this._toRemoveAll) { + this.removeAll(this._toRemoveAll); + this._toRemoveAll = null; + } }; /** diff --git a/test/qunit/unit/test-callbacks-invoker.js b/test/qunit/unit/test-callbacks-invoker.js index e5679f0f67..9eb52a0bde 100644 --- a/test/qunit/unit/test-callbacks-invoker.js +++ b/test/qunit/unit/test-callbacks-invoker.js @@ -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 () {