fix(reader): optimize memory management for canvas and image handling in infinite scrolling (#433)

* fix(reader): optimize memory management for canvas and image handling in infinite scrolling

* fix(reader): improve image loading management and optimize memory usage in infinite scrolling
This commit is contained in:
brios
2026-04-22 17:27:10 -04:00
committed by Zach Yale
parent 4a68fa28f6
commit 376ac6ff60
3 changed files with 87 additions and 7 deletions
@@ -74,7 +74,11 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
private static readonly CONTINUATION_HINT_SHOW_PX = 220;
private static readonly CONTINUATION_HINT_HIDE_PX = 380;
private static readonly LONG_STRIP_BUFFER = 7;
/** Max pages kept in DOM for long strip; pages outside ±EVICTION_WINDOW are removed. */
private static readonly LONG_STRIP_EVICTION_WINDOW = 15;
private static readonly AUTO_CLOSE_MENU_TIMEOUT = 3000;
/** Max pages kept in DOM for infinite scroll; pages outside this window are removed. */
private static readonly INFINITE_SCROLL_MAX_DOM_PAGES = 18;
private destroy$ = new Subject<void>();
private progressSaveSubject$ = new Subject<void>();
@@ -789,8 +793,10 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
const keepUrls = new Set(keepPages.map(p => this.getPageImageUrl(p)));
this.currentImageUrls.forEach(url => keepUrls.add(url));
for (const url of this.preloadedImages.keys()) {
for (const [url, img] of this.preloadedImages.entries()) {
if (!keepUrls.has(url)) {
img.onload = null;
img.onerror = null;
this.preloadedImages.delete(url);
}
}
@@ -1170,6 +1176,7 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
for (let i = lastLoadedIndex + 1; i < endIndex; i++) {
this.infiniteScrollPages.push(i);
}
this.trimInfiniteScrollPages('tail');
this.isLoadingMore = false;
});
}
@@ -1198,6 +1205,7 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
added.push(p);
}
this.infiniteScrollPages = [...added, ...this.infiniteScrollPages];
this.trimInfiniteScrollPages('head');
this.afterNextPaint(() => {
const afterTop = anchorEl.getBoundingClientRect().top;
@@ -1206,6 +1214,22 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
});
}
/**
* Caps the infinite scroll DOM to INFINITE_SCROLL_MAX_DOM_PAGES pages.
* When appending ('tail'), oldest pages at the head are dropped.
* When prepending ('head'), newest pages at the tail are dropped.
*/
private trimInfiniteScrollPages(growingSide: 'head' | 'tail'): void {
const max = CbxReaderComponent.INFINITE_SCROLL_MAX_DOM_PAGES;
if (this.infiniteScrollPages.length <= max) return;
if (growingSide === 'tail') {
this.infiniteScrollPages = this.infiniteScrollPages.slice(-max);
} else {
this.infiniteScrollPages = this.infiniteScrollPages.slice(0, max);
}
}
private updateCurrentPageFromScroll(container: HTMLElement): void {
const images = Array.from(
container.querySelectorAll('.infinite-scroll-wrapper img.page-image[data-page]')
@@ -1300,6 +1324,16 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
}
}
// Evict pages far from the current viewport to cap DOM size
const evictionWindow = CbxReaderComponent.LONG_STRIP_EVICTION_WINDOW;
const keepStart = Math.max(0, pageNum - evictionWindow);
const keepEnd = Math.min(this.pages.length - 1, pageNum + evictionWindow);
this.longStripImages = this.longStripImages.filter(item => {
if (item.page >= keepStart && item.page <= keepEnd) return true;
this.longStripLoadedPages.delete(item.page);
return false;
});
// Keep sorted by page number
this.longStripImages.sort((a, b) => a.page - b.page);
}
@@ -2240,6 +2274,19 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
if (this.autoCloseMenuTimer) {
clearTimeout(this.autoCloseMenuTimer);
}
// Release all preloaded image memory
for (const img of this.preloadedImages.values()) {
img.onload = null;
img.onerror = null;
}
this.preloadedImages.clear();
// Clear infinite scroll DOM references
this.infiniteScrollPages = [];
this.currentImageUrls = [];
this.previousImageUrls = [];
this.destroy$.next();
this.destroy$.complete();
}