From 5653244ac089e99167abc39d053605377f9796b0 Mon Sep 17 00:00:00 2001 From: Jean-Francois Leblanc-Richard Date: Wed, 22 Dec 2021 12:59:40 -0500 Subject: [PATCH 1/2] Fix events being fired after Map#remove has been called when the WebGL context is lost and restored (#726) --- CHANGELOG.md | 1 + src/ui/map.ts | 1 + test/unit/ui/map.test.js | 16 ++++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 042c8798df..f217b95630 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ - Fix padding-top of the popup to improve readability of popup text (#354). - Fix GeoJSONSource#loaded sometimes returning true while there are still pending loads (#669) - Fix MapDataEvent#isSourceLoaded being true in GeoJSONSource "dataloading" event handlers (#694) +- Fix events being fired after Map#remove has been called when the WebGL context is lost and restored (#726) ## 1.15.2 diff --git a/src/ui/map.ts b/src/ui/map.ts index f4ce393646..4b19a2b991 100755 --- a/src/ui/map.ts +++ b/src/ui/map.ts @@ -2677,6 +2677,7 @@ class Map extends Camera { const extension = this.painter.context.gl.getExtension('WEBGL_lose_context'); if (extension) extension.loseContext(); + this._canvas.removeEventListener('webglcontextrestored', this._contextRestored, false); DOM.remove(this._canvasContainer); DOM.remove(this._controlContainer); this._container.classList.remove('maplibregl-map', 'mapboxgl-map'); diff --git a/test/unit/ui/map.test.js b/test/unit/ui/map.test.js index 13ad531ee0..8bd5090251 100755 --- a/test/unit/ui/map.test.js +++ b/test/unit/ui/map.test.js @@ -995,6 +995,22 @@ test('Map', (t) => { }); }); + t.test('does not fire "webglcontextrestored" after #remove has been called', (t) => { + const map = createMap(t); + const canvas = map.getCanvas(); + + map.once('webglcontextlost', () => { + map.once('webglcontextrestored', () => t.fail('"webglcontextrestored" fired after #remove has been called')); + map.remove(); + canvas.dispatchEvent(new window.Event('webglcontextrestored')); + t.end(); + }); + + // Dispatch the event manually because at the time of this writing, gl does not support + // the WEBGL_lose_context extension. + canvas.dispatchEvent(new window.Event('webglcontextlost')); + }); + t.test('#redraw', (t) => { const map = createMap(t); From c4a7314f585592c89f11fe523766f2d78efd4cfc Mon Sep 17 00:00:00 2001 From: Jean-Francois Leblanc-Richard Date: Wed, 22 Dec 2021 15:10:01 -0500 Subject: [PATCH 2/2] Remove "webglcontextlost" event listener in Map#remove --- src/ui/map.ts | 1 + test/unit/ui/map.test.js | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/ui/map.ts b/src/ui/map.ts index 4b19a2b991..701335445f 100755 --- a/src/ui/map.ts +++ b/src/ui/map.ts @@ -2678,6 +2678,7 @@ class Map extends Camera { const extension = this.painter.context.gl.getExtension('WEBGL_lose_context'); if (extension) extension.loseContext(); this._canvas.removeEventListener('webglcontextrestored', this._contextRestored, false); + this._canvas.removeEventListener('webglcontextlost', this._contextLost, false); DOM.remove(this._canvasContainer); DOM.remove(this._controlContainer); this._container.classList.remove('maplibregl-map', 'mapboxgl-map'); diff --git a/test/unit/ui/map.test.js b/test/unit/ui/map.test.js index 8bd5090251..58d5889137 100755 --- a/test/unit/ui/map.test.js +++ b/test/unit/ui/map.test.js @@ -995,6 +995,17 @@ test('Map', (t) => { }); }); + t.test('does not fire "webglcontextlost" after #remove has been called', (t) => { + const map = createMap(t); + const canvas = map.getCanvas(); + map.once('webglcontextlost', () => t.fail('"webglcontextlost" fired after #remove has been called')); + map.remove(); + // Dispatch the event manually because at the time of this writing, gl does not support + // the WEBGL_lose_context extension. + canvas.dispatchEvent(new window.Event('webglcontextlost')); + t.end(); + }); + t.test('does not fire "webglcontextrestored" after #remove has been called', (t) => { const map = createMap(t); const canvas = map.getCanvas();