Skip to content

Commit

Permalink
Merge pull request #224 from Akxe/fix-memory-leak
Browse files Browse the repository at this point in the history
Fix memory leak
  • Loading branch information
codehippie1 authored Jul 31, 2023
2 parents 034164d + cd32e60 commit 45f2a87
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/src/ng2-pdfjs-viewer.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Component, Input, Output, ViewChild, EventEmitter, ElementRef } from '@angular/core';
import { Component, Input, Output, OnInit, ViewChild, EventEmitter, ElementRef } from '@angular/core';

@Component({
selector: 'ng2-pdfjs-viewer',
template: `<iframe title="ng2-pdfjs-viewer" [hidden]="externalWindow || (!externalWindow && !pdfSrc)" #iframe width="100%" height="100%"></iframe>`
})
export class PdfJsViewerComponent {
export class PdfJsViewerComponent implements OnInit {
@ViewChild('iframe', { static: true }) iframe: ElementRef;
static lastID = 0;
@Input() public viewerId = `ng2-pdfjs-viewer-ID${++lastID}`;
Expand Down Expand Up @@ -135,6 +135,7 @@ export class PdfJsViewerComponent {
this.loadPdf();
}

private relaseUrl?: () => void; // Avoid memory leask with `URL.createObjectURL`
private loadPdf() {
if (!this._src) {
return;
Expand Down Expand Up @@ -180,15 +181,20 @@ export class PdfJsViewerComponent {
}
}

this.relaseUrl?.();
let fileUrl;
//if (typeof this.src === "string") {
// fileUrl = this.src;
//}
if (this._src instanceof Blob) {
fileUrl = encodeURIComponent(URL.createObjectURL(this._src));
const url = URL.createObjectURL(this._src);
fileUrl = encodeURIComponent(url);
this.relaseUrl = () => URL.revokeObjectURL(url);
} else if (this._src instanceof Uint8Array) {
let blob = new Blob([this._src], { type: "application/pdf" });
fileUrl = encodeURIComponent(URL.createObjectURL(blob));
const url = createObjectURL(blob);
this.relaseUrl = () => URL.revokeObjectURL(url);
fileUrl = encodeURIComponent(url);
} else {
fileUrl = this._src;
}
Expand Down

0 comments on commit 45f2a87

Please sign in to comment.