From 535ed9406d7a2f24084c4b3683e36e9e62c8d1f0 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 29 Feb 2016 14:47:58 +0800 Subject: [PATCH] Remove callbacks after flowout an item to avoid duplicated callback calls --- cocos2d/core/CCGame.js | 3 ++- cocos2d/core/load-pipeline/CCLoader.js | 2 ++ cocos2d/core/load-pipeline/loading-items.js | 3 ++- cocos2d/core/load-pipeline/pipeline.js | 11 ++++++++--- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cocos2d/core/CCGame.js b/cocos2d/core/CCGame.js index d133ad5e43..d995acefcb 100644 --- a/cocos2d/core/CCGame.js +++ b/cocos2d/core/CCGame.js @@ -308,7 +308,8 @@ var game = /** @lends cc.game# */{ // @ Persist root node section /** - * Add a persistent root node to the game, the persistent node won't be destroyed during scene transition + * Add a persistent root node to the game, the persistent node won't be destroyed during scene transition. + * The target node must be placed in the root level of hierarchy, otherwise this API won't have any effect. * @method addPersistRootNode * @param {Node} node - The node to be made persistent */ diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js index f295c760ee..045c8f3d7e 100644 --- a/cocos2d/core/load-pipeline/CCLoader.js +++ b/cocos2d/core/load-pipeline/CCLoader.js @@ -33,6 +33,7 @@ var loader = new Loader(); /** * Loader for resource loading process. It's a singleton object. * @class loader + * @extends Pipeline * @static */ cc.loader = new Pipeline([ @@ -146,6 +147,7 @@ JS.mixin(cc.loader, { function loadedCheck (item) { checker[item.src] = item; + self._items.remove(item.src, loadedCheck); if (item.error) { error = error || []; error.push(item.src); diff --git a/cocos2d/core/load-pipeline/loading-items.js b/cocos2d/core/load-pipeline/loading-items.js index 05d619befd..1c2fec478a 100644 --- a/cocos2d/core/load-pipeline/loading-items.js +++ b/cocos2d/core/load-pipeline/loading-items.js @@ -39,6 +39,7 @@ var JS = require('../platform/js'); * Item can hold other custom properties * * @class LoadingItems + * @extends CallbacksInvoker */ var LoadingItems = function () { CallbacksInvoker.call(this); @@ -126,7 +127,7 @@ JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, { * Remove an item, can only remove completed item, ongoing item can not be removed * @param {String} url */ - remove: function (url) { + removeItem: function (url) { if (this.completed[url]) { delete this.completed[url]; delete this.map[url]; diff --git a/cocos2d/core/load-pipeline/pipeline.js b/cocos2d/core/load-pipeline/pipeline.js index ac23979c10..83b6e7b21c 100644 --- a/cocos2d/core/load-pipeline/pipeline.js +++ b/cocos2d/core/load-pipeline/pipeline.js @@ -318,8 +318,10 @@ JS.mixin(Pipeline.prototype, { var checker = {}; var count = 0; + var items = this._items; function loadedCheck (item) { checker[item.src] = item; + items.remove(item.src, loadedCheck); for (var url in checker) { // Not done yet @@ -335,9 +337,9 @@ JS.mixin(Pipeline.prototype, { var url = urlList[i].src || urlList[i]; if (typeof url !== 'string') continue; - var item = this._items.map[url]; + var item = items.map[url]; if ( !item ) { - this._items.add(url, loadedCheck); + items.add(url, loadedCheck); checker[url] = null; count++; } @@ -389,6 +391,9 @@ JS.mixin(Pipeline.prototype, { this.complete(); items.invoke(url, item); + + // remove all callbacks added for url + items.removeAll(url); }, /** @@ -443,7 +448,7 @@ JS.mixin(Pipeline.prototype, { item.error = new Error('Canceled manually'); this.flowOut(item); } - this._items.remove(url); + this._items.removeItem(url); } else { return false;