Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
resolve: memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangyu committed Oct 18, 2022
1 parent a10c790 commit 91f6587
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
23 changes: 20 additions & 3 deletions addon/chrome/content/previewPDF.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,14 @@
<div id="viewerContainer" class="pdfViewer"></div>

<script>
Components.utils.import("resource://gre/modules/osfile.jsm");

var cachedData = {
itemID: -1,
pdfDocument: undefined,
appAnnotations: [],
viewportWidth: 0,
previewType: -1,
};
var noteIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="__size__" height="__size__" viewBox="0 0 24 24"><polygon fill="__color__" points="0.5 0.5 23.5 0.5 23.5 23.5 11.5 23.5 0.5 12.5 0.5 0.5"></polygon><polygon points="0.5 12.5 11.5 12.5 11.5 23.5 0.5 12.5" fill="#fff" opacity="0.4"></polygon><path d="M0,0V12.707L11.293,24H24V0ZM11,22.293,1.707,13H11ZM23,23H12V12H1V1H23Z"></path></svg>`;
var MIN_SCALE = 0.25;
Expand All @@ -126,6 +129,7 @@
var zoomTime = 0;

function updateToolbar(previewType) {
cachedData.previewType = previewType;
const viewerContainer = document.getElementById("viewerContainer");
const toolbar = document.querySelector(".toolbar");
const showToolbar =
Expand All @@ -134,7 +138,15 @@
(previewType === 2 && Zotero.Prefs.get("pdfpreview.showToolInTab"));
toolbar.style.visibility = showToolbar ? "visible" : "hidden";
toolbar.style.height = showToolbar ? "32px" : "0";
viewerContainer.style.height = showToolbar ? "calc(100% - 32px)" : "inherit";
viewerContainer.style.height = showToolbar
? "calc(100% - 32px)"
: "inherit";
}

async function getBuffer(itemID) {
let path = await Zotero.Items.get(itemID).getFilePathAsync();

This comment has been minimized.

Copy link
@volatile-static

volatile-static Oct 18, 2022

Contributor

为什么await异步版本而不是直接使用同步版本?

This comment has been minimized.

Copy link
@windingwind

windingwind Oct 18, 2022

Owner

参考的读取文件的代码,具体没在意。有啥讲究吗

This comment has been minimized.

Copy link
@volatile-static

volatile-static Oct 18, 2022

Contributor

不清楚,他很多函数都是分两个版本,一个没后缀,一个后缀async。
我只知道没后缀的不用await了

let buf = await OS.File.read(path, {});
return new Uint8Array(buf).buffer;
}

function getScale() {
Expand Down Expand Up @@ -468,13 +480,18 @@
if (cachedData.itemID !== e.data.itemID) {
updateToolbar(e.data.previewType);
// Destroy worker to avoid memory leak
// and increase the previewCount to let the plugin decide
// when to destroy the iframe
// because the buffers are not free even after the destroy()
Zotero.PDFPreview.preview._previewCounts[
cachedData.previewType
] += 1;
cachedData.pdfDocument && cachedData.pdfDocument.destroy();
cachedData.pdfDocument = await pdfjsLib.getDocument({
data: e.data.buffer,
data: await getBuffer(e.data.itemID),
cMapUrl: "resource://zotero/pdf-reader/cmaps/",
cMapPacked: true,
}).promise;
delete e.data.buffer;
cachedData.itemID = e.data.itemID;
cachedData.viewportWidth = e.data.width;
cachedData.appAnnotations = e.data.annotations;
Expand Down
2 changes: 1 addition & 1 deletion src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class AddonEvents extends AddonModule {
});
}

private doPreview(force: boolean = false) {
public doPreview(force: boolean = false) {
this.updatePreviewTab();
const previewType = this.getPreviewType();
console.log(previewType);
Expand Down
36 changes: 28 additions & 8 deletions src/preview.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import PDFPreview from "./addon";
import { PreviewType } from "./base";
import AddonModule from "./module";

Expand All @@ -13,12 +14,26 @@ class Annotation {
this.pageLabel = pageLabel;
}
}

const RELOAD_COUNT = 10;

class AddonPreview extends AddonModule {
item: Zotero.Item;
lastType: PreviewType;
_initPromise: any;
_loadingPromise: any;
_skipRendering: boolean;
_previewCounts: any;

constructor(parent: PDFPreview) {
super(parent);
this._previewCounts = {};
let type = PreviewType.info;
while (type !== PreviewType.null) {
this._previewCounts[type] = 0;
type++;
}
}

public async updatePreviewItem(alwaysUpdate: boolean = false) {
let items = ZoteroPane.getSelectedItems();
Expand Down Expand Up @@ -48,13 +63,6 @@ class AddonPreview extends AddonModule {
return this.item;
}

public async getBuffer() {
console.log(this.item);
let path: string = await this.item.getFilePathAsync();
let buf: any = await OS.File.read(path, {});
return new Uint8Array(buf).buffer;
}

public getPreviewIds(type: PreviewType) {
let iframeId = "";
let containerId = "";
Expand Down Expand Up @@ -112,8 +120,21 @@ class AddonPreview extends AddonModule {
return;
}

if (this._previewCounts[type] >= RELOAD_COUNT) {
this._previewCounts[type] = 0;
await this._Addon.events.initPreview(type);
this._Addon.events.doPreview(true);
return;
}
await this._Addon.preview._initPromise;
let { iframe } = this.getPreviewElements(type);

let t = 0;
while (t < 500 && iframe.contentDocument.readyState !== "complete") {
await Zotero.Promise.delay(10);
t += 10;
}

let item = await this.updatePreviewItem(
type !== this.lastType ||
// @ts-ignore
Expand Down Expand Up @@ -155,7 +176,6 @@ class AddonPreview extends AddonModule {
{
type: "renderPreview",
itemID: this.item.id,
buffer: await this.getBuffer(),
width: width - 40,
annotations: Zotero.Prefs.get("pdfpreview.showAnnotations")
? item
Expand Down

0 comments on commit 91f6587

Please sign in to comment.