Skip to content

Commit 5752e01

Browse files
authored
[Web] Migrate to get and set (#3263)
## Description In this PR I've migrated our handlers into `get`/`set` functions instead of traditional getters and setters convention (like in `Java`). ## Test plan Run web example
1 parent dd087e5 commit 5752e01

22 files changed

+364
-366
lines changed

src/RNGestureHandlerModule.web.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default {
3737
handlerTag,
3838
new GestureClass(new GestureHandlerWebDelegate())
3939
);
40-
InteractionManager.getInstance().configureInteractions(
40+
InteractionManager.instance.configureInteractions(
4141
NodeManager.getHandler(handlerTag),
4242
config as unknown as Config
4343
);
@@ -67,8 +67,7 @@ export default {
6767
},
6868
updateGestureHandler(handlerTag: number, newConfig: Config) {
6969
NodeManager.getHandler(handlerTag).updateGestureConfig(newConfig);
70-
71-
InteractionManager.getInstance().configureInteractions(
70+
InteractionManager.instance.configureInteractions(
7271
NodeManager.getHandler(handlerTag),
7372
newConfig
7473
);

src/web/detectors/RotationGestureDetector.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export default class RotationGestureDetector
1818
private previousTime = 0;
1919

2020
private previousAngle = 0;
21-
private rotation = 0;
21+
private _rotation = 0;
2222

23-
private anchorX = 0;
24-
private anchorY = 0;
23+
private _anchorX = 0;
24+
private _anchorY = 0;
2525

2626
private isInProgress = false;
2727

@@ -45,28 +45,28 @@ export default class RotationGestureDetector
4545
const vectorX: number = secondPointerCoords.x - firstPointerCoords.x;
4646
const vectorY: number = secondPointerCoords.y - firstPointerCoords.y;
4747

48-
this.anchorX = (firstPointerCoords.x + secondPointerCoords.x) / 2;
49-
this.anchorY = (firstPointerCoords.y + secondPointerCoords.y) / 2;
48+
this._anchorX = (firstPointerCoords.x + secondPointerCoords.x) / 2;
49+
this._anchorY = (firstPointerCoords.y + secondPointerCoords.y) / 2;
5050

5151
// Angle diff should be positive when rotating in clockwise direction
5252
const angle: number = -Math.atan2(vectorY, vectorX);
5353

54-
this.rotation = Number.isNaN(this.previousAngle)
54+
this._rotation = Number.isNaN(this.previousAngle)
5555
? 0
5656
: this.previousAngle - angle;
5757

5858
this.previousAngle = angle;
5959

6060
if (this.rotation > Math.PI) {
61-
this.rotation -= Math.PI;
61+
this._rotation -= Math.PI;
6262
} else if (this.rotation < -Math.PI) {
63-
this.rotation += Math.PI;
63+
this._rotation += Math.PI;
6464
}
6565

6666
if (this.rotation > Math.PI / 2) {
67-
this.rotation -= Math.PI;
67+
this._rotation -= Math.PI;
6868
} else if (this.rotation < -Math.PI / 2) {
69-
this.rotation += Math.PI;
69+
this._rotation += Math.PI;
7070
}
7171
}
7272

@@ -85,7 +85,7 @@ export default class RotationGestureDetector
8585
return;
8686
}
8787

88-
const pointerIDs: IterableIterator<number> = tracker.getData().keys();
88+
const pointerIDs: IterableIterator<number> = tracker.trackedPointers.keys();
8989

9090
this.keyPointers[0] = pointerIDs.next().value as number;
9191
this.keyPointers[1] = pointerIDs.next().value as number;
@@ -143,24 +143,24 @@ export default class RotationGestureDetector
143143
return true;
144144
}
145145

146-
public getTimeDelta(): number {
147-
return this.currentTime + this.previousTime;
146+
public reset(): void {
147+
this.keyPointers = [NaN, NaN];
148+
this.isInProgress = false;
148149
}
149150

150-
public getAnchorX(): number {
151-
return this.anchorX;
151+
public get anchorX() {
152+
return this._anchorX;
152153
}
153154

154-
public getAnchorY(): number {
155-
return this.anchorY;
155+
public get anchorY() {
156+
return this._anchorY;
156157
}
157158

158-
public getRotation(): number {
159-
return this.rotation;
159+
public get rotation() {
160+
return this._rotation;
160161
}
161162

162-
public reset(): void {
163-
this.keyPointers = [NaN, NaN];
164-
this.isInProgress = false;
163+
public get timeDelta() {
164+
return this.currentTime + this.previousTime;
165165
}
166166
}

src/web/detectors/ScaleGestureDetector.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
1414
public onScale: (detector: ScaleGestureDetector) => boolean;
1515
public onScaleEnd: (detector: ScaleGestureDetector) => void;
1616

17-
private focusX!: number;
18-
private focusY!: number;
17+
private _focusX!: number;
18+
private _focusY!: number;
1919

20-
private currentSpan!: number;
20+
private _currentSpan!: number;
2121
private prevSpan!: number;
2222
private initialSpan!: number;
2323

@@ -42,7 +42,7 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
4242
this.currentTime = event.time;
4343

4444
const action: EventTypes = event.eventType;
45-
const numOfPointers = tracker.getTrackedPointersCount();
45+
const numOfPointers = tracker.trackedPointersCount;
4646

4747
const streamComplete: boolean =
4848
action === EventTypes.UP ||
@@ -86,7 +86,7 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
8686
let devSumX = 0;
8787
let devSumY = 0;
8888

89-
tracker.getData().forEach((value, key) => {
89+
tracker.trackedPointers.forEach((value, key) => {
9090
if (key === ignoredPointer) {
9191
return;
9292
}
@@ -105,8 +105,8 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
105105

106106
// Begin/end events
107107
const wasInProgress: boolean = this.inProgress;
108-
this.focusX = focusX;
109-
this.focusY = focusY;
108+
this._focusX = focusX;
109+
this._focusY = focusY;
110110

111111
if (this.inProgress && (span < this.minSpan || configChanged)) {
112112
this.onScaleEnd(this);
@@ -115,15 +115,15 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
115115
}
116116

117117
if (configChanged) {
118-
this.initialSpan = this.prevSpan = this.currentSpan = span;
118+
this.initialSpan = this.prevSpan = this._currentSpan = span;
119119
}
120120

121121
if (
122122
!this.inProgress &&
123123
span >= this.minSpan &&
124124
(wasInProgress || Math.abs(span - this.initialSpan) > this.spanSlop)
125125
) {
126-
this.prevSpan = this.currentSpan = span;
126+
this.prevSpan = this._currentSpan = span;
127127
this.prevTime = this.currentTime;
128128
this.inProgress = this.onScaleBegin(this);
129129
}
@@ -133,7 +133,7 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
133133
return true;
134134
}
135135

136-
this.currentSpan = span;
136+
this._currentSpan = span;
137137

138138
if (this.inProgress && !this.onScale(this)) {
139139
return true;
@@ -145,27 +145,27 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
145145
return true;
146146
}
147147

148-
public getCurrentSpan(): number {
149-
return this.currentSpan;
150-
}
148+
public calculateScaleFactor(numOfPointers: number): number {
149+
if (numOfPointers < 2) {
150+
return 1;
151+
}
151152

152-
public getFocusX(): number {
153-
return this.focusX;
153+
return this.prevSpan > 0 ? this.currentSpan / this.prevSpan : 1;
154154
}
155155

156-
public getFocusY(): number {
157-
return this.focusY;
156+
public get currentSpan() {
157+
return this._currentSpan;
158158
}
159159

160-
public getTimeDelta(): number {
161-
return this.currentTime - this.prevTime;
160+
public get focusX() {
161+
return this._focusX;
162162
}
163163

164-
public getScaleFactor(numOfPointers: number): number {
165-
if (numOfPointers < 2) {
166-
return 1;
167-
}
164+
public get focusY() {
165+
return this._focusY;
166+
}
168167

169-
return this.prevSpan > 0 ? this.currentSpan / this.prevSpan : 1;
168+
public get timeDelta() {
169+
return this.currentTime - this.prevTime;
170170
}
171171
}

src/web/handlers/FlingGestureHandler.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,29 +125,28 @@ export default class FlingGestureHandler extends GestureHandler {
125125
}
126126

127127
private newPointerAction(): void {
128-
if (this.currentState === State.UNDETERMINED) {
128+
if (this.state === State.UNDETERMINED) {
129129
this.startFling();
130130
}
131131

132-
if (this.currentState !== State.BEGAN) {
132+
if (this.state !== State.BEGAN) {
133133
return;
134134
}
135135

136136
this.tryEndFling();
137137

138138
if (
139-
this.tracker.getTrackedPointersCount() >
140-
this.maxNumberOfPointersSimultaneously
139+
this.tracker.trackedPointersCount > this.maxNumberOfPointersSimultaneously
141140
) {
142141
this.maxNumberOfPointersSimultaneously =
143-
this.tracker.getTrackedPointersCount();
142+
this.tracker.trackedPointersCount;
144143
}
145144
}
146145

147146
private pointerMoveAction(event: AdaptedEvent): void {
148147
this.tracker.track(event);
149148

150-
if (this.currentState !== State.BEGAN) {
149+
if (this.state !== State.BEGAN) {
151150
return;
152151
}
153152

@@ -177,7 +176,7 @@ export default class FlingGestureHandler extends GestureHandler {
177176
}
178177

179178
private onUp(event: AdaptedEvent): void {
180-
if (this.currentState === State.BEGAN) {
179+
if (this.state === State.BEGAN) {
181180
this.endFling();
182181
}
183182

0 commit comments

Comments
 (0)