Skip to content

Commit 36d302e

Browse files
authored
[Web] Extract coords assignment into helper functions (#3574)
## Description I've noticed that these lines are repeated multiple times, so I decided to extract them into helper functions. ## Test plan Tested on _**Transformations**_ and _**Multitap**_ examples.
1 parent 9e05a1d commit 36d302e

File tree

2 files changed

+39
-55
lines changed

2 files changed

+39
-55
lines changed

packages/react-native-gesture-handler/src/web/handlers/PanGestureHandler.ts

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,20 @@ export default class PanGestureHandler extends GestureHandler {
218218
clearTimeout(this.activationTimeout);
219219
}
220220

221+
private updateLastCoords() {
222+
const { x, y } = this.tracker.getAbsoluteCoordsAverage();
223+
224+
this.lastX = x;
225+
this.lastY = y;
226+
}
227+
228+
private updateVelocity(pointerId: number) {
229+
const { x, y } = this.tracker.getVelocity(pointerId);
230+
231+
this.velocityX = x;
232+
this.velocityY = y;
233+
}
234+
221235
// Events Handling
222236
protected onPointerDown(event: AdaptedEvent): void {
223237
if (!this.isButtonInConfig(event.button)) {
@@ -229,9 +243,7 @@ export default class PanGestureHandler extends GestureHandler {
229243

230244
super.onPointerDown(event);
231245

232-
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
233-
this.lastX = lastCoords.x;
234-
this.lastY = lastCoords.y;
246+
this.updateLastCoords();
235247

236248
this.startX = this.lastX;
237249
this.startY = this.lastY;
@@ -250,9 +262,7 @@ export default class PanGestureHandler extends GestureHandler {
250262
this.offsetX += this.lastX - this.startX;
251263
this.offsetY += this.lastY - this.startY;
252264

253-
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
254-
this.lastX = lastCoords.x;
255-
this.lastY = lastCoords.y;
265+
this.updateLastCoords();
256266

257267
this.startX = this.lastX;
258268
this.startY = this.lastY;
@@ -299,9 +309,7 @@ export default class PanGestureHandler extends GestureHandler {
299309
this.offsetX += this.lastX - this.startX;
300310
this.offsetY += this.lastY - this.startY;
301311

302-
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
303-
this.lastX = lastCoords.x;
304-
this.lastY = lastCoords.y;
312+
this.updateLastCoords();
305313

306314
this.startX = this.lastX;
307315
this.startY = this.lastY;
@@ -320,13 +328,8 @@ export default class PanGestureHandler extends GestureHandler {
320328
this.tracker.track(event);
321329
this.stylusData = event.stylusData;
322330

323-
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
324-
this.lastX = lastCoords.x;
325-
this.lastY = lastCoords.y;
326-
327-
const velocity = this.tracker.getVelocity(event.pointerId);
328-
this.velocityX = velocity.x;
329-
this.velocityY = velocity.y;
331+
this.updateLastCoords();
332+
this.updateVelocity(event.pointerId);
330333

331334
this.checkBegan();
332335

@@ -341,13 +344,8 @@ export default class PanGestureHandler extends GestureHandler {
341344
this.tracker.track(event);
342345
this.stylusData = event.stylusData;
343346

344-
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
345-
this.lastX = lastCoords.x;
346-
this.lastY = lastCoords.y;
347-
348-
const velocity = this.tracker.getVelocity(event.pointerId);
349-
this.velocityX = velocity.x;
350-
this.velocityY = velocity.y;
347+
this.updateLastCoords();
348+
this.updateVelocity(event.pointerId);
351349

352350
this.checkBegan();
353351

@@ -391,9 +389,7 @@ export default class PanGestureHandler extends GestureHandler {
391389

392390
this.tracker.addToTracker(event);
393391

394-
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
395-
this.lastX = lastCoords.x;
396-
this.lastY = lastCoords.y;
392+
this.updateLastCoords();
397393

398394
this.startX = this.lastX;
399395
this.startY = this.lastY;
@@ -403,13 +399,8 @@ export default class PanGestureHandler extends GestureHandler {
403399
}
404400
this.tracker.track(event);
405401

406-
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
407-
this.lastX = lastCoords.x;
408-
this.lastY = lastCoords.y;
409-
410-
const velocity = this.tracker.getVelocity(event.pointerId);
411-
this.velocityX = velocity.x;
412-
this.velocityY = velocity.y;
402+
this.updateLastCoords();
403+
this.updateVelocity(event.pointerId);
413404

414405
this.tryToSendMoveEvent(false, event);
415406
this.scheduleWheelEnd(event);
@@ -540,9 +531,7 @@ export default class PanGestureHandler extends GestureHandler {
540531
}, this.activateAfterLongPress);
541532
}
542533
} else {
543-
const velocity = this.tracker.getVelocity(event.pointerId);
544-
this.velocityX = velocity.x;
545-
this.velocityY = velocity.y;
534+
this.updateVelocity(event.pointerId);
546535
}
547536
}
548537

packages/react-native-gesture-handler/src/web/handlers/TapGestureHandler.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ export default class TapGestureHandler extends GestureHandler {
9999
}
100100
}
101101

102+
private updateLastCoords() {
103+
const { x, y } = this.tracker.getAbsoluteCoordsAverage();
104+
105+
this.lastX = x;
106+
this.lastY = y;
107+
}
108+
102109
// Handling Events
103110
protected onPointerDown(event: AdaptedEvent): void {
104111
if (!this.isButtonInConfig(event.button)) {
@@ -129,22 +136,18 @@ export default class TapGestureHandler extends GestureHandler {
129136
this.offsetX += this.lastX - this.startX;
130137
this.offsetY += this.lastY - this.startY;
131138

132-
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
133-
this.lastX = lastCoords.x;
134-
this.lastY = lastCoords.y;
139+
this.updateLastCoords();
135140

136-
this.startX = lastCoords.x;
137-
this.startY = lastCoords.y;
141+
this.startX = this.lastX;
142+
this.startY = this.lastY;
138143

139144
this.updateState(event);
140145
}
141146

142147
protected onPointerUp(event: AdaptedEvent): void {
143148
super.onPointerUp(event);
144149

145-
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
146-
this.lastX = lastCoords.x;
147-
this.lastY = lastCoords.y;
150+
this.updateLastCoords();
148151

149152
this.tracker.removeFromTracker(event.pointerId);
150153

@@ -158,9 +161,7 @@ export default class TapGestureHandler extends GestureHandler {
158161
this.offsetX += this.lastX - this.startX;
159162
this.offsetY += this.lastY = this.startY;
160163

161-
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
162-
this.lastX = lastCoords.x;
163-
this.lastY = lastCoords.y;
164+
this.updateLastCoords();
164165

165166
this.startX = this.lastX;
166167
this.startY = this.lastY;
@@ -172,10 +173,7 @@ export default class TapGestureHandler extends GestureHandler {
172173
this.trySettingPosition(event);
173174
this.tracker.track(event);
174175

175-
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
176-
this.lastX = lastCoords.x;
177-
this.lastY = lastCoords.y;
178-
176+
this.updateLastCoords();
179177
this.updateState(event);
180178

181179
super.onPointerMove(event);
@@ -185,10 +183,7 @@ export default class TapGestureHandler extends GestureHandler {
185183
this.trySettingPosition(event);
186184
this.tracker.track(event);
187185

188-
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
189-
this.lastX = lastCoords.x;
190-
this.lastY = lastCoords.y;
191-
186+
this.updateLastCoords();
192187
this.updateState(event);
193188

194189
super.onPointerOutOfBounds(event);

0 commit comments

Comments
 (0)