mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
Initial implementation of pipeline
This commit is contained in:
@@ -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;
|
||||||
@@ -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;
|
||||||
Reference in New Issue
Block a user