Skip to content

Commit 20cbc9e

Browse files
committed
one more refactor
1 parent 37c4070 commit 20cbc9e

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

packages/react-native-gesture-handler/src/RNGestureHandlerModule.web.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export default {
6767
},
6868
detachGestureHandler(handlerTag: number) {
6969
if (shouldPreventDrop) {
70+
shouldPreventDrop = false;
7071
return;
7172
}
7273

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,9 @@ export default abstract class GestureHandler implements IGestureHandler {
360360
if (!this.enabled) {
361361
return;
362362
}
363-
if (!this.propsRef) {
364-
throw new Error(
365-
tagMessage('Cannot handle event when component props are null')
366-
);
367-
}
363+
this.ensurePropsRef();
368364
const { onGestureHandlerEvent, onGestureHandlerTouchEvent }: PropsRef =
369-
this.propsRef.current;
365+
this.propsRef!.current;
370366

371367
const touchEvent: ResultTouchEvent | undefined =
372368
this.transformTouchEvent(event);
@@ -388,16 +384,12 @@ export default abstract class GestureHandler implements IGestureHandler {
388384
//
389385

390386
public sendEvent = (newState: State, oldState: State): void => {
391-
if (!this.propsRef) {
392-
throw new Error(
393-
tagMessage('Cannot handle event when component props are null')
394-
);
395-
}
387+
this.ensurePropsRef();
396388
const {
397389
onGestureHandlerEvent,
398390
onGestureHandlerStateChange,
399391
onGestureHandlerAnimatedEvent,
400-
}: PropsRef = this.propsRef.current;
392+
}: PropsRef = this.propsRef!.current;
401393
const resultEvent: ResultEvent = this.transformEventData(
402394
newState,
403395
oldState
@@ -545,11 +537,7 @@ export default abstract class GestureHandler implements IGestureHandler {
545537
}
546538

547539
private cancelTouches(): void {
548-
if (!this.propsRef) {
549-
throw new Error(
550-
tagMessage('Cannot handle event when component props are null')
551-
);
552-
}
540+
this.ensurePropsRef();
553541
const rect = this.delegate.measureView();
554542

555543
const all: PointerData[] = [];
@@ -594,11 +582,19 @@ export default abstract class GestureHandler implements IGestureHandler {
594582
timeStamp: Date.now(),
595583
};
596584

597-
const { onGestureHandlerEvent }: PropsRef = this.propsRef.current;
585+
const { onGestureHandlerEvent }: PropsRef = this.propsRef!.current;
598586

599587
invokeNullableMethod(onGestureHandlerEvent, cancelEvent);
600588
}
601589

590+
protected ensurePropsRef(): void {
591+
if (!this.propsRef) {
592+
throw new Error(
593+
tagMessage('Cannot handle event when component props are null')
594+
);
595+
}
596+
}
597+
602598
protected transformNativeEvent(): Record<string, unknown> {
603599
// Those properties are shared by most handlers and if not this method will be overriden
604600
const lastCoords = this.tracker.getAbsoluteCoordsAverage();

packages/react-native-gesture-handler/src/web/tools/GestureHandlerWebDelegate.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Config } from '../interfaces';
1212
import { MouseButton } from '../../handlers/gestureHandlerCommon';
1313
import KeyboardEventManager from './KeyboardEventManager';
1414
import WheelEventManager from './WheelEventManager';
15+
import { tagMessage } from '../../utils';
1516

1617
interface DefaultViewStyles {
1718
userSelect: string;
@@ -22,7 +23,7 @@ export class GestureHandlerWebDelegate
2223
implements GestureHandlerDelegate<HTMLElement, IGestureHandler>
2324
{
2425
private isInitialized = false;
25-
private _view!: HTMLElement;
26+
private _view: HTMLElement | null = null;
2627

2728
private gestureHandler!: IGestureHandler;
2829
private eventManagers: EventManager<unknown>[] = [];
@@ -64,7 +65,7 @@ export class GestureHandlerWebDelegate
6465
}
6566

6667
detach(): void {
67-
this.view = null as unknown as HTMLElement;
68+
this._view = null;
6869
this.defaultViewStyles = {
6970
userSelect: '',
7071
touchAction: '',
@@ -232,6 +233,10 @@ export class GestureHandlerWebDelegate
232233
}
233234

234235
public get view() {
236+
if (!this._view) {
237+
throw new Error(tagMessage('component`s view is null'));
238+
}
239+
235240
return this._view;
236241
}
237242
public set view(value: HTMLElement) {

0 commit comments

Comments
 (0)