chore(pdf-reader): drop redundant pdf in-memory caching (#1754)
This commit is contained in:
@@ -5,7 +5,6 @@ import {PdfReaderComponent} from './pdf-reader.component';
|
||||
interface PdfReaderHarness {
|
||||
embedPdfIframe: {contentWindow: {postMessage: ReturnType<typeof vi.fn>}} | null;
|
||||
embedPdfSaveResolve?: (buffer: ArrayBuffer | null) => void;
|
||||
cachedPdfBuffer: ArrayBuffer | null;
|
||||
pdfBlobUrl: string | null;
|
||||
authService: {getInternalAccessToken: () => string | null};
|
||||
cacheStorageService: {delete: ReturnType<typeof vi.fn>};
|
||||
@@ -13,10 +12,6 @@ interface PdfReaderHarness {
|
||||
saveEmbedPdfDocument: () => Promise<boolean>;
|
||||
}
|
||||
|
||||
function bytes(buffer: ArrayBuffer): number[] {
|
||||
return Array.from(new Uint8Array(buffer));
|
||||
}
|
||||
|
||||
function makeComponent(savedBuffer: ArrayBuffer): PdfReaderHarness {
|
||||
const component = Object.create(PdfReaderComponent.prototype) as PdfReaderHarness;
|
||||
component.embedPdfIframe = {
|
||||
@@ -26,7 +21,6 @@ function makeComponent(savedBuffer: ArrayBuffer): PdfReaderHarness {
|
||||
})
|
||||
}
|
||||
};
|
||||
component.cachedPdfBuffer = new Uint8Array([9, 9, 9]).buffer;
|
||||
component.pdfBlobUrl = 'blob:old-pdf';
|
||||
component.authService = {getInternalAccessToken: () => null};
|
||||
component.cacheStorageService = {delete: vi.fn(() => Promise.resolve(true))};
|
||||
@@ -76,8 +70,6 @@ describe('PdfReaderComponent save handling', () => {
|
||||
|
||||
await expect(save).resolves.toBe(true);
|
||||
|
||||
expect(bytes(component.cachedPdfBuffer!)).toEqual([4, 5, 6]);
|
||||
expect(component.cachedPdfBuffer).not.toBe(savedBuffer);
|
||||
expect(revokeObjectUrl).toHaveBeenCalledWith('blob:old-pdf');
|
||||
expect(createObjectUrl).toHaveBeenCalledTimes(1);
|
||||
expect(component.pdfBlobUrl).toBe('blob:saved-pdf');
|
||||
|
||||
@@ -124,7 +124,6 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
|
||||
private initTimeout?: ReturnType<typeof setTimeout>;
|
||||
private isInitializingBookViewer = false;
|
||||
private pdfFetchAbortController?: AbortController;
|
||||
private cachedPdfBuffer: ArrayBuffer | null = null;
|
||||
private suppressProgressSave = false;
|
||||
private initialPage = 1;
|
||||
private pendingDocTargetPage: number | null = null;
|
||||
@@ -487,16 +486,6 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
|
||||
this.embedPdfBook.setPanMode(true);
|
||||
}
|
||||
|
||||
// Cache the raw PDF bytes so the doc-viewer switch never needs a network request
|
||||
if (!this.cachedPdfBuffer) {
|
||||
const blobSrc = this.pdfBlobUrl || (this.bookData.startsWith('blob:') ? this.bookData : null);
|
||||
if (blobSrc) {
|
||||
fetch(blobSrc).then(r => r.arrayBuffer()).then(buf => {
|
||||
this.cachedPdfBuffer = buf;
|
||||
}).catch(() => { /* caching is best-effort */ });
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize bookmark service early so toggleBookmark works before documentOpened$
|
||||
this.pdfBookmarkService.initialize(this.bookId);
|
||||
|
||||
@@ -979,24 +968,21 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
|
||||
try {
|
||||
let pdfBuffer: ArrayBuffer;
|
||||
|
||||
if (this.cachedPdfBuffer) {
|
||||
// Use the in-memory cache — completely network-free
|
||||
pdfBuffer = this.cachedPdfBuffer.slice(0);
|
||||
const source = this.pdfBlobUrl || this.bookData;
|
||||
if (source.startsWith('blob:')) {
|
||||
const res = await fetch(source);
|
||||
pdfBuffer = await res.arrayBuffer();
|
||||
} else {
|
||||
const source = this.pdfBlobUrl || this.bookData;
|
||||
if (source.startsWith('blob:')) {
|
||||
const res = await fetch(source);
|
||||
pdfBuffer = await res.arrayBuffer();
|
||||
} else {
|
||||
const headers: Record<string, string> = {};
|
||||
const token = this.authService.getInternalAccessToken();
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
const response = await fetch(source, { headers, credentials: 'include' });
|
||||
if (!response.ok) throw new Error(`PDF fetch failed: ${response.status}`);
|
||||
pdfBuffer = await response.arrayBuffer();
|
||||
const headers: Record<string, string> = {};
|
||||
const token = this.authService.getInternalAccessToken();
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
const response = await fetch(source, {headers, credentials: 'include'});
|
||||
if (!response.ok) {
|
||||
throw new Error(`PDF fetch failed: ${response.status}`);
|
||||
}
|
||||
pdfBuffer = await response.arrayBuffer();
|
||||
}
|
||||
|
||||
if (this.viewerMode() !== 'document') return;
|
||||
@@ -1159,7 +1145,6 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
|
||||
|
||||
private updateSavedPdfCache(buffer: ArrayBuffer): void {
|
||||
const savedBuffer = buffer.slice(0);
|
||||
this.cachedPdfBuffer = savedBuffer;
|
||||
this.revokePdfBlobUrl();
|
||||
this.pdfBlobUrl = URL.createObjectURL(new Blob([savedBuffer], { type: 'application/pdf' }));
|
||||
}
|
||||
@@ -1317,7 +1302,6 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
|
||||
this.touchCleanup?.();
|
||||
|
||||
this.revokePdfBlobUrl();
|
||||
this.cachedPdfBuffer = null;
|
||||
if (this.bookData?.startsWith('blob:')) {
|
||||
URL.revokeObjectURL(this.bookData);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user