Skip to content

Commit

Permalink
routes/crate: Avoid awaiting versions unless they are required
Browse files Browse the repository at this point in the history
  • Loading branch information
eth3lbert committed Jan 8, 2025
1 parent ddfede2 commit a03882a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
12 changes: 11 additions & 1 deletion app/controllers/crate/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default class CrateVersionController extends Controller {
this.stackedGraph = false;
}

@alias('loadDownloadsTask.last.value') downloads;
@alias('model.crate') crate;
@alias('model.requestedVersion') requestedVersion;
@alias('model.version') currentVersion;
Expand Down Expand Up @@ -64,6 +63,17 @@ export default class CrateVersionController extends Controller {
return readme;
});

/** @return {import("../../models/version-download").default | null} */
get downloads() {
let crate = this.crate;
let downloads = this.loadDownloadsTask.last?.value;
if (!downloads || !crate.versionsObj) {
return null;
}
let allLoaded = downloads.every(d => !!crate.versionsObj[d.belongsTo('version').id()]);
if (allLoaded) return downloads;
}

// This task would be `perform()` in setupController
loadDownloadsTask = task(async () => {
let downloads = await this.downloadsContext.version_downloads;
Expand Down
9 changes: 3 additions & 6 deletions app/routes/crate/version-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ export default class VersionRoute extends Route {
return version;
}

async afterModel(_resolvedModel, transition) {
afterModel(_resolvedModel, transition) {
let crate = this.modelFor('crate');
// TODO: Resolved version without waiting for versions to be resolved
try {
await crate.loadVersionsTask.perform();
} catch (error) {
crate.loadVersionsTask.perform().catch(error => {
let title = `${crate.name}: Failed to load version data`;
return this.router.replaceWith('catch-all', { transition, error, title, tryAgain: true });
}
});
}

setupController(controller, model) {
Expand Down
9 changes: 3 additions & 6 deletions app/routes/crate/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ export default class VersionRoute extends Route {
async model(params, transition) {
let crate = this.modelFor('crate');

// TODO: Resolved version without waiting for versions to be resolved
let versions;
try {
versions = await crate.loadVersionsTask.perform();
} catch (error) {
let versions = crate.loadVersionsTask.perform().catch(error => {
let title = `${crate.name}: Failed to load version data`;
return this.router.replaceWith('catch-all', { transition, error, title, tryAgain: true });
}
});

let version;
let requestedVersion = params.version_num;
Expand Down Expand Up @@ -56,6 +52,7 @@ export default class VersionRoute extends Route {
}));

if (!version) {
versions = await versions;
let versionNums = versions.map(it => it.num);
semverSort(versionNums, { loose: true });

Expand Down

0 comments on commit a03882a

Please sign in to comment.