diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js index 23ff6aa2e1..1579ce1825 100644 --- a/cocos2d/core/load-pipeline/CCLoader.js +++ b/cocos2d/core/load-pipeline/CCLoader.js @@ -196,7 +196,8 @@ JS.mixin(cc.loader, { // No new resources, complete directly if (totalCount === completedCount) { if (singleRes) { - var result = this._items.map[resources[0]]; + var src = resources[0].src || resources[0]; + var result = this._items.map[src]; completeCallback.call(this, result.error, result.content); } else { 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 a9c86a1e25..3440530703 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -164,24 +164,20 @@ var AssetLibrary = { return callInNextTick(callback, new Error('[AssetLibrary] uuid must be string'), null); } - if (CC_EDITOR) { - Loader.removeItem(uuid); - } - Loader.load(item, function (error, asset) { if (error || !asset) { error = new Error('[AssetLibrary] loading JSON or dependencies failed : ' + JSON.stringify(error)); } 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); @@ -207,12 +203,12 @@ var AssetLibrary = { skips: [ Loader.downloader.id ] }; Loader.load(item, function (error, asset) { - if (error || !asset) { + if (error) { error = new Error('[AssetLibrary] loading JSON or dependencies failed : ' + JSON.stringify(error)); } if (thisTick) { callInNextTick(function () { - if (CC_EDITOR || isScene(asset)) { + if (isScene(asset) || CC_EDITOR) { Loader.removeItem(randomUuid); } asset._uuid = ''; @@ -220,7 +216,7 @@ var AssetLibrary = { }); } else { - if (CC_EDITOR || isScene(asset)) { + if (isScene(asset) || CC_EDITOR) { Loader.removeItem(randomUuid); } asset._uuid = ''; diff --git a/cocos2d/core/platform/callbacks-invoker.js b/cocos2d/core/platform/callbacks-invoker.js index ae304688cc..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 @@ -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; + } }; /** diff --git a/cocos2d/core/textures/CCTextureCache.js b/cocos2d/core/textures/CCTextureCache.js index 1357fa87e4..a2b5789c05 100644 --- a/cocos2d/core/textures/CCTextureCache.js +++ b/cocos2d/core/textures/CCTextureCache.js @@ -319,7 +319,7 @@ game.once(game.EVENT_RENDERER_INITED, function () { tex = locTexs[url] = new Texture2D(); tex.url = url; cc.loader.load(url, function (err, texture) { - if (err || !texture) { + if (err) { return cb && cb.call(target, err || new Error('Unknown error')); } @@ -377,7 +377,7 @@ game.once(game.EVENT_RENDERER_INITED, function () { tex = locTexs[url] = new Texture2D(); tex.url = url; cc.loader.load(url, function (err, texture) { - if (err || !texture) { + if (err) { return cb && cb.call(target, err || new Error('Unknown error')); } diff --git a/cocos2d/particle/CCParticleSystem.js b/cocos2d/particle/CCParticleSystem.js index 465631e07f..472d798037 100644 --- a/cocos2d/particle/CCParticleSystem.js +++ b/cocos2d/particle/CCParticleSystem.js @@ -137,10 +137,10 @@ var properties = { this._file = value; if (value) { this._applyFile(); - cc.engine.repaintInEditMode(); - //if (CC_EDITOR) { - // self.preview = self.preview; - //} + if (CC_EDITOR) { + cc.engine.repaintInEditMode(); + //self.preview = self.preview; + } } else { this.custom = true; diff --git a/cocos2d/tilemap/CCTiledMap.js b/cocos2d/tilemap/CCTiledMap.js index 8d44fed52c..dcf40583d8 100644 --- a/cocos2d/tilemap/CCTiledMap.js +++ b/cocos2d/tilemap/CCTiledMap.js @@ -349,8 +349,8 @@ var TiledMap = cc.Class({ _preloadTmx: function(file, cb) { var self = this; - cc.loader.load(file, function (err, content) { - if (err || !content) { + cc.loader.load(file, function (err) { + if (err) { if (cb) cb(err || new Error('Preload TMX failed: unknown error')); return; } 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 () {