From 03013043159cf4aa8ed8fd4ceea1d55772a06ebe Mon Sep 17 00:00:00 2001 From: Saleh Yusefnejad Date: Sat, 11 Jan 2025 22:59:53 +0330 Subject: [PATCH 1/2] add missing dispose process to BitPdfReader #9647 --- .../PdfReader/BitPdfReader.razor.cs | 16 ++++++--- .../Components/PdfReader/BitPdfReader.ts | 36 +++++++++++-------- .../BitPdfReaderJsRuntimeExtensions.cs | 9 +++-- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReader.razor.cs b/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReader.razor.cs index 736a1ac3d7..f4067bcb0a 100644 --- a/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReader.razor.cs +++ b/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReader.razor.cs @@ -5,7 +5,7 @@ namespace Bit.BlazorUI; /// /// BitPdfReader is a simple pdf renderer utilizing the pdfjs library to bring pdf reading feature into Blazor world. /// -public partial class BitPdfReader +public partial class BitPdfReader : IAsyncDisposable { private bool _allPageRendered; private int _numberOfPages = 1; @@ -36,7 +36,7 @@ public partial class BitPdfReader /// /// The configuration of the pdf reader (). /// - [Parameter] public BitPdfReaderConfig Config { get; set; } + [Parameter] public BitPdfReaderConfig Config { get; set; } = new(); /// /// Renders the pages horizontally. @@ -69,6 +69,7 @@ public partial class BitPdfReader [Parameter] public string? Style { get; set; } + /// /// Re-renders the provided page number or the current page. /// @@ -174,9 +175,9 @@ protected override async Task OnAfterRenderAsync(bool firstRender) "_content/Bit.BlazorUI.Extras/pdf.js/pdfjs-4.7.76-worker.js" ]; - await _js.BitPdfReaderInitPdfJs(scripts); + await _js.BitPdfReaderInit(scripts); - _numberOfPages = await _js.BitPdfReaderSetupPdfDoc(Config); + _numberOfPages = await _js.BitPdfReaderSetup(Config); await OnPdfLoaded.InvokeAsync(); @@ -243,4 +244,11 @@ private async Task RefreshAllPages() await OnPdfPageRendered.InvokeAsync(); } + + + + public async ValueTask DisposeAsync() + { + await _js.BitPdfReaderDispose(Config.Id); + } } diff --git a/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReader.ts b/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReader.ts index 450b9a0d08..096a64f9a8 100644 --- a/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReader.ts +++ b/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReader.ts @@ -1,17 +1,3 @@ -declare type PdfJsLib = { - getDocument: (src?: string | URL | TypedArray | ArrayBuffer | DocumentInitParameters) => PDFDocumentLoadingTask; - GlobalWorkerOptions: GlobalWorkerOptions -} - -declare type BitPdfReaderConfig = { - id: string; - url: string; - scale: number; - autoScale: boolean; - pdfDoc?: PDFDocumentProxy; - isRendering: boolean[]; -} - namespace BitBlazorUI { export class PdfReader { private static _initPromise?: Promise; @@ -116,5 +102,27 @@ namespace BitBlazorUI { config.isRendering[pageNumber] = false; } } + + public static dispose(id: string) { + const config = PdfReader._bitPdfReaders.get(id); + if (!config) return; + + config.pdfDoc?.destroy(); + PdfReader._bitPdfReaders.delete(id); + } } +} + +declare type PdfJsLib = { + getDocument: (src?: string | URL | TypedArray | ArrayBuffer | DocumentInitParameters) => PDFDocumentLoadingTask; + GlobalWorkerOptions: GlobalWorkerOptions +} + +declare type BitPdfReaderConfig = { + id: string; + url: string; + scale: number; + autoScale: boolean; + pdfDoc?: PDFDocumentProxy; + isRendering: boolean[]; } \ No newline at end of file diff --git a/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReaderJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReaderJsRuntimeExtensions.cs index 07586dedf3..a085bf673d 100644 --- a/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReaderJsRuntimeExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReaderJsRuntimeExtensions.cs @@ -2,12 +2,12 @@ internal static class BitPdfReaderJsRuntimeExtensions { - public static ValueTask BitPdfReaderInitPdfJs(this IJSRuntime jsRuntime, IEnumerable scripts) + public static ValueTask BitPdfReaderInit(this IJSRuntime jsRuntime, IEnumerable scripts) { return jsRuntime.InvokeVoidAsync("BitBlazorUI.PdfReader.init", scripts); } - public static ValueTask BitPdfReaderSetupPdfDoc(this IJSRuntime jsRuntime, BitPdfReaderConfig config) + public static ValueTask BitPdfReaderSetup(this IJSRuntime jsRuntime, BitPdfReaderConfig config) { return jsRuntime.InvokeAsync("BitBlazorUI.PdfReader.setup", config); } @@ -21,4 +21,9 @@ public static ValueTask BitPdfReaderRefreshPage(this IJSRuntime jsRuntime, BitPd { return jsRuntime.InvokeVoidAsync("BitBlazorUI.PdfReader.refreshPage", config, pageNumber); } + + public static ValueTask BitPdfReaderDispose(this IJSRuntime jsRuntime, string id) + { + return jsRuntime.InvokeVoidAsync("BitBlazorUI.PdfReader.refreshPage", id); + } } From d6a15e6d8daf318ffe8849f3644b75de26729e7d Mon Sep 17 00:00:00 2001 From: Saleh Yusefnejad Date: Sat, 11 Jan 2025 23:11:54 +0330 Subject: [PATCH 2/2] fix reivew --- .../Components/PdfReader/BitPdfReaderJsRuntimeExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReaderJsRuntimeExtensions.cs b/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReaderJsRuntimeExtensions.cs index a085bf673d..cbd5da3125 100644 --- a/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReaderJsRuntimeExtensions.cs +++ b/src/BlazorUI/Bit.BlazorUI.Extras/Components/PdfReader/BitPdfReaderJsRuntimeExtensions.cs @@ -24,6 +24,6 @@ public static ValueTask BitPdfReaderRefreshPage(this IJSRuntime jsRuntime, BitPd public static ValueTask BitPdfReaderDispose(this IJSRuntime jsRuntime, string id) { - return jsRuntime.InvokeVoidAsync("BitBlazorUI.PdfReader.refreshPage", id); + return jsRuntime.InvokeVoidAsync("BitBlazorUI.PdfReader.dispose", id); } }