From a03882a560fd50f147502b9d90b504fd248377bc Mon Sep 17 00:00:00 2001 From: eth3lbert Date: Wed, 8 Jan 2025 22:47:07 +0800 Subject: [PATCH] routes/crate: Avoid awaiting versions unless they are required --- app/controllers/crate/version.js | 12 +++++++++++- app/routes/crate/version-dependencies.js | 9 +++------ app/routes/crate/version.js | 9 +++------ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/app/controllers/crate/version.js b/app/controllers/crate/version.js index d9aa81e6511..621862843b9 100644 --- a/app/controllers/crate/version.js +++ b/app/controllers/crate/version.js @@ -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; @@ -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; diff --git a/app/routes/crate/version-dependencies.js b/app/routes/crate/version-dependencies.js index 94f5999f262..8410761edc2 100644 --- a/app/routes/crate/version-dependencies.js +++ b/app/routes/crate/version-dependencies.js @@ -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) { diff --git a/app/routes/crate/version.js b/app/routes/crate/version.js index d463978bea6..3e8a1457280 100644 --- a/app/routes/crate/version.js +++ b/app/routes/crate/version.js @@ -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; @@ -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 });