From c708d82ca503f56f59cd744f347e36f9b7cd4fa6 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 4 Feb 2016 17:45:57 +0800 Subject: [PATCH] Initial implementation of pipeline --- cocos2d/core/loader/loading-items.js | 75 ++++++++++++++ cocos2d/core/loader/pipeline.js | 146 +++++++++++++++++++++++++++ 2 files changed, 221 insertions(+) create mode 100644 cocos2d/core/loader/loading-items.js create mode 100644 cocos2d/core/loader/pipeline.js diff --git a/cocos2d/core/loader/loading-items.js b/cocos2d/core/loader/loading-items.js new file mode 100644 index 0000000000..7524ec212f --- /dev/null +++ b/cocos2d/core/loader/loading-items.js @@ -0,0 +1,75 @@ +/**************************************************************************** + Copyright (c) 2016 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +var EventTarget = require('../event/event-target'); +var JS = require('../platform/js'); + +function createItem (url) { + return { + src: url, + error: null, + content: null, + complete: false + }; +} + +var LoadingItems = function () { + EventTarget.call(this); + + this.map = {}; + this.completed = {}; + // this.list = []; +}; + +JS.mixin(LoadingItems.prototype, EventTarget.prototype, { + append: function (urlList) { + var list = []; + for (var i = 0; i < urlList.length; ++i) { + var url = urlList[i]; + // No duplicated url + if (!this.map[url]) { + this.map[url] = createItem(url); + list.push(url); + } + } + return list; + }, + isCompleted: function () { + return Object.keys(this.map).length === 0; + }, + itemDone: function (url) { + if (!this.map[url]) { + return; + } + + var item = this.map[url]; + item.complete = true; + this.completed[url] = item; + delete this.map[url]; + + this.emit(url, item); + } +}); + +module.exports = LoadingItems; \ No newline at end of file diff --git a/cocos2d/core/loader/pipeline.js b/cocos2d/core/loader/pipeline.js new file mode 100644 index 0000000000..85ee2f85c7 --- /dev/null +++ b/cocos2d/core/loader/pipeline.js @@ -0,0 +1,146 @@ +/**************************************************************************** + Copyright (c) 2016 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +var JS = require('../platform/js'); +var LoadingItems = require('loading-items'); + +var asyncFLow = function (item) { + var pipeId = this.id; + var itemState = item[pipeId]; + + if (itemState === Pipeline.ItemState.COMPLETE) { + if (this.next) { + this.next.flow(item); + } + else { + this.pipeline.flowOut(item); + } + } + else if (itemState === Pipeline.ItemState.WORKING) { + return; + } + else { + item[pipeId] = Pipeline.ItemState.WORKING; + this.handle(item, function (err) { + if (err) { + item[pipeId] = Pipeline.ItemState.ERROR; + item.error = err; + this.pipeline.flowOut(item); + } + else { + item[pipeId] = Pipeline.ItemState.COMPLETE; + if (this.next) { + this.next.flow(item); + } + else { + this.pipeline.flowOut(item); + } + } + }); + } +}; +var syncFlow = function (item) { + var pipeId = this.id; + var itemState = item[pipeId]; + + if (itemState === Pipeline.ItemState.COMPLETE) { + if (this.next) { + this.next.flow(item); + } + else { + this.pipeline.flowOut(item); + } + } + else if (itemState === Pipeline.ItemState.WORKING) { + return; + } + else { + item[pipeId] = Pipeline.ItemState.WORKING; + this.handle(item); + item[pipeId] = Pipeline.ItemState.COMPLETE; + if (this.next) { + this.next.flow(item); + } + else { + this.pipeline.flowOut(item); + } + } +}; + +var Pipeline = function (pipes) { + this._pipes = pipes; + this._items = new LoadingItems(); + this._flowing = false; + + for (var i = 0; i < pipes.length; ++i) { + var pipe = pipes[i]; + // Must have handle and id, handle for flow, id for state flag + if (!pipe.handle || !pipe.id) { + continue; + } + + pipe.pipeline = this; + pipe.next = i < pipes.length - 1 ? pipes[i+1] : null; + + if (pipe.isAsync) { + pipe.flow = asyncFLow; + } + else { + pipe.flow = syncFlow; + } + } +}; + +Pipeline.ItemState = new cc.Enum({ + WORKING: 1, + COMPLETE: 2, + ERROR: 3 +}); + +JS.mixin(Pipeline.prototype, { + flow: function (urlList) { + if (!this._flowing) { + this._flowing = true; + } + + var appendedUrls = this._items.append(urlList); + var items = this._items.map; + + if (this._pipes.length > 0) { + for (var i = 0; i < appendedUrls.length; i++) { + var url = appendedUrls[i]; + this._pipes[0].flow(items[url]); + } + } + }, + flowOut: function (item) { + this._items.itemDone(item.src); + // All completed + if (this._items.isCompleted) { + // this.onComplete(this._items); + } + } +}); + +module.exports = Pipeline; \ No newline at end of file