-
Notifications
You must be signed in to change notification settings - Fork 6
Refactor: optimize notes covers rendering #356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
46030ef
c359154
5022ddf
ba4bd12
a870d20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,17 +69,62 @@ export default function (onlyCreatedByUser = false): UseNoteListComposableState | |
| const isLoading = ref(false); | ||
|
|
||
| /** | ||
| * Get note list | ||
| * Get note list (metadata only, covers are not downloaded) | ||
| * @param page - number of pages | ||
| */ | ||
| const load = async (page: number): Promise<NoteList> => { | ||
| isLoading.value = true; | ||
| try { | ||
| return await noteListService.getNoteList(page, onlyCreatedByUser); | ||
| } finally { | ||
| isLoading.value = false; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Load cover images for all notes in the list in the background | ||
| * Updates each note's cover reactively as it arrives | ||
| */ | ||
| const loadCovers = async (): Promise<void> => { | ||
| if (isEmpty(noteList.value)) { | ||
| return; | ||
| } | ||
|
|
||
| const list = await noteListService.getNoteList(page, onlyCreatedByUser); | ||
| const list = noteList.value; | ||
| const items = list.items; | ||
|
|
||
| isLoading.value = false; | ||
| await Promise.all(items.map(async (item, index) => { | ||
| /** | ||
| * If cover is null, the note has no cover image | ||
| */ | ||
| if (item.cover === null) { | ||
| return; | ||
| } | ||
|
|
||
| /** | ||
| * If cover is already a blob URL, it was already loaded | ||
| */ | ||
| if (item.cover.startsWith('blob:')) { | ||
| return; | ||
| } | ||
|
|
||
| return list; | ||
| const url = await noteListService.loadCover(item.id, item.cover); | ||
|
|
||
| if (url !== null) { | ||
| const currentItem = list.items[index]; | ||
|
|
||
| if (currentItem?.id !== item.id) { | ||
| return; | ||
| } | ||
| /** | ||
| * Update the specific note's cover reactively so the card renders the image | ||
| */ | ||
| list.items[index] = { | ||
| ...currentItem, | ||
| cover: url, | ||
| }; | ||
| } | ||
| })); | ||
| }; | ||
|
Comment on lines
+88
to
128
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Might be worth tracking in-flight note ids or only passing the newly-appended items into |
||
|
|
||
| /** | ||
|
|
@@ -102,6 +147,12 @@ export default function (onlyCreatedByUser = false): UseNoteListComposableState | |
| } else { | ||
| noteList.value = loadedNotes; | ||
| } | ||
|
|
||
| /** | ||
| * Kick off cover downloads in the background | ||
| * List is already rendered, covers will appear one by one as they load | ||
| */ | ||
| loadCovers().catch(console.error); | ||
| }; | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.