From 2e5dda77e009c57d0591077371eac9125095fb39 Mon Sep 17 00:00:00 2001 From: John Factotum <50942278+johnfactotum@users.noreply.github.com> Date: Thu, 1 Jun 2023 01:44:00 +0800 Subject: [PATCH] Unload section when destroying the renderer --- paginator.js | 9 ++++++++- view.js | 13 +++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/paginator.js b/paginator.js index df12574..41e1597 100644 --- a/paginator.js +++ b/paginator.js @@ -151,6 +151,7 @@ const makeMarginals = (length, part) => Array.from({ length }, () => { }) class View { + #observer = new ResizeObserver(() => this.expand()) #element = document.createElement('div') #iframe = document.createElement('iframe') #contentRange = document.createRange() @@ -212,7 +213,7 @@ class View { const layout = beforeRender?.({ vertical, rtl, background }) this.#iframe.style.display = 'block' this.render(layout) - new ResizeObserver(() => this.expand()).observe(doc.body) + this.#observer.observe(doc.body) // the resize observer above doesn't work in Firefox // (see https://bugzilla.mozilla.org/show_bug.cgi?id=1832939) @@ -352,6 +353,9 @@ class View { get overlayer() { return this.#overlayer } + destroy() { + if (this.document) this.#observer.unobserve(this.document.body) + } } // NOTE: everything here assumes the so-called "negative scroll type" for RTL @@ -941,6 +945,9 @@ export class Paginator extends HTMLElement { } destroy() { this.#observer.unobserve(this) + this.#view.destroy() + this.#view = null + this.sections[this.#index]?.unload?.() } } diff --git a/view.js b/view.js index a68bf9e..1834fcd 100644 --- a/view.js +++ b/view.js @@ -129,6 +129,15 @@ export class View extends HTMLElement { this.renderer.open(book) this.#root.append(this.renderer) } + close() { + this.renderer?.destroy?.() + this.#sectionProgress = null + this.#tocProgress = null + this.#pageProgress = null + this.#searchResults = new Map() + this.lastLocation = null + this.history = new History() + } goToTextStart() { return this.goTo(this.book.landmarks ?.find(m => m.type.includes('bodymatter') || m.type.includes('text')) @@ -392,10 +401,6 @@ export class View extends HTMLElement { for (const item of list) this.deleteAnnotation(item) this.#searchResults.clear() } - destroy() { - this.book.destroy?.() - this.renderer?.destroy?.() - } } customElements.define('foliate-view', View)