Skip to content

Commit

Permalink
fix(ui): Redraw map on visibilityState change to fix map not being up…
Browse files Browse the repository at this point in the history
…dated if the tab was invisible for too long
  • Loading branch information
Hypfer committed Dec 19, 2021
1 parent 7a2c3d1 commit 1c86152
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions frontend/src/map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Map<P, S> extends React.Component<P & MapProps, S & MapState > {
protected ctx: any;
protected canvas: HTMLCanvasElement | null;
protected readonly resizeListener: () => void;
protected readonly visibilityStateChangeListener: () => void;

protected drawableComponents: Array<CanvasImageSource> = [];
protected drawableComponentsMutex: semaphore.Semaphore = semaphore(1); //Required to sync up with the render webWorker
Expand Down Expand Up @@ -92,6 +93,12 @@ class Map<P, S> extends React.Component<P & MapProps, S & MapState > {
this.draw();
};

this.visibilityStateChangeListener = () => {
if (document.visibilityState === "visible") {
this.draw();
}
};

this.canvas = null;
this.ctx = null;
}
Expand All @@ -110,6 +117,7 @@ class Map<P, S> extends React.Component<P & MapProps, S & MapState > {
trackTransforms(this.ctx);
this.registerCanvasInteractionHandlers();
window.addEventListener("resize", this.resizeListener);
document.addEventListener("visibilitychange", this.visibilityStateChangeListener);

this.ctx.imageSmoothingEnabled = false;

Expand Down Expand Up @@ -183,6 +191,7 @@ class Map<P, S> extends React.Component<P & MapProps, S & MapState > {

componentWillUnmount(): void {
window.removeEventListener("resize", this.resizeListener);
document.removeEventListener("visibilitychange", this.visibilityStateChangeListener);
}

protected updateInternalDrawableState() : void {
Expand Down Expand Up @@ -257,7 +266,7 @@ class Map<P, S> extends React.Component<P & MapProps, S & MapState > {
}


protected draw() : void{
protected draw() : void {
window.requestAnimationFrame(() => {
this.drawableComponentsMutex.take(() => {
if (!this.ctx || !this.canvas) {
Expand Down Expand Up @@ -397,11 +406,7 @@ class Map<P, S> extends React.Component<P & MapProps, S & MapState > {
s.active = false;
});

if (didUpdateStructures) {
this.draw();
}

if (drawRequested) {
if (didUpdateStructures || drawRequested) {
this.draw();
}
}
Expand Down

0 comments on commit 1c86152

Please sign in to comment.