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:
@@ -100,7 +100,7 @@
|
||||
<div class="strip-width-constrain" [style.width]="cbxQuickSettingsState().stripMaxWidthPercent + '%'">
|
||||
@for (item of longStripImages; track item.page) {
|
||||
<img [src]="item.src" alt="Page {{ item.page + 1 }}" class="long-strip-image" [attr.data-page]="item.page"
|
||||
draggable="false" (load)="onLongStripImageLoad($event, item.page)" />
|
||||
draggable="false" [attr.loading]="item.page === currentPage ? 'eager' : 'lazy'" decoding="async" (load)="onLongStripImageLoad($event, item.page)" />
|
||||
}
|
||||
@if (nextBookInSeries) {
|
||||
<div class="cbx-continuation-spacer" aria-hidden="true"></div>
|
||||
@@ -125,6 +125,7 @@
|
||||
@for (pageIdx of infiniteScrollPages; track pageIdx) {
|
||||
<img [src]="getPageImageUrl(pageIdx)" alt="Page {{ pageIdx + 1 }}" class="page-image"
|
||||
[attr.data-page]="pageIdx" role="button" tabindex="0" draggable="false"
|
||||
[attr.loading]="pageIdx === currentPage ? 'eager' : 'lazy'" decoding="async"
|
||||
(click)="onImageClick()" (keydown.enter)="onImageClick()" (dblclick)="onImageDoubleClick()"
|
||||
(load)="onPageImageLoad($event, pageIdx)" />
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
+37
-5
@@ -1,4 +1,4 @@
|
||||
import {Component, Input, OnChanges, SimpleChanges, ViewChild, ElementRef, AfterViewInit} from '@angular/core';
|
||||
import {Component, Input, OnChanges, OnDestroy, SimpleChanges, ViewChild, ElementRef, AfterViewInit} from '@angular/core';
|
||||
import {CommonModule} from '@angular/common';
|
||||
import {CbxPageSplitOption} from '../../../settings/user-management/user.service';
|
||||
|
||||
@@ -42,7 +42,7 @@ export type CanvasSplitState = 'NO_SPLIT' | 'LEFT_PART' | 'RIGHT_PART';
|
||||
}
|
||||
`]
|
||||
})
|
||||
export class CanvasRendererComponent implements OnChanges, AfterViewInit {
|
||||
export class CanvasRendererComponent implements OnChanges, AfterViewInit, OnDestroy {
|
||||
@Input() imageUrl = '';
|
||||
@Input() splitState: CanvasSplitState = 'NO_SPLIT';
|
||||
@Input() splitOption: CbxPageSplitOption = CbxPageSplitOption.NO_SPLIT;
|
||||
@@ -51,6 +51,7 @@ export class CanvasRendererComponent implements OnChanges, AfterViewInit {
|
||||
|
||||
imageLoaded = false;
|
||||
private currentImage: HTMLImageElement | null = null;
|
||||
private pendingImage: HTMLImageElement | null = null;
|
||||
|
||||
private static readonly MAX_CANVAS_SAFARI = 4096;
|
||||
private static readonly MAX_CANVAS_OTHER = 16384;
|
||||
@@ -61,6 +62,22 @@ export class CanvasRendererComponent implements OnChanges, AfterViewInit {
|
||||
}
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
// Release canvas memory
|
||||
const canvas = this.canvasRef?.nativeElement;
|
||||
if (canvas) {
|
||||
canvas.width = 0;
|
||||
canvas.height = 0;
|
||||
}
|
||||
this.cancelPendingImage();
|
||||
if (this.currentImage) {
|
||||
this.currentImage.onload = null;
|
||||
this.currentImage.onerror = null;
|
||||
this.currentImage.src = '';
|
||||
this.currentImage = null;
|
||||
}
|
||||
}
|
||||
|
||||
ngOnChanges(changes: SimpleChanges): void {
|
||||
if (changes['imageUrl'] || changes['splitState'] || changes['splitOption']) {
|
||||
if (changes['imageUrl'] && this.currentImage?.src !== this.imageUrl) {
|
||||
@@ -74,17 +91,32 @@ export class CanvasRendererComponent implements OnChanges, AfterViewInit {
|
||||
private loadAndDraw(): void {
|
||||
if (!this.imageUrl) return;
|
||||
|
||||
this.cancelPendingImage();
|
||||
|
||||
this.imageLoaded = false;
|
||||
const img = new Image();
|
||||
img.crossOrigin = 'anonymous';
|
||||
this.pendingImage = img;
|
||||
img.onload = () => {
|
||||
this.currentImage = img;
|
||||
this.drawImage(img);
|
||||
this.imageLoaded = true;
|
||||
if (this.pendingImage === img) {
|
||||
this.currentImage = img;
|
||||
this.drawImage(img);
|
||||
this.imageLoaded = true;
|
||||
this.pendingImage = null;
|
||||
}
|
||||
};
|
||||
img.src = this.imageUrl;
|
||||
}
|
||||
|
||||
private cancelPendingImage(): void {
|
||||
if (this.pendingImage) {
|
||||
this.pendingImage.onload = null;
|
||||
this.pendingImage.onerror = null;
|
||||
this.pendingImage.src = '';
|
||||
this.pendingImage = null;
|
||||
}
|
||||
}
|
||||
|
||||
private drawImage(img: HTMLImageElement): void {
|
||||
const canvas = this.canvasRef?.nativeElement;
|
||||
if (!canvas) return;
|
||||
|
||||
Reference in New Issue
Block a user