mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
Resolve circle reference issue
This commit is contained in:
@@ -176,7 +176,7 @@ JS.mixin(CCLoader.prototype, {
|
||||
}
|
||||
}
|
||||
|
||||
LoadingItems.create(this, progressCallback, function (errors, items) {
|
||||
LoadingItems.create(this, resources, progressCallback, function (errors, items) {
|
||||
callInNextTick(function () {
|
||||
if (!completeCallback)
|
||||
return;
|
||||
@@ -196,10 +196,13 @@ JS.mixin(CCLoader.prototype, {
|
||||
self.clear();
|
||||
}
|
||||
});
|
||||
}, resources);
|
||||
});
|
||||
},
|
||||
|
||||
flowInDeps: function (urlList, callback) {
|
||||
flowInDeps: function (owner, urlList, callback) {
|
||||
if (!owner.deps) {
|
||||
owner.deps = [];
|
||||
}
|
||||
for (var i = 0; i < urlList.length; ++i) {
|
||||
var url = urlList[i].id || urlList[i];
|
||||
if (typeof url !== 'string')
|
||||
@@ -207,14 +210,19 @@ JS.mixin(CCLoader.prototype, {
|
||||
var item = this.getItem(url);
|
||||
if (item) {
|
||||
urlList[i] = item;
|
||||
// Collect deps to avoid circle reference
|
||||
owner.deps.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
var deps = LoadingItems.create(this, null, function (errors, items) {
|
||||
var queue = LoadingItems.create(this, function (errors, items) {
|
||||
// Clear deps because it's already done
|
||||
// Each item will only flowInDeps once, so it's still safe here
|
||||
owner.deps.length = 0;
|
||||
callback(errors, items);
|
||||
items.destroy();
|
||||
});
|
||||
return deps.append(urlList);
|
||||
return queue.append(urlList, owner);
|
||||
},
|
||||
|
||||
_resources: resources,
|
||||
|
||||
@@ -54,7 +54,8 @@ function createItem (id, queueId) {
|
||||
error: null,
|
||||
content: null,
|
||||
complete: false,
|
||||
states: {}
|
||||
states: {},
|
||||
deps: null
|
||||
};
|
||||
JS.mixin(result, id);
|
||||
}
|
||||
@@ -67,7 +68,8 @@ function createItem (id, queueId) {
|
||||
error: null,
|
||||
content: null,
|
||||
complete: false,
|
||||
states: {}
|
||||
states: {},
|
||||
deps: null
|
||||
};
|
||||
}
|
||||
|
||||
@@ -81,6 +83,24 @@ function createItem (id, queueId) {
|
||||
return result;
|
||||
}
|
||||
|
||||
function checkCircleReference(owner, item) {
|
||||
if (!item.deps) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var i, deps = item.deps, subDep;
|
||||
for (var i = 0; i < deps.length; i++) {
|
||||
subDep = deps[i];
|
||||
if (subDep.id === owner.id) {
|
||||
return true;
|
||||
}
|
||||
else if (subDep.deps && checkCircleReference(owner, subDep)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* LoadingItems is the manager of items in pipeline.</br>
|
||||
@@ -112,7 +132,7 @@ function createItem (id, queueId) {
|
||||
* @class LoadingItems
|
||||
* @extends CallbacksInvoker
|
||||
*/
|
||||
var LoadingItems = function (pipeline, onProgress, onComplete, urlList) {
|
||||
var LoadingItems = function (pipeline, urlList, onProgress, onComplete) {
|
||||
CallbacksInvoker.call(this);
|
||||
|
||||
this._id = ++_qid;
|
||||
@@ -123,6 +143,8 @@ var LoadingItems = function (pipeline, onProgress, onComplete, urlList) {
|
||||
|
||||
this._errorUrls = [];
|
||||
|
||||
this._appending = false;
|
||||
|
||||
this.onProgress = onProgress;
|
||||
|
||||
this.onComplete = onComplete;
|
||||
@@ -179,7 +201,16 @@ var LoadingItems = function (pipeline, onProgress, onComplete, urlList) {
|
||||
|
||||
LoadingItems.ItemState = new cc.Enum(ItemState);
|
||||
|
||||
LoadingItems.create = function (pipeline, onProgress, onComplete, urlList) {
|
||||
LoadingItems.create = function (pipeline, urlList, onProgress, onComplete) {
|
||||
if (onProgress === undefined) {
|
||||
onComplete = urlList;
|
||||
urlList = onProgress = null;
|
||||
}
|
||||
else if (onComplete === undefined) {
|
||||
onComplete = onProgress;
|
||||
onProgress = null;
|
||||
}
|
||||
|
||||
var queue = _pool.pop();
|
||||
if (queue) {
|
||||
queue._pipeline = pipeline;
|
||||
@@ -194,8 +225,9 @@ LoadingItems.create = function (pipeline, onProgress, onComplete, urlList) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
queue = new LoadingItems(pipeline, onProgress, onComplete, urlList);
|
||||
queue = new LoadingItems(pipeline, urlList, onProgress, onComplete);
|
||||
}
|
||||
|
||||
return queue;
|
||||
};
|
||||
|
||||
@@ -212,11 +244,12 @@ LoadingItems.itemComplete = function (item) {
|
||||
};
|
||||
|
||||
JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, {
|
||||
append: function (urlList) {
|
||||
append: function (urlList, owner) {
|
||||
if (!this.active) {
|
||||
return [];
|
||||
}
|
||||
|
||||
this._appending = true;
|
||||
var accepted = [], i, url, item;
|
||||
for (i = 0; i < urlList.length; ++i) {
|
||||
url = urlList[i];
|
||||
@@ -224,10 +257,10 @@ JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, {
|
||||
// Already queued in another items queue, url is actually the item
|
||||
if (url.queueId && !this.map[url.id]) {
|
||||
this.map[url.id] = url;
|
||||
// Queued and has a alias, consider as completed
|
||||
if (true) {//url.complete) {
|
||||
// Queued and completed or Owner circle referenced by dependency
|
||||
if (url.complete || checkCircleReference(owner, url)) {
|
||||
this.totalCount++;
|
||||
console.log('----- Completed already ' + url.id + ', rest: ' + (this.totalCount - this.completedCount-1));
|
||||
console.log('----- Completed already or circle referenced ' + url.id + ', rest: ' + (this.totalCount - this.completedCount-1));
|
||||
this.itemComplete(url.id);
|
||||
continue;
|
||||
}
|
||||
@@ -260,6 +293,7 @@ JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, {
|
||||
}
|
||||
}
|
||||
}
|
||||
this._appending = false;
|
||||
|
||||
// Manually complete
|
||||
if (this.completedCount === this.totalCount) {
|
||||
@@ -441,14 +475,13 @@ JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, {
|
||||
this._errorUrls.splice(errorListId, 1);
|
||||
}
|
||||
|
||||
item.complete = true;
|
||||
this.completed[id] = item;
|
||||
this.completedCount++;
|
||||
|
||||
this.onProgress && this.onProgress(this.completedCount, this.totalCount, item);
|
||||
|
||||
// All completed
|
||||
if (this.completedCount >= this.totalCount) {
|
||||
if (!this._appending && this.completedCount >= this.totalCount) {
|
||||
console.log('===== All Completed ');
|
||||
this.allComplete();
|
||||
}
|
||||
@@ -458,24 +491,21 @@ JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, {
|
||||
|
||||
destroy: function () {
|
||||
this.active = false;
|
||||
this._appending = false;
|
||||
this._pipeline = null;
|
||||
this._errorUrls.length = 0;
|
||||
this.onProgress = null;
|
||||
this.onComplete = null;
|
||||
|
||||
// Reinitialize CallbacksInvoker, generate three new objects, could be improved
|
||||
CallbacksInvoker.call(this);
|
||||
|
||||
for (var key in this.map) {
|
||||
delete this.map[key];
|
||||
}
|
||||
for (key in this.completed) {
|
||||
delete this.completed[key];
|
||||
}
|
||||
this.map = {};
|
||||
this.completed = {};
|
||||
|
||||
this.totalCount = 0;
|
||||
this.completedCount = 0;
|
||||
|
||||
// Reinitialize CallbacksInvoker, generate three new objects, could be improved
|
||||
CallbacksInvoker.call(this);
|
||||
|
||||
_queues[this._id] = null;
|
||||
if (_pool.indexOf(this) === -1 && _pool.length < _POOL_MAX_LENGTH) {
|
||||
_pool.push(this);
|
||||
|
||||
@@ -96,16 +96,13 @@ module.exports = {
|
||||
|
||||
var unpacker = globalUnpackers[packUuid];
|
||||
if (unpacker) {
|
||||
// ensure async
|
||||
setTimeout(function () {
|
||||
var json = unpacker.retrieve(uuid);
|
||||
if (json) {
|
||||
callback(null, json);
|
||||
}
|
||||
else {
|
||||
error(callback, uuid, packUuid);
|
||||
}
|
||||
}, 0);
|
||||
var json = unpacker.retrieve(uuid);
|
||||
if (json) {
|
||||
callback(null, json);
|
||||
}
|
||||
else {
|
||||
error(callback, uuid, packUuid);
|
||||
}
|
||||
}
|
||||
else {
|
||||
this._loadNewPack(uuid, packUuid, callback);
|
||||
|
||||
@@ -281,18 +281,19 @@ JS.mixin(Pipeline.prototype, {
|
||||
* @param {Function} callback
|
||||
* @return {Array} Items accepted by the pipeline
|
||||
*/
|
||||
flowInDeps: function (urlList, callback) {
|
||||
var deps = LoadingItems.create(this, null, function (errors, items) {
|
||||
flowInDeps: function (owner, urlList, callback) {
|
||||
var deps = LoadingItems.create(this, function (errors, items) {
|
||||
callback(errors, items);
|
||||
items.destroy();
|
||||
});
|
||||
return deps.append(urlList);
|
||||
return deps.append(urlList, owner);
|
||||
},
|
||||
|
||||
flowOut: function (item) {
|
||||
if (!this._cache[item.id]) {
|
||||
this._cache[item.id] = item;
|
||||
}
|
||||
item.complete = true;
|
||||
LoadingItems.itemComplete(item);
|
||||
},
|
||||
|
||||
|
||||
@@ -75,18 +75,15 @@ function loadDepends (pipeline, item, asset, tdInfo, deferredLoadRawAssetsInRunt
|
||||
else {
|
||||
objList = JS.array.copy(tdInfo.uuidObjList);
|
||||
propList = JS.array.copy(tdInfo.uuidPropList);
|
||||
depends = [];
|
||||
depends = new Array(uuidList.length);
|
||||
// declare depends assets
|
||||
for (i = 0; i < uuidList.length; i++) {
|
||||
dependUuid = uuidList[i];
|
||||
if (dependUuid === uuid) {
|
||||
continue;
|
||||
}
|
||||
depends.push({
|
||||
depends[i] = {
|
||||
id: dependUuid,
|
||||
type: 'uuid',
|
||||
uuid: dependUuid
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
// declare raw
|
||||
@@ -104,7 +101,9 @@ function loadDepends (pipeline, item, asset, tdInfo, deferredLoadRawAssetsInRunt
|
||||
// cache dependencies for auto release
|
||||
var dependKeys = item.dependKeys = [];
|
||||
|
||||
pipeline.flowInDeps(depends, function (errors, items) {
|
||||
// Predefine content for dependencies usage
|
||||
// item.content = asset;
|
||||
pipeline.flowInDeps(item, depends, function (errors, items) {
|
||||
var item;
|
||||
for (var src in items.map) {
|
||||
item = items.map[src];
|
||||
@@ -118,7 +117,7 @@ function loadDepends (pipeline, item, asset, tdInfo, deferredLoadRawAssetsInRunt
|
||||
var dependProp = propList[i];
|
||||
item = items.map[dependSrc];
|
||||
if (item) {
|
||||
if (item.complete) {
|
||||
if (item.complete || item.content) {
|
||||
if (item.error) {
|
||||
cc._throw(item.error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user