From db727b00a977631ec32c0430a667a1d8f3febfcc Mon Sep 17 00:00:00 2001 From: Fred Chasen Date: Fri, 10 Feb 2017 15:29:20 -0500 Subject: [PATCH] add destroy methods --- examples/spreads.html | 7 ++++++- src/archive.js | 11 ++++++++++- src/container.js | 10 ++++++++++ src/locations.js | 26 +++++++++++++++++++++++++- src/packaging.js | 18 ++++++++++++++++++ src/pagelist.js | 19 +++++++++++++++++++ src/rendition.js | 23 ++++++++++++++++++++++- src/resources.js | 20 ++++++++++++++++++++ src/section.js | 21 +++++++++++++++++++++ src/spine.js | 24 ++++++++++++++++++++++++ src/themes.js | 8 ++++++++ 11 files changed, 183 insertions(+), 4 deletions(-) diff --git a/examples/spreads.html b/examples/spreads.html index 777da8f..54d0396 100644 --- a/examples/spreads.html +++ b/examples/spreads.html @@ -20,7 +20,7 @@ diff --git a/src/archive.js b/src/archive.js index 5a0e995..7050925 100644 --- a/src/archive.js +++ b/src/archive.js @@ -11,8 +11,10 @@ class Archive { constructor() { this.zip = undefined; - this.checkRequirements(); this.urlCache = {}; + + this.checkRequirements(); + } /** @@ -243,6 +245,13 @@ class Archive { var fromCache = this.urlCache[url]; if(fromCache) _URL.revokeObjectURL(fromCache); } + + destroy() { + this.urlCache.forEach((fromCache) => _URL.revokeObjectURL(fromCache)); + + this.zip = undefined; + this.urlCache = {}; + } } export default Archive; diff --git a/src/container.js b/src/container.js index 2d4eaa4..ab8ea4c 100644 --- a/src/container.js +++ b/src/container.js @@ -8,6 +8,10 @@ import {qs} from "./utils/core"; */ class Container { constructor(containerDocument) { + this.packagePath = ''; + this.directory = ''; + this.encoding = ''; + if (containerDocument) { this.parse(containerDocument); } @@ -35,6 +39,12 @@ class Container { this.directory = path.dirname(this.packagePath); this.encoding = containerDocument.xmlEncoding; } + + destroy() { + this.packagePath = undefined; + this.directory = undefined; + this.encoding = undefined; + } } export default Container; diff --git a/src/locations.js b/src/locations.js index f692c3c..71e9c0f 100644 --- a/src/locations.js +++ b/src/locations.js @@ -23,6 +23,10 @@ class Locations { this.break = 150; this._current = 0; + + this.currentLocation = ''; + this._currentCfi =''; + this.processingTimeout = undefined; } /** @@ -76,7 +80,7 @@ class Locations { section.unload(); - setTimeout(() => completed.resolve(locations), this.pause); + this.processingTimeout = setTimeout(() => completed.resolve(locations), this.pause); return completed.promise; }.bind(this)); @@ -257,6 +261,26 @@ class Locations { length () { return this._locations.length; } + + destroy () { + this.spine = undefined; + this.request = undefined; + this.pause = undefined; + + this.q.clear(); + this.q = undefined; + this.epubcfi = undefined; + + this._locations = undefined + this.total = undefined; + + this.break = undefined; + this._current = undefined; + + this.currentLocation = undefined; + this._currentCfi = undefined; + clearTimeout(this.processingTimeout); + } } EventEmitter(Locations.prototype); diff --git a/src/packaging.js b/src/packaging.js index 012003d..6f56124 100644 --- a/src/packaging.js +++ b/src/packaging.js @@ -7,6 +7,14 @@ import {qs, qsa, qsp} from "./utils/core"; */ class Packaging { constructor(packageDocument) { + this.manifest = {}; + this.navPath = ''; + this.ncxPath = ''; + this.coverPath = ''; + this.spineNodeIndex = 0; + this.spine = []; + this.metadata = {}; + if (packageDocument) { this.parse(packageDocument); } @@ -273,6 +281,16 @@ class Packaging { return ""; } + + destroy() { + this.manifest = undefined; + this.navPath = undefined; + this.ncxPath = undefined; + this.coverPath = undefined; + this.spineNodeIndex = undefined; + this.spine = undefined; + this.metadata = undefined; + } } export default Packaging; diff --git a/src/pagelist.js b/src/pagelist.js index fa95318..36be13a 100644 --- a/src/pagelist.js +++ b/src/pagelist.js @@ -17,6 +17,14 @@ class PageList { this.locations = []; this.epubcfi = new EpubCFI(); + this.firstPage = 0; + this.lastPage = 0; + this.totalPages = 0; + + this.toc = undefined; + this.ncx = undefined; + + this.lastPage if (xml) { this.pageList = this.parse(xml); } @@ -250,6 +258,17 @@ class PageList { var percentage = this.percentageFromPage(pg); return percentage; } + + destroy() { + this.pages = undefined; + this.locations = undefined; + this.epubcfi = undefined; + + this.pageList = undefined; + + this.toc = undefined; + this.ncx = undefined; + } } export default PageList; diff --git a/src/rendition.js b/src/rendition.js index 0214a81..fc485c2 100644 --- a/src/rendition.js +++ b/src/rendition.js @@ -514,8 +514,29 @@ class Rendition { destroy(){ // Clear the queue this.q.clear(); + this.q = undefined; - this.manager.destroy(); + this.book = book; + + this.views = null; + + this.hooks.display.clear(); + this.hooks.serialize.clear(); + this.hooks.content.clear(); + this.hooks.layout.clear(); + this.hooks.render.clear(); + this.hooks.show.clear(); + this.hooks = {}; + + this.themes.destroy(); + this.themes = undefined; + + this.epubcfi = undefined; + + this.starting = undefined; + this.started = undefined; + + this.manager && this.manager.destroy(); } /** diff --git a/src/resources.js b/src/resources.js index 52a3c66..4337ba5 100644 --- a/src/resources.js +++ b/src/resources.js @@ -27,6 +27,13 @@ class Resources { this.replacementUrls = []; + this.html = []; + this.assets = []; + this.css = []; + + this.urls = []; + this.cssUrls = []; + this.split(); this.splitUrls(); } @@ -259,6 +266,19 @@ class Resources { } return substitute(content, relUrls, this.replacementUrls); } + + destroy() { + this.settings = undefined; + this.manifest = undefined; + this.resources = undefined; + this.replacementUrls = undefined; + this.html = undefined; + this.assets = undefined; + this.css = undefined; + + this.urls = undefined; + this.cssUrls = undefined; + } } export default Resources; diff --git a/src/section.js b/src/section.js index fd5196f..c4bcfd3 100644 --- a/src/section.js +++ b/src/section.js @@ -171,11 +171,32 @@ class Section { return new EpubCFI(el, this.cfiBase).toString(); } + /** + * Unload the section document + */ unload() { this.document = undefined; this.contents = undefined; this.output = undefined; } + + destroy() { + this.unload(); + this.hooks.serialize.clear(); + this.hooks.content.clear(); + + this.hooks = undefined; + this.idref = undefined; + this.linear = undefined; + this.properties = undefined; + this.index = undefined; + this.href = undefined; + this.url = undefined; + this.next = undefined; + this.prev = undefined; + + this.cfiBase = undefined; + } } export default Section; diff --git a/src/spine.js b/src/spine.js index 21c444e..ceb972d 100644 --- a/src/spine.js +++ b/src/spine.js @@ -23,6 +23,12 @@ class Spine { this.epubcfi = new EpubCFI(); this.loaded = false; + + this.items = undefined; + this.manifest = undefined; + this.spineNodeIndex = undefined; + this.baseUrl = undefined; + this.length = undefined; } /** @@ -158,7 +164,25 @@ class Spine { } destroy() { + this.each((section) => section.destroy()); + this.spineItems = undefined + this.spineByHref = undefined + this.spineById = undefined + + this.hooks.serialize.clear(); + this.hooks.content.clear(); + this.hooks = undefined; + + this.epubcfi = undefined; + + this.loaded = false; + + this.items = undefined; + this.manifest = undefined; + this.spineNodeIndex = undefined; + this.baseUrl = undefined; + this.length = undefined; } } diff --git a/src/themes.js b/src/themes.js index 4547f8b..d4db495 100644 --- a/src/themes.js +++ b/src/themes.js @@ -151,6 +151,14 @@ class Themes { this.override("font-size", size); } + destroy() { + this.rendition = undefined; + this._themes = undefined; + this._overrides = undefined; + this._current = undefined; + this._injected = undefined; + } + } export default Themes;