wasm: Always call freeDocument before opening a new file.
authorTor Andersson <[email protected]>
Thu, 17 Sep 2020 12:32:55 +0000 (14:32 +0200)
committerTor Andersson <[email protected]>
Thu, 17 Sep 2020 15:15:26 +0000 (17:15 +0200)
If fetch() throws an error, we don't want the old document lying around.

platform/wasm/view.html

index dff7f56436959abe1e39a7ac8e196b4b231b362a..e09bff9f01fa6e76ca4d8f0e946e9783780047a6 100644 (file)
@@ -369,6 +369,7 @@ function showPageError(where, page, error) {
 }
 
 async function openURL(url) {
+       freeDocument();
        try {
                let response = await fetch(url);
                if (!response.ok)
@@ -380,13 +381,14 @@ async function openURL(url) {
 }
 
 function openFile(file) {
+       freeDocument();
        if (file instanceof File) {
                initDocument(file, file.name)
                        .catch(error => showDocumentError("initDocument", error));
        }
 }
 
-async function initDocument(blob, magic) {
+function freeDocument() {
        if (doc) {
                mupdf.freeDocument(doc);
                doc = 0;
@@ -395,10 +397,11 @@ async function initDocument(blob, magic) {
                dirty = [];
                searchDirty = [];
        }
-
        emptyNode(document.getElementById("pages"));
        emptyNode(document.getElementById("outline"));
+}
 
+async function initDocument(blob, magic) {
        let data = await blob.arrayBuffer();
        doc = await mupdf.openDocument(data, magic);
        pageCount = await mupdf.countPages(doc);