Skip to content

Commit

Permalink
routes/crate: Get or fetch requested version by num
Browse files Browse the repository at this point in the history
Previously, we fetched all versions and then found the requested version
within them. This commit changes the approach to either retrieve the
version from loaded versions or fetch it from the endpoint. This change
allows us to migrate to paginated versions in the future.
  • Loading branch information
eth3lbert committed Jan 23, 2025
1 parent 03ebd0b commit 859959f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
26 changes: 20 additions & 6 deletions app/routes/crate/version-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
import { NotFoundError } from '@ember-data/adapter/error';
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default class VersionRoute extends Route {
@service store;
@service router;

async model(params, transition) {
let crate = this.modelFor('crate');

let versions;
// TODO: Resolved version without waiting for versions to be resolved
try {
versions = await crate.loadVersionsTask.perform();
await 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 requestedVersion = params.version_num;
let version = versions.find(version => version.num === requestedVersion);
if (!version) {
let title = `${crate.name}: Version ${requestedVersion} not found`;
return this.router.replaceWith('catch-all', { transition, title });
let version;
try {
version =
crate.loadedVersionsByNum.get(requestedVersion) ??
(await this.store.queryRecord('version', {
name: crate.id,
num: requestedVersion,
}));
} catch (error) {
if (error instanceof NotFoundError) {
let title = `${crate.name}: Version ${requestedVersion} not found`;
return this.router.replaceWith('catch-all', { transition, title });
} else {
let title = `${crate.name}: Failed to load version data`;
return this.router.replaceWith('catch-all', { transition, error, title, tryAgain: true });
}
}

try {
Expand Down
36 changes: 31 additions & 5 deletions app/routes/crate/version.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NotFoundError } from '@ember-data/adapter/error';
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { waitForPromise } from '@ember/test-waiters';
Expand All @@ -10,10 +11,12 @@ import { AjaxError } from '../../utils/ajax';
export default class VersionRoute extends Route {
@service router;
@service sentry;
@service store;

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();
Expand All @@ -25,14 +28,37 @@ export default class VersionRoute extends Route {
let version;
let requestedVersion = params.version_num;
if (requestedVersion) {
version = versions.find(version => version.num === requestedVersion);
if (!version) {
let title = `${crate.name}: Version ${requestedVersion} not found`;
return this.router.replaceWith('catch-all', { transition, title });
try {
version =
crate.loadedVersionsByNum.get(requestedVersion) ??
(await this.store.queryRecord('version', {
name: crate.id,
num: requestedVersion,
}));
} catch (error) {
if (error instanceof NotFoundError) {
let title = `${crate.name}: Version ${requestedVersion} not found`;
return this.router.replaceWith('catch-all', { transition, title });
} else {
let title = `${crate.name}: Failed to load version data`;
return this.router.replaceWith('catch-all', { transition, error, title, tryAgain: true });
}
}
} else {
let { default_version } = crate;
version = versions.find(version => version.num === default_version);
version =
crate.loadedVersionsByNum.get(default_version) ??
(await this.store
.queryRecord('version', {
name: crate.id,
num: default_version,
})
.catch(error => {
if (!(error instanceof NotFoundError)) {
let title = `${crate.name}: Failed to load version data`;
this.router.replaceWith('catch-all', { transition, error, title, tryAgain: true });
}
}));

if (!version) {
let versionNums = versions.map(it => it.num);
Expand Down

0 comments on commit 859959f

Please sign in to comment.