From 153e06e4d50b8632e05fd3ef77afb905cad97abb Mon Sep 17 00:00:00 2001 From: ahocevar Date: Mon, 3 Jun 2019 13:21:28 +0200 Subject: [PATCH 1/3] Detach label cache on Map#setTarget() --- src/ol/PluggableMap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ol/PluggableMap.js b/src/ol/PluggableMap.js index e6035259cc..0832c11043 100644 --- a/src/ol/PluggableMap.js +++ b/src/ol/PluggableMap.js @@ -546,7 +546,6 @@ class PluggableMap extends BaseObject { cancelAnimationFrame(this.animationDelayKey_); this.animationDelayKey_ = undefined; } - this.detachLabelCache(); this.setTarget(null); super.disposeInternal(); } @@ -1046,6 +1045,7 @@ class PluggableMap extends BaseObject { removeEventListener(EventType.RESIZE, this.handleResize_, false); this.handleResize_ = undefined; } + this.detachLabelCache(); } else { targetElement.appendChild(this.viewport_); From 68b7831dafc144d295c60591e8eb3941b274fb43 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Mon, 3 Jun 2019 14:27:45 +0200 Subject: [PATCH 2/3] Listen to label cache again when we have a target --- src/ol/PluggableMap.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/ol/PluggableMap.js b/src/ol/PluggableMap.js index 0832c11043..bd037c75c7 100644 --- a/src/ol/PluggableMap.js +++ b/src/ol/PluggableMap.js @@ -301,6 +301,11 @@ class PluggableMap extends BaseObject { */ this.interactions = optionsInternal.interactions || new Collection(); + /** + * @type {import("./events/Target.js").default} + */ + this.labelCache_ = null; + /** * @type {import("./events.js").EventsKey} */ @@ -512,20 +517,31 @@ class PluggableMap extends BaseObject { } /** - * Attach a label cache for listening to font changes. + * Attach a label cache and listen to font changes. Called by the renderer. * @param {import("./events/Target.js").default} labelCache Label cache. */ attachLabelCache(labelCache) { - this.detachLabelCache(); - this.labelCacheListenerKey_ = listen(labelCache, EventType.CLEAR, this.redrawText.bind(this)); + this.unlistenLabelCache_(); + this.labelCache_ = labelCache; + this.listenLabelCache_(); } /** - * Detach the label cache, i.e. no longer listen to font changes. + * Listen to font changes. + * @private */ - detachLabelCache() { + listenLabelCache_() { + this.labelCacheListenerKey_ = listen(this.labelCache_, EventType.CLEAR, this.redrawText.bind(this)); + } + + /** + * No longer listen to font changes. + * @private + */ + unlistenLabelCache_() { if (this.labelCacheListenerKey_) { unlistenByKey(this.labelCacheListenerKey_); + delete this.labelCacheListenerKey_; } } @@ -1045,7 +1061,7 @@ class PluggableMap extends BaseObject { removeEventListener(EventType.RESIZE, this.handleResize_, false); this.handleResize_ = undefined; } - this.detachLabelCache(); + this.unlistenLabelCache_(); } else { targetElement.appendChild(this.viewport_); @@ -1060,6 +1076,7 @@ class PluggableMap extends BaseObject { this.handleResize_ = this.updateSize.bind(this); window.addEventListener(EventType.RESIZE, this.handleResize_, false); } + this.listenLabelCache_(); } this.updateSize(); From 8c21c9196ddd9e54f01cb361017b0bb61ecc645b Mon Sep 17 00:00:00 2001 From: ahocevar Date: Mon, 3 Jun 2019 15:10:35 +0200 Subject: [PATCH 3/3] Move label cache listener management to the renderer --- src/ol/PluggableMap.js | 50 ++++++++++-------------------------- src/ol/renderer/Composite.js | 13 +++++++++- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/src/ol/PluggableMap.js b/src/ol/PluggableMap.js index bd037c75c7..fe088e7cd9 100644 --- a/src/ol/PluggableMap.js +++ b/src/ol/PluggableMap.js @@ -328,7 +328,7 @@ class PluggableMap extends BaseObject { * @type {import("./renderer/Map.js").default} * @private */ - this.renderer_ = this.createRenderer(); + this.renderer_ = null; /** * @type {function(Event): void|undefined} @@ -516,35 +516,6 @@ class PluggableMap extends BaseObject { overlay.setMap(this); } - /** - * Attach a label cache and listen to font changes. Called by the renderer. - * @param {import("./events/Target.js").default} labelCache Label cache. - */ - attachLabelCache(labelCache) { - this.unlistenLabelCache_(); - this.labelCache_ = labelCache; - this.listenLabelCache_(); - } - - /** - * Listen to font changes. - * @private - */ - listenLabelCache_() { - this.labelCacheListenerKey_ = listen(this.labelCache_, EventType.CLEAR, this.redrawText.bind(this)); - } - - /** - * No longer listen to font changes. - * @private - */ - unlistenLabelCache_() { - if (this.labelCacheListenerKey_) { - unlistenByKey(this.labelCacheListenerKey_); - delete this.labelCacheListenerKey_; - } - } - /** * * @inheritDoc @@ -558,10 +529,6 @@ class PluggableMap extends BaseObject { removeEventListener(EventType.RESIZE, this.handleResize_, false); this.handleResize_ = undefined; } - if (this.animationDelayKey_) { - cancelAnimationFrame(this.animationDelayKey_); - this.animationDelayKey_ = undefined; - } this.setTarget(null); super.disposeInternal(); } @@ -1056,14 +1023,24 @@ class PluggableMap extends BaseObject { } if (!targetElement) { + if (this.renderer_) { + this.renderer_.dispose(); + this.renderer_ = null; + } + if (this.animationDelayKey_) { + cancelAnimationFrame(this.animationDelayKey_); + this.animationDelayKey_ = undefined; + } removeNode(this.viewport_); if (this.handleResize_ !== undefined) { removeEventListener(EventType.RESIZE, this.handleResize_, false); this.handleResize_ = undefined; } - this.unlistenLabelCache_(); } else { targetElement.appendChild(this.viewport_); + if (!this.renderer_) { + this.renderer_ = this.createRenderer(); + } const keyboardEventTarget = !this.keyboardEventTarget_ ? targetElement : this.keyboardEventTarget_; @@ -1076,7 +1053,6 @@ class PluggableMap extends BaseObject { this.handleResize_ = this.updateSize.bind(this); window.addEventListener(EventType.RESIZE, this.handleResize_, false); } - this.listenLabelCache_(); } this.updateSize(); @@ -1183,7 +1159,7 @@ class PluggableMap extends BaseObject { * @api */ render() { - if (this.animationDelayKey_ === undefined) { + if (this.renderer_ && this.animationDelayKey_ === undefined) { this.animationDelayKey_ = requestAnimationFrame(this.animationDelay_); } } diff --git a/src/ol/renderer/Composite.js b/src/ol/renderer/Composite.js index 49727a68c5..4725d38905 100644 --- a/src/ol/renderer/Composite.js +++ b/src/ol/renderer/Composite.js @@ -9,6 +9,8 @@ import MapRenderer from './Map.js'; import SourceState from '../source/State.js'; import {replaceChildren} from '../dom.js'; import {labelCache} from '../render/canvas.js'; +import EventType from '../events/EventType.js'; +import {listen, unlistenByKey} from '../events.js'; /** @@ -23,7 +25,11 @@ class CompositeMapRenderer extends MapRenderer { */ constructor(map) { super(map); - map.attachLabelCache(labelCache); + + /** + * @type {import("./events.js").EventsKey} + */ + this.labelCacheKey_ = listen(labelCache, EventType.CLEAR, map.redrawText.bind(map)); /** * @private @@ -66,6 +72,11 @@ class CompositeMapRenderer extends MapRenderer { } } + disposeInternal() { + unlistenByKey(this.labelCacheKey_); + super.disposeInternal(); + } + /** * @inheritDoc */