Skip to content

Commit

Permalink
Deregister mapbox events listeners when destroy map (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xintong Xia authored and Xiaoji Chen committed Feb 20, 2019
1 parent c633c92 commit c24dfe2
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/mapbox/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ export default class Mapbox {
}

// PRIVATE API
_fireLoadEvent = () => {
this.props.onLoad({
type: 'load',
target: this._map
});
};

_reuse(props: Props) {
this._map = Mapbox.savedMap;
// When reusing the saved map, we need to reparent the map(canvas) and other child nodes
Expand All @@ -240,11 +247,6 @@ export default class Mapbox {
Mapbox.savedMap = null;

// Step3: update style and call onload again
const fireLoadEvent = () => props.onLoad({
type: 'load',
target: this._map
});

if (props.mapStyle) {
this._map.setStyle(props.mapStyle, {
diff: true
Expand All @@ -253,9 +255,9 @@ export default class Mapbox {

// call onload event handler after style fully loaded when style needs update
if (this._map.isStyleLoaded()) {
fireLoadEvent();
this._fireLoadEvent();
} else {
this._map.once('styledata', fireLoadEvent);
this._map.once('styledata', this._fireLoadEvent);
}
}

Expand Down Expand Up @@ -312,9 +314,16 @@ export default class Mapbox {

if (!Mapbox.savedMap) {
Mapbox.savedMap = this._map;

// deregister the mapbox event listeners
this._map.off('load', this.props.onLoad);
this._map.off('error', this.props.onError);
this._map.off('styledata', this._fireLoadEvent);

} else {
this._map.remove();
}

this._map = null;
}

Expand Down

0 comments on commit c24dfe2

Please sign in to comment.