From aea1561c3c75aaf3ad1fde47c9013fdb43f8debb Mon Sep 17 00:00:00 2001 From: pandamicro Date: Sun, 14 Feb 2016 00:20:14 +0800 Subject: [PATCH] Improve loader implementation --- cocos2d/core/loader/loading-items.js | 11 +++++--- cocos2d/core/loader/pipeline.js | 40 +++++++++++++++------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/cocos2d/core/loader/loading-items.js b/cocos2d/core/loader/loading-items.js index 7524ec212f..989c9038ee 100644 --- a/cocos2d/core/loader/loading-items.js +++ b/cocos2d/core/loader/loading-items.js @@ -22,7 +22,7 @@ THE SOFTWARE. ****************************************************************************/ -var EventTarget = require('../event/event-target'); +var CallbacksInvoker = require('../event/callbacks-invoker'); var JS = require('../platform/js'); function createItem (url) { @@ -35,14 +35,14 @@ function createItem (url) { } var LoadingItems = function () { - EventTarget.call(this); + CallbacksInvoker.call(this); this.map = {}; this.completed = {}; // this.list = []; }; -JS.mixin(LoadingItems.prototype, EventTarget.prototype, { +JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, { append: function (urlList) { var list = []; for (var i = 0; i < urlList.length; ++i) { @@ -56,7 +56,10 @@ JS.mixin(LoadingItems.prototype, EventTarget.prototype, { return list; }, isCompleted: function () { - return Object.keys(this.map).length === 0; + for (var any in this.map) { + return false; + } + return true; }, itemDone: function (url) { if (!this.map[url]) { diff --git a/cocos2d/core/loader/pipeline.js b/cocos2d/core/loader/pipeline.js index 85ee2f85c7..2676ecc113 100644 --- a/cocos2d/core/loader/pipeline.js +++ b/cocos2d/core/loader/pipeline.js @@ -25,11 +25,17 @@ var JS = require('../platform/js'); var LoadingItems = require('loading-items'); -var asyncFLow = function (item) { +var ItemState = { + WORKING: 1, + COMPLETE: 2, + ERROR: 3 +}; + +function asyncFlow (item) { var pipeId = this.id; var itemState = item[pipeId]; - if (itemState === Pipeline.ItemState.COMPLETE) { + if (itemState === ItemState.COMPLETE) { if (this.next) { this.next.flow(item); } @@ -37,19 +43,19 @@ var asyncFLow = function (item) { this.pipeline.flowOut(item); } } - else if (itemState === Pipeline.ItemState.WORKING) { + else if (itemState === ItemState.WORKING) { return; } else { - item[pipeId] = Pipeline.ItemState.WORKING; + item[pipeId] = ItemState.WORKING; this.handle(item, function (err) { if (err) { - item[pipeId] = Pipeline.ItemState.ERROR; + item[pipeId] = ItemState.ERROR; item.error = err; this.pipeline.flowOut(item); } else { - item[pipeId] = Pipeline.ItemState.COMPLETE; + item[pipeId] = ItemState.COMPLETE; if (this.next) { this.next.flow(item); } @@ -59,12 +65,12 @@ var asyncFLow = function (item) { } }); } -}; -var syncFlow = function (item) { +} +function syncFlow (item) { var pipeId = this.id; var itemState = item[pipeId]; - if (itemState === Pipeline.ItemState.COMPLETE) { + if (itemState === ItemState.COMPLETE) { if (this.next) { this.next.flow(item); } @@ -72,13 +78,13 @@ var syncFlow = function (item) { this.pipeline.flowOut(item); } } - else if (itemState === Pipeline.ItemState.WORKING) { + else if (itemState === ItemState.WORKING) { return; } else { - item[pipeId] = Pipeline.ItemState.WORKING; + item[pipeId] = ItemState.WORKING; this.handle(item); - item[pipeId] = Pipeline.ItemState.COMPLETE; + item[pipeId] = ItemState.COMPLETE; if (this.next) { this.next.flow(item); } @@ -86,7 +92,7 @@ var syncFlow = function (item) { this.pipeline.flowOut(item); } } -}; +} var Pipeline = function (pipes) { this._pipes = pipes; @@ -104,7 +110,7 @@ var Pipeline = function (pipes) { pipe.next = i < pipes.length - 1 ? pipes[i+1] : null; if (pipe.isAsync) { - pipe.flow = asyncFLow; + pipe.flow = asyncFlow; } else { pipe.flow = syncFlow; @@ -112,11 +118,7 @@ var Pipeline = function (pipes) { } }; -Pipeline.ItemState = new cc.Enum({ - WORKING: 1, - COMPLETE: 2, - ERROR: 3 -}); +Pipeline.ItemState = new cc.Enum(ItemState); JS.mixin(Pipeline.prototype, { flow: function (urlList) {