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