Skip to content

Commit

Permalink
Force map rerender synchronously after props update (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress authored Feb 11, 2019
1 parent 030c223 commit bd85a56
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/mapbox/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,23 @@ export default class Mapbox {
// (e.g. until "componentDidUpdate")
resize() {
this._map.resize();
return this;
}

// Force redraw the map now. Typically resize() and jumpTo() is reflected in the next
// render cycle, which is managed by Mapbox's animation loop.
// This removes the synchronization issue caused by requestAnimationFrame.
redraw() {
const map = this._map;
// map render will throw error if style is not loaded
if (this._map.isStyleLoaded()) {
this._map._render();
if (map.isStyleLoaded()) {
map._render();
// cancel the scheduled update
if (map._frame) {
map._frame.cancel();
map._frame = null;
}
}
return this;
}

// External apps can access map this way
Expand Down Expand Up @@ -369,8 +381,12 @@ export default class Mapbox {
newProps = Object.assign({}, this.props, newProps);
checkPropTypes(newProps, 'Mapbox');

this._updateMapViewport(oldProps, newProps);
this._updateMapSize(oldProps, newProps);
const viewportChanged = this._updateMapViewport(oldProps, newProps);
const sizeChanged = this._updateMapSize(oldProps, newProps);

if (viewportChanged || sizeChanged) {
this.redraw();
}

this.props = newProps;
}
Expand All @@ -383,6 +399,7 @@ export default class Mapbox {
this.height = newProps.height;
this.resize();
}
return sizeChanged;
}

_updateMapViewport(oldProps: any, newProps: Props) {
Expand All @@ -405,6 +422,7 @@ export default class Mapbox {
this._map.transform.altitude = newViewState.altitude;
}
}
return viewportChanged;
}

_getViewState(props: Props) : ViewState {
Expand Down

0 comments on commit bd85a56

Please sign in to comment.