Skip to content

Commit

Permalink
fix #7478, remove controls before destroying map (#7479)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansis committed Oct 26, 2018
1 parent c6d9fa5 commit 29cd586
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/ui/map.js
Expand Up @@ -1664,6 +1664,10 @@ class Map extends Camera {
*/
remove() {
if (this._hash) this._hash.remove();

for (const control of this._controls) control.onRemove(this);
this._controls = [];

if (this._frame) {
this._frame.cancel();
this._frame = null;
Expand All @@ -1675,9 +1679,6 @@ class Map extends Camera {
window.removeEventListener('online', this._onWindowOnline, false);
}

for (const control of this._controls) control.onRemove(this);
this._controls = [];

const extension = this.painter.context.gl.getExtension('WEBGL_lose_context');
if (extension) extension.loseContext();
removeNode(this._canvasContainer);
Expand Down
24 changes: 24 additions & 0 deletions test/unit/ui/map.test.js
Expand Up @@ -817,6 +817,30 @@ test('Map', (t) => {
t.end();
});

t.test('#remove calls onRemove on added controls before style is destroyed', (t) => {
const map = createMap(t);
let onRemoveCalled = 0;
let style;
const control = {
onRemove: function(map) {
onRemoveCalled++;
t.deepEqual(map.getStyle(), style);
},
onAdd: function (_) {
return window.document.createElement('div');
}
};

map.addControl(control);

map.on('style.load', () => {
style = map.getStyle();
map.remove();
t.equal(onRemoveCalled, 1);
t.end();
});
});

t.test('#addControl', (t) => {
const map = createMap(t);
const control = {
Expand Down

0 comments on commit 29cd586

Please sign in to comment.