Skip to content

Commit

Permalink
Replace wrapAssetRequest promise argument with a callback
Browse files Browse the repository at this point in the history
The flexibility may be useful later
  • Loading branch information
GarboMuffin committed May 23, 2024
1 parent d69256a commit d39a30f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
28 changes: 15 additions & 13 deletions src/engine/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -3460,24 +3460,26 @@ class Runtime extends EventEmitter {
/**
* Wrap an asset loading promise with progress support.
* @template T
* @param {Promise<T>} promise
* @param {() => Promise<T>} callback
* @returns {Promise<T>}
*/
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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/serialization/sb2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */))
));
Expand Down Expand Up @@ -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))
));
Expand Down
4 changes: 2 additions & 2 deletions src/serialization/sb3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/util/tw-asset-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,7 +35,7 @@ class AssetUtil {
}
}

return runtime.wrapAssetRequest(runtime.storage.load(assetType, md5, ext));
return runtime.wrapAssetRequest(() => runtime.storage.load(assetType, md5, ext));
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/integration/tw_asset_progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

2 comments on commit d39a30f

@CST1229
Copy link

@CST1229 CST1229 commented on d39a30f Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lazy loading preparations???

@GarboMuffin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no

Please sign in to comment.