Skip to content

Commit

Permalink
Cache averages in PointerTracker (#2212)
Browse files Browse the repository at this point in the history
## Description

This PR adds caching average coordinates in `PointerTracker`. Some handlers (like `pan`) send average values in native event. The problem is, that these events are being sent after pointers are removed from tracker. This means, that tracker may return NaN as averege, when received event was `pointerup`. In that case, cached value should be returned.

## Test plan

Tested on example app
  • Loading branch information
m-bert authored Sep 13, 2022
1 parent 1895063 commit ca71f48
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/web/tools/PointerTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default class PointerTracker {

private lastMovedPointerId: number;

private cachedAverages: { x: number; y: number } = { x: 0, y: 0 };

public constructor() {
this.lastMovedPointerId = NaN;

Expand All @@ -49,6 +51,11 @@ export default class PointerTracker {

this.trackedPointers.set(event.pointerId, newElement);
this.mapTouchEventId(event.pointerId);

this.cachedAverages = {
x: this.getLastAvgX(),
y: this.getLastAvgY(),
};
}

public removeFromTracker(pointerId: number): void {
Expand Down Expand Up @@ -78,6 +85,14 @@ export default class PointerTracker {
element.lastY = event.y;

this.trackedPointers.set(event.pointerId, element);

const avgX: number = this.getLastAvgX();
const avgY: number = this.getLastAvgY();

this.cachedAverages = {
x: avgX,
y: avgY,
};
}

//Mapping TouchEvents ID
Expand Down Expand Up @@ -156,11 +171,17 @@ export default class PointerTracker {
}
}

// Some handlers use these methods to send average values in native event.
// This may happen when pointers have already been removed from tracker (i.e. pointerup event).
// In situation when NaN would be sent as a response, we return cached value.
// That prevents handlers from crashing
public getLastAvgX(): number {
return this.getSumX() / this.trackedPointers.size;
const avgX: number = this.getSumX() / this.trackedPointers.size;
return isNaN(avgX) ? this.cachedAverages.x : avgX;
}
public getLastAvgY(): number {
return this.getSumY() / this.trackedPointers.size;
const avgY: number = this.getSumY() / this.trackedPointers.size;
return isNaN(avgY) ? this.cachedAverages.y : avgY;
}
public getSumX(ignoredPointer?: number): number {
let sumX = 0;
Expand Down

0 comments on commit ca71f48

Please sign in to comment.