Skip to content

Commit d39a30f

Browse files
committed
Replace wrapAssetRequest promise argument with a callback
The flexibility may be useful later
1 parent d69256a commit d39a30f

File tree

5 files changed

+24
-22
lines changed

5 files changed

+24
-22
lines changed

src/engine/runtime.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3460,24 +3460,26 @@ class Runtime extends EventEmitter {
34603460
/**
34613461
* Wrap an asset loading promise with progress support.
34623462
* @template T
3463-
* @param {Promise<T>} promise
3463+
* @param {() => Promise<T>} callback
34643464
* @returns {Promise<T>}
34653465
*/
3466-
wrapAssetRequest (promise) {
3466+
wrapAssetRequest (callback) {
34673467
this.totalAssetRequests++;
34683468
this.emitAssetProgress();
34693469

3470-
return promise
3471-
.then(result => {
3472-
this.finishedAssetRequests++;
3473-
this.emitAssetProgress();
3474-
return result;
3475-
})
3476-
.catch(error => {
3477-
this.finishedAssetRequests++;
3478-
this.emitAssetProgress();
3479-
throw error;
3480-
});
3470+
const onSuccess = result => {
3471+
this.finishedAssetRequests++;
3472+
this.emitAssetProgress();
3473+
return result;
3474+
};
3475+
3476+
const onError = error => {
3477+
this.finishedAssetRequests++;
3478+
this.emitAssetProgress();
3479+
throw error;
3480+
};
3481+
3482+
return callback().then(onSuccess, onError);
34813483
}
34823484
}
34833485

src/serialization/sb2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ const parseScratchAssets = function (object, runtime, topLevel, zip) {
500500
// the file name of the costume should be the baseLayerID followed by the file ext
501501
const assetFileName = `${costumeSource.baseLayerID}.${ext}`;
502502
const textLayerFileName = costumeSource.textLayerID ? `${costumeSource.textLayerID}.png` : null;
503-
costumePromises.push(runtime.wrapAssetRequest(
503+
costumePromises.push(runtime.wrapAssetRequest(() =>
504504
deserializeCostume(costume, runtime, zip, assetFileName, textLayerFileName)
505505
.then(() => loadCostume(costume.md5, costume, runtime, 2 /* optVersion */))
506506
));
@@ -536,7 +536,7 @@ const parseScratchAssets = function (object, runtime, topLevel, zip) {
536536
// the file name of the sound should be the soundID (provided from the project.json)
537537
// followed by the file ext
538538
const assetFileName = `${soundSource.soundID}.${ext}`;
539-
soundPromises.push(runtime.wrapAssetRequest(
539+
soundPromises.push(runtime.wrapAssetRequest(() =>
540540
deserializeSound(sound, runtime, zip, assetFileName)
541541
.then(() => loadSound(sound, runtime, soundBank))
542542
));

src/serialization/sb3.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ const parseScratchAssets = function (object, runtime, zip) {
11151115
// we're always loading the 'sb3' representation of the costume
11161116
// any translation that needs to happen will happen in the process
11171117
// of building up the costume object into an sb3 format
1118-
return runtime.wrapAssetRequest(deserializeCostume(costume, runtime, zip)
1118+
return runtime.wrapAssetRequest(() => deserializeCostume(costume, runtime, zip)
11191119
.then(() => loadCostume(costumeMd5Ext, costume, runtime)));
11201120
// Only attempt to load the costume after the deserialization
11211121
// process has been completed
@@ -1140,7 +1140,7 @@ const parseScratchAssets = function (object, runtime, zip) {
11401140
// we're always loading the 'sb3' representation of the costume
11411141
// any translation that needs to happen will happen in the process
11421142
// of building up the costume object into an sb3 format
1143-
return runtime.wrapAssetRequest(deserializeSound(sound, runtime, zip)
1143+
return runtime.wrapAssetRequest(() => deserializeSound(sound, runtime, zip)
11441144
.then(() => loadSound(sound, runtime, assets.soundBank)));
11451145
// Only attempt to load the sound after the deserialization
11461146
// process has been completed.

src/util/tw-asset-util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class AssetUtil {
2525
}
2626

2727
if (file) {
28-
return runtime.wrapAssetRequest(file.async('uint8array').then(data => runtime.storage.createAsset(
28+
return runtime.wrapAssetRequest(() => file.async('uint8array').then(data => runtime.storage.createAsset(
2929
assetType,
3030
ext,
3131
data,
@@ -35,7 +35,7 @@ class AssetUtil {
3535
}
3636
}
3737

38-
return runtime.wrapAssetRequest(runtime.storage.load(assetType, md5, ext));
38+
return runtime.wrapAssetRequest(() => runtime.storage.load(assetType, md5, ext));
3939
}
4040
}
4141

test/integration/tw_asset_progress.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ test('wrapAssetRequest', t => {
6969
});
7070

7171
Promise.all([
72-
runtime.wrapAssetRequest(Promise.resolve(1)),
73-
runtime.wrapAssetRequest(Promise.resolve(2))
72+
runtime.wrapAssetRequest(() => Promise.resolve(1)),
73+
runtime.wrapAssetRequest(() => Promise.resolve(2))
7474
]).then(results => {
7575
t.same(results, [1, 2]);
7676

7777
// eslint-disable-next-line prefer-promise-reject-errors
78-
runtime.wrapAssetRequest(Promise.reject(3)).catch(error => {
78+
runtime.wrapAssetRequest(() => Promise.reject(3)).catch(error => {
7979
t.equal(error, 3);
8080
t.same(log, [
8181
[0, 1],

0 commit comments

Comments
 (0)