Skip to content

Commit

Permalink
Improve RecentlyViewedDocs/Dashboard performance (#318)
Browse files Browse the repository at this point in the history
* Improve RecentlyViewedDocs fetching

* Documentation tweak
  • Loading branch information
jeffdaley authored Sep 1, 2023
1 parent 8998855 commit 6d1bae1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
25 changes: 19 additions & 6 deletions web/app/routes/authenticated/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,26 @@ export default class DashboardRoute extends Route {
return result.hits as HermesDocument[];
});

try {
/**
* If the user is loading the dashboard for the first time,
* we await the `fetchAll` task so we can display the
* documents at the same time as the rest of the layout.
*
* If a user leaves and returns to the dashboard, we call `fetchAll`
* but don't await it. This speeds up the transition and is especially
* efficient when the RecentDocs list hasn't changed since its last load.
*
* It's possible for the user to see the RecentDocs list update
* in real time (by visiting a document and very quickly clicking back),
* but unlikely, since we call `fetchAll` in the `/document` route,
* just after the doc is marked viewed. In most cases, the task will be
* finished by the time the user returns to the dashboard.
*
*/
if (this.recentDocs.all) {
void this.recentDocs.fetchAll.perform();
} else {
await this.recentDocs.fetchAll.perform();
} catch {
/**
* This tells our template to show the error state.
*/
this.recentDocs.all = null;
}

return docsAwaitingReview;
Expand Down
4 changes: 4 additions & 0 deletions web/app/routes/authenticated/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export default class DocumentRoute extends Route {
}
}

// With the document fetched and added to the db's RecentlyViewedDocs index,
// make a background call to update the front-end index.
void this.recentDocs.fetchAll.perform();

if (!!doc.createdTime) {
doc.createdDate = parseDate(doc.createdTime * 1000, "long");
}
Expand Down
5 changes: 3 additions & 2 deletions web/app/services/recently-viewed-docs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Service from "@ember/service";
import { inject as service } from "@ember/service";
import { enqueueTask, restartableTask } from "ember-concurrency";
import { keepLatestTask } from "ember-concurrency";
import FetchService from "./fetch";
import { tracked } from "@glimmer/tracking";
import { HermesDocument } from "hermes/types/document";
Expand Down Expand Up @@ -41,7 +41,7 @@ export default class RecentlyViewedDocsService extends Service {
* Fetches an array of recently viewed docs.
* Called in the dashboard route if the docs are not already loaded.
*/
fetchAll = restartableTask(async () => {
fetchAll = keepLatestTask(async () => {
try {
/**
* Fetch the file IDs from the backend.
Expand Down Expand Up @@ -99,6 +99,7 @@ export default class RecentlyViewedDocsService extends Service {
*/
this.all = newAll;
} catch (e: unknown) {
this.all = null; // Causes the dashboard to show an error message.
console.error("Error fetching recently viewed docs", e);
throw e;
}
Expand Down

0 comments on commit 6d1bae1

Please sign in to comment.