Try to make loadCostume more readable, and revert deserializeCostume to changing the costume object directly

This commit is contained in:
DD 2018-11-05 15:50:28 -05:00
parent 692b71a737
commit 86149325b1
4 changed files with 33 additions and 23 deletions

View file

@ -152,6 +152,12 @@ const loadCostumeFromAsset = function (costume, costumeAsset, runtime, optVersio
* @returns {?Promise} - a promise which will resolve after skinId is set, or null on error.
*/
const loadCostume = function (md5ext, costume, runtime, optVersion) {
let costumePromise;
if (costume.asset) {
// Costume comes with asset. It could be coming from camera, image upload, drag and drop, or sb file
costumePromise = Promise.resolve(costume.asset);
} else {
// Need to load the costume from storage. The server should have a reference to this md5.
if (!runtime.storage) {
log.error('No storage module present; cannot load costume asset: ', md5ext);
return Promise.resolve(costume);
@ -163,10 +169,14 @@ const loadCostume = function (md5ext, costume, runtime, optVersion) {
const ext = idParts[1].toLowerCase();
const assetType = (ext === 'svg') ? AssetType.ImageVector : AssetType.ImageBitmap;
costume.dataFormat = ext;
return (
(costume.asset && Promise.resolve(costume.asset)) ||
runtime.storage.load(assetType, md5, ext)
).then(costumeAsset => {
costumePromise = runtime.storage.load(assetType, md5, ext);
if (!costumePromise) {
log.error(`Couldn't fetch costume asset: ${md5ext}`);
return;
}
}
return costumePromise.then(costumeAsset => {
costume.asset = costumeAsset;
return loadCostumeFromAsset(costume, costumeAsset, runtime, optVersion);
})

View file

@ -79,7 +79,10 @@ const deserializeCostume = function (costume, runtime, zip, assetFileName) {
costume.asset.dataFormat,
new Uint8Array(Object.keys(costume.asset.data).map(key => costume.asset.data[key])),
costume.asset.assetId
));
))
.then(asset => {
costume.asset = asset;
});
}
if (!zip) {
@ -112,7 +115,10 @@ const deserializeCostume = function (costume, runtime, zip, assetFileName) {
costumeFormat,
data,
assetId
));
))
.then(asset => {
costume.asset = asset;
});
};
module.exports = {

View file

@ -413,10 +413,7 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
// the file name of the costume should be the baseLayerID followed by the file ext
const assetFileName = `${costumeSource.baseLayerID}.${ext}`;
costumePromises.push(deserializeCostume(costume, runtime, zip, assetFileName)
.then(asset => {
costume.asset = asset;
return loadCostume(costume.md5, costume, runtime, 2 /* optVersion */);
})
.then(() => loadCostume(costume.md5, costume, runtime, 2 /* optVersion */))
);
}
}

View file

@ -847,10 +847,7 @@ const parseScratchObject = function (object, runtime, extensions, zip) {
// any translation that needs to happen will happen in the process
// of building up the costume object into an sb3 format
return deserializeCostume(costume, runtime, zip)
.then(asset => {
costume.asset = asset;
return loadCostume(costumeMd5Ext, costume, runtime);
});
.then(() => loadCostume(costumeMd5Ext, costume, runtime));
// Only attempt to load the costume after the deserialization
// process has been completed
});