Skip to content

Commit ba6981f

Browse files
authored
Improve preload code (#230)
- Use Object.entries - Check that preload names are not undefined (caused by loading the same resource twice) - More consistent naming
1 parent 57f05c6 commit ba6981f

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

JetStreamDriver.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,11 @@ class Benchmark {
905905
}
906906
if (this.plan.preload) {
907907
let preloadCode = "";
908-
for (let [ variableName, blobURLOrPath ] of this.preloads)
908+
for (let [ variableName, blobURLOrPath ] of this.preloads) {
909+
console.assert(variableName?.length > 0, "Invalid preload name.");
910+
console.assert(blobURLOrPath?.length > 0, "Invalid preload data.");
909911
preloadCode += `JetStream.preload[${JSON.stringify(variableName)}] = "${blobURLOrPath}";\n`;
912+
}
910913
scripts.add(preloadCode);
911914
}
912915

@@ -1060,8 +1063,8 @@ class Benchmark {
10601063

10611064
if (this.plan.preload) {
10621065
this.preloads = [];
1063-
for (let prop of Object.getOwnPropertyNames(this.plan.preload)) {
1064-
promises.push(this.loadBlob("preload", prop, this.plan.preload[prop]).then((blobData) => {
1066+
for (const [name, resource] of Object.entries(this.plan.preload)) {
1067+
promises.push(this.loadBlob("preload", name, resource).then((blobData) => {
10651068
if (!globalThis.allIsGood)
10661069
return;
10671070
this.preloads.push([ blobData.prop, blobData.blobURL ]);
@@ -1070,7 +1073,7 @@ class Benchmark {
10701073
// We'll try again later in retryPrefetchResourceForBrowser(). Don't throw an error.
10711074
if (!this.failedPreloads)
10721075
this.failedPreloads = { };
1073-
this.failedPreloads[prop] = true;
1076+
this.failedPreloads[name] = true;
10741077
JetStream.counter.failedPreloadResources++;
10751078
}));
10761079
}
@@ -1127,9 +1130,8 @@ class Benchmark {
11271130
}
11281131

11291132
if (this.plan.preload) {
1130-
for (const prop of Object.getOwnPropertyNames(this.plan.preload)) {
1131-
const resource = this.plan.preload[prop];
1132-
const allDone = await this.retryPrefetchResource("preload", prop, resource);
1133+
for (const [name, resource] of Object.entries(this.plan.preload)) {
1134+
const allDone = await this.retryPrefetchResource("preload", name, resource);
11331135
if (allDone)
11341136
return true; // All resources loaded, nothing more to do.
11351137
}
@@ -1149,21 +1151,21 @@ class Benchmark {
11491151
if (!this.plan.preload) {
11501152
return;
11511153
}
1152-
for (let [name, file] of Object.entries(this.plan.preload)) {
1153-
const compressed = isCompressed(file);
1154+
for (let [name, resource] of Object.entries(this.plan.preload)) {
1155+
const compressed = isCompressed(resource);
11541156
if (compressed && !JetStreamParams.prefetchResources) {
1155-
file = uncompressedName(file);
1157+
resource = uncompressedName(resource);
11561158
}
11571159

11581160
if (JetStreamParams.prefetchResources) {
1159-
let bytes = new Int8Array(read(file, "binary"));
1161+
let bytes = new Int8Array(read(resource, "binary"));
11601162
if (compressed) {
11611163
bytes = zlib.decompress(bytes);
11621164
}
1163-
this.shellPrefetchedResources[file] = bytes;
1165+
this.shellPrefetchedResources[resource] = bytes;
11641166
}
11651167

1166-
this.preloads.push([name, file]);
1168+
this.preloads.push([name, resource]);
11671169
}
11681170
}
11691171

@@ -1753,10 +1755,10 @@ class WasmLegacyBenchmark extends Benchmark {
17531755
}
17541756

17551757
str += "};\n";
1756-
1757-
const keys = Object.keys(this.plan.preload);
1758-
for (let i = 0; i < keys.length; ++i) {
1759-
str += `JetStream.loadBlob("${keys[i]}", "${this.plan.preload[keys[i]]}", () => {\n`;
1758+
let preloadCount = 0;
1759+
for (const [name, resource] of Object.entries(this.plan.preload)) {
1760+
preloadCount++;
1761+
str += `JetStream.loadBlob(${JSON.stringify(name)}, "${resource}", () => {\n`;
17601762
}
17611763
if (this.plan.async) {
17621764
str += `doRun().catch((e) => {
@@ -1767,7 +1769,7 @@ class WasmLegacyBenchmark extends Benchmark {
17671769
} else {
17681770
str += `doRun();`
17691771
}
1770-
for (let i = 0; i < keys.length; ++i) {
1772+
for (let i = 0; i < preloadCount; ++i) {
17711773
str += `})`;
17721774
}
17731775
str += `;`;

mobx/dist/LICENSE.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
This file was generated with the generate-license-file npm package!
2+
https://www.npmjs.com/package/generate-license-file
3+
4+
The following npm package may be included in this product:
5+
6+
- mobx@6.13.7
7+
8+
This package contains the following license:
9+
10+
The MIT License (MIT)
11+
12+
Copyright (c) 2015 Michel Weststrate
13+
14+
Permission is hereby granted, free of charge, to any person obtaining a copy
15+
of this software and associated documentation files (the "Software"), to deal
16+
in the Software without restriction, including without limitation the rights
17+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18+
copies of the Software, and to permit persons to whom the Software is
19+
furnished to do so, subject to the following conditions:
20+
21+
The above copyright notice and this permission notice shall be included in all
22+
copies or substantial portions of the Software.
23+
24+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30+
SOFTWARE.
31+
32+
-----------
33+
34+
This file was generated with the generate-license-file npm package!
35+
https://www.npmjs.com/package/generate-license-file

0 commit comments

Comments
 (0)