Skip to content

Commit

Permalink
Auto merge of #4130 - Turbo87:duplicate-playground-req, r=locks
Browse files Browse the repository at this point in the history
services/playground: Extract loadCrates() method

This avoids duplicate requests to the playground API, caused by the `preloadPlaygroundCratesTask` in the `application` route requesting the data after it has already been loaded by the `CrateSidebar` component too.
  • Loading branch information
bors committed Nov 8, 2021
2 parents fd99c9d + d6b376c commit a658181
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
14 changes: 6 additions & 8 deletions app/components/crate-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ export default class DownloadGraph extends Component {
super(...arguments);

// load Rust Playground crates list, if necessary
if (!this.playground.crates) {
this.playground.loadCratesTask.perform().catch(error => {
if (!(didCancel(error) || error.isServerError || error.isNetworkError)) {
// report unexpected errors to Sentry
this.sentry.captureException(error);
}
});
}
this.playground.loadCrates().catch(error => {
if (!(didCancel(error) || error.isServerError || error.isNetworkError)) {
// report unexpected errors to Sentry
this.sentry.captureException(error);
}
});
}
}
2 changes: 1 addition & 1 deletion app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ export default class ApplicationRoute extends Route {

@task *preloadPlaygroundCratesTask() {
yield rawTimeout(1000);
yield this.playground.loadCratesTask.perform();
yield this.playground.loadCrates();
}
}
6 changes: 6 additions & 0 deletions app/services/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import ajax from '../utils/ajax';
export default class PlaygroundService extends Service {
@alias('loadCratesTask.lastSuccessful.value') crates;

async loadCrates() {
if (!this.crates) {
return this.loadCratesTask.perform();
}
}

@dropTask *loadCratesTask() {
let response = yield ajax('https://play.rust-lang.org/meta/crates');
return response.crates;
Expand Down

0 comments on commit a658181

Please sign in to comment.