Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing dispose process to BitPdfReader (#9647) #9661

Merged
merged 2 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Bit.BlazorUI;
/// <summary>
/// BitPdfReader is a simple pdf renderer utilizing the pdfjs library to bring pdf reading feature into Blazor world.
/// </summary>
public partial class BitPdfReader
public partial class BitPdfReader : IAsyncDisposable
{
private bool _allPageRendered;
private int _numberOfPages = 1;
Expand Down Expand Up @@ -36,7 +36,7 @@ public partial class BitPdfReader
/// <summary>
/// The configuration of the pdf reader (<see cref="BitPdfReaderConfig"/>).
/// </summary>
[Parameter] public BitPdfReaderConfig Config { get; set; }
[Parameter] public BitPdfReaderConfig Config { get; set; } = new();

/// <summary>
/// Renders the pages horizontally.
Expand Down Expand Up @@ -69,6 +69,7 @@ public partial class BitPdfReader
[Parameter] public string? Style { get; set; }



/// <summary>
/// Re-renders the provided page number or the current page.
/// </summary>
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -243,4 +244,11 @@ private async Task RefreshAllPages()

await OnPdfPageRendered.InvokeAsync();
}



public async ValueTask DisposeAsync()
{
await _js.BitPdfReaderDispose(Config.Id);
}
msynk marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -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<unknown>;
Expand Down Expand Up @@ -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[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

internal static class BitPdfReaderJsRuntimeExtensions
{
public static ValueTask BitPdfReaderInitPdfJs(this IJSRuntime jsRuntime, IEnumerable<string> scripts)
public static ValueTask BitPdfReaderInit(this IJSRuntime jsRuntime, IEnumerable<string> scripts)
{
return jsRuntime.InvokeVoidAsync("BitBlazorUI.PdfReader.init", scripts);
}

public static ValueTask<int> BitPdfReaderSetupPdfDoc(this IJSRuntime jsRuntime, BitPdfReaderConfig config)
public static ValueTask<int> BitPdfReaderSetup(this IJSRuntime jsRuntime, BitPdfReaderConfig config)
{
return jsRuntime.InvokeAsync<int>("BitBlazorUI.PdfReader.setup", config);
}
Expand All @@ -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.dispose", id);
}
}
Loading