diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 92e368923c..169c880f7d 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -3460,24 +3460,26 @@ class Runtime extends EventEmitter { /** * Wrap an asset loading promise with progress support. * @template T - * @param {Promise} promise + * @param {() => Promise} callback * @returns {Promise} */ - wrapAssetRequest (promise) { + wrapAssetRequest (callback) { this.totalAssetRequests++; this.emitAssetProgress(); - return promise - .then(result => { - this.finishedAssetRequests++; - this.emitAssetProgress(); - return result; - }) - .catch(error => { - this.finishedAssetRequests++; - this.emitAssetProgress(); - throw error; - }); + const onSuccess = result => { + this.finishedAssetRequests++; + this.emitAssetProgress(); + return result; + }; + + const onError = error => { + this.finishedAssetRequests++; + this.emitAssetProgress(); + throw error; + }; + + return callback().then(onSuccess, onError); } } diff --git a/src/serialization/sb2.js b/src/serialization/sb2.js index 6dc9cef4d0..1cde8e26e2 100644 --- a/src/serialization/sb2.js +++ b/src/serialization/sb2.js @@ -500,7 +500,7 @@ const parseScratchAssets = function (object, runtime, topLevel, zip) { // the file name of the costume should be the baseLayerID followed by the file ext const assetFileName = `${costumeSource.baseLayerID}.${ext}`; const textLayerFileName = costumeSource.textLayerID ? `${costumeSource.textLayerID}.png` : null; - costumePromises.push(runtime.wrapAssetRequest( + costumePromises.push(runtime.wrapAssetRequest(() => deserializeCostume(costume, runtime, zip, assetFileName, textLayerFileName) .then(() => loadCostume(costume.md5, costume, runtime, 2 /* optVersion */)) )); @@ -536,7 +536,7 @@ const parseScratchAssets = function (object, runtime, topLevel, zip) { // the file name of the sound should be the soundID (provided from the project.json) // followed by the file ext const assetFileName = `${soundSource.soundID}.${ext}`; - soundPromises.push(runtime.wrapAssetRequest( + soundPromises.push(runtime.wrapAssetRequest(() => deserializeSound(sound, runtime, zip, assetFileName) .then(() => loadSound(sound, runtime, soundBank)) )); diff --git a/src/serialization/sb3.js b/src/serialization/sb3.js index bad68d8411..815c2837a4 100644 --- a/src/serialization/sb3.js +++ b/src/serialization/sb3.js @@ -1115,7 +1115,7 @@ const parseScratchAssets = function (object, runtime, zip) { // we're always loading the 'sb3' representation of the costume // any translation that needs to happen will happen in the process // of building up the costume object into an sb3 format - return runtime.wrapAssetRequest(deserializeCostume(costume, runtime, zip) + return runtime.wrapAssetRequest(() => deserializeCostume(costume, runtime, zip) .then(() => loadCostume(costumeMd5Ext, costume, runtime))); // Only attempt to load the costume after the deserialization // process has been completed @@ -1140,7 +1140,7 @@ const parseScratchAssets = function (object, runtime, zip) { // we're always loading the 'sb3' representation of the costume // any translation that needs to happen will happen in the process // of building up the costume object into an sb3 format - return runtime.wrapAssetRequest(deserializeSound(sound, runtime, zip) + return runtime.wrapAssetRequest(() => deserializeSound(sound, runtime, zip) .then(() => loadSound(sound, runtime, assets.soundBank))); // Only attempt to load the sound after the deserialization // process has been completed. diff --git a/src/util/tw-asset-util.js b/src/util/tw-asset-util.js index 64abc10bb7..380d9fefb8 100644 --- a/src/util/tw-asset-util.js +++ b/src/util/tw-asset-util.js @@ -25,7 +25,7 @@ class AssetUtil { } if (file) { - return runtime.wrapAssetRequest(file.async('uint8array').then(data => runtime.storage.createAsset( + return runtime.wrapAssetRequest(() => file.async('uint8array').then(data => runtime.storage.createAsset( assetType, ext, data, @@ -35,7 +35,7 @@ class AssetUtil { } } - return runtime.wrapAssetRequest(runtime.storage.load(assetType, md5, ext)); + return runtime.wrapAssetRequest(() => runtime.storage.load(assetType, md5, ext)); } } diff --git a/test/integration/tw_asset_progress.js b/test/integration/tw_asset_progress.js index fd751da91a..4eccf9d3c2 100644 --- a/test/integration/tw_asset_progress.js +++ b/test/integration/tw_asset_progress.js @@ -69,13 +69,13 @@ test('wrapAssetRequest', t => { }); Promise.all([ - runtime.wrapAssetRequest(Promise.resolve(1)), - runtime.wrapAssetRequest(Promise.resolve(2)) + runtime.wrapAssetRequest(() => Promise.resolve(1)), + runtime.wrapAssetRequest(() => Promise.resolve(2)) ]).then(results => { t.same(results, [1, 2]); // eslint-disable-next-line prefer-promise-reject-errors - runtime.wrapAssetRequest(Promise.reject(3)).catch(error => { + runtime.wrapAssetRequest(() => Promise.reject(3)).catch(error => { t.equal(error, 3); t.same(log, [ [0, 1],