From f0c7b7218aad3d552ed1ff5f406f4358c5bad572 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 24 Feb 2016 19:52:28 +0800 Subject: [PATCH] Improve Pipeline / CCLoader APIs --- cocos2d/core/CCGame.js | 2 +- cocos2d/core/load-pipeline/CCLoader.js | 18 ++++++++++++++++++ cocos2d/core/load-pipeline/downloader.js | 5 ++++- cocos2d/core/load-pipeline/loader.js | 5 ++++- cocos2d/core/load-pipeline/pipeline.js | 15 +++++++++++++++ 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/cocos2d/core/CCGame.js b/cocos2d/core/CCGame.js index 1bbc673c90..b27f7393b6 100644 --- a/cocos2d/core/CCGame.js +++ b/cocos2d/core/CCGame.js @@ -259,7 +259,7 @@ var game = /** @lends cc.game# */{ // Load game scripts var jsList = config[CONFIG_KEY.jsList]; - if (jsList) { + if (jsList && jsList.length > 0) { cc.loader.load(jsList, function (err) { if (err) throw new Error(JSON.stringify(err)); self._prepared = true; diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js index 7664ab7135..728440345f 100644 --- a/cocos2d/core/load-pipeline/CCLoader.js +++ b/cocos2d/core/load-pipeline/CCLoader.js @@ -42,6 +42,24 @@ cc.loader = new Pipeline([ JS.mixin(cc.loader, { + /** + * The downloader in cc.loader's pipeline, it's by default the first pipe. + * It's used to download files with several handlers: pure text, image, script, audio, font, uuid. + * You can add your own download function with addDownloadHandlers + * @property downloader + * @type {Object} + */ + downloader: downloader, + + /** + * The downloader in cc.loader's pipeline, it's by default the second pipe. + * It's used to parse downloaded content with several handlers: JSON, image, plist, fnt, uuid. + * You can add your own download function with addLoadHandlers + * @property loader + * @type {Object} + */ + loader: loader, + /** * Get XMLHttpRequest. * @returns {XMLHttpRequest} diff --git a/cocos2d/core/load-pipeline/downloader.js b/cocos2d/core/load-pipeline/downloader.js index 1b1745f3df..4834002cee 100644 --- a/cocos2d/core/load-pipeline/downloader.js +++ b/cocos2d/core/load-pipeline/downloader.js @@ -316,6 +316,8 @@ var defaultMap = { 'default' : downloadText }; +var ID = 'Downloader'; + /** * The downloader pipe, it can download several types of files: * 1. Text @@ -338,7 +340,7 @@ var defaultMap = { * @param {Object} extMap Custom supported types with corresponded handler */ var Downloader = function (extMap) { - this.id = 'Downloader'; + this.id = ID; this.async = true; this.pipeline = null; this.maxConcurrent = 2; @@ -347,6 +349,7 @@ var Downloader = function (extMap) { this.extMap = JS.mixin(extMap, defaultMap); }; +Downloader.ID = ID; JS.mixin(Downloader.prototype, { /** * Add custom supported types handler or modify existing type handler. diff --git a/cocos2d/core/load-pipeline/loader.js b/cocos2d/core/load-pipeline/loader.js index cbf499caf7..eef44cf359 100644 --- a/cocos2d/core/load-pipeline/loader.js +++ b/cocos2d/core/load-pipeline/loader.js @@ -182,6 +182,8 @@ var defaultMap = { 'default' : loadNothing }; +var ID = 'Loader'; + /** * The loader pipe, it can load several types of files: * 1. Images @@ -206,12 +208,13 @@ var defaultMap = { * @param {Object} extMap Custom supported types with corresponded handler */ var Loader = function (extMap) { - this.id = 'Loader'; + this.id = ID; this.async = true; this.pipeline = null; this.extMap = JS.mixin(extMap, defaultMap); }; +Loader.ID = ID; JS.mixin(Loader.prototype, { /** * Add custom supported types handler or modify existing type handler. diff --git a/cocos2d/core/load-pipeline/pipeline.js b/cocos2d/core/load-pipeline/pipeline.js index 64a7a4533a..295ccc6f56 100644 --- a/cocos2d/core/load-pipeline/pipeline.js +++ b/cocos2d/core/load-pipeline/pipeline.js @@ -202,6 +202,14 @@ Pipeline.ItemState = new cc.Enum(ItemState); Pipeline.getXMLHttpRequest = getXMLHttpRequest; JS.mixin(Pipeline.prototype, { + /** + * Insert a new pipe at the given index of the pipeline. + * A pipe must contain an `id` in string and a `handle` function, the id must be unique in the pipeline. + * + * @method insertPipe + * @param {Object} pipe The pipe to be inserted + * @param {Number} index The index to insert + */ insertPipe: function (pipe, index) { // Must have handle and id, handle for flow, id for state flag if (!pipe.handle || !pipe.id) { @@ -225,6 +233,13 @@ JS.mixin(Pipeline.prototype, { } }, + /** + * Add a new pipe at the end of the pipeline. + * A pipe must contain an `id` in string and a `handle` function, the id must be unique in the pipeline. + * + * @method appendPipe + * @param {Object} pipe The pipe to be appended + */ appendPipe: function (pipe) { // Must have handle and id, handle for flow, id for state flag if (!pipe.handle || !pipe.id) {