-
-
Notifications
You must be signed in to change notification settings - Fork 981
/
eventReceiver.ts
155 lines (139 loc) · 4.87 KB
/
eventReceiver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { DeviceEventEmitter, EmitterSubscription } from 'react-native';
import { State } from '../../State';
import { TouchEventType } from '../../TouchEventType';
import {
GestureTouchEvent,
GestureUpdateEvent,
GestureStateChangeEvent,
} from '../gestureHandlerCommon';
import { findHandler, findOldGestureHandler } from '../handlersRegistry';
import { BaseGesture } from './gesture';
import {
GestureStateManager,
GestureStateManagerType,
} from './gestureStateManager';
let gestureHandlerEventSubscription: EmitterSubscription | null = null;
let gestureHandlerStateChangeEventSubscription: EmitterSubscription | null =
null;
const gestureStateManagers: Map<number, GestureStateManagerType> = new Map<
number,
GestureStateManagerType
>();
const lastUpdateEvent: (GestureUpdateEvent | undefined)[] = [];
function isStateChangeEvent(
event: GestureUpdateEvent | GestureStateChangeEvent | GestureTouchEvent
): event is GestureStateChangeEvent {
// @ts-ignore oldState doesn't exist on GestureTouchEvent and that's the point
return event.oldState != null;
}
function isTouchEvent(
event: GestureUpdateEvent | GestureStateChangeEvent | GestureTouchEvent
): event is GestureTouchEvent {
return event.eventType != null;
}
export function onGestureHandlerEvent(
event: GestureUpdateEvent | GestureStateChangeEvent | GestureTouchEvent
) {
const handler = findHandler(event.handlerTag) as BaseGesture<
Record<string, unknown>
>;
if (handler) {
if (isStateChangeEvent(event)) {
if (
event.oldState === State.UNDETERMINED &&
event.state === State.BEGAN
) {
handler.handlers.onBegin?.(event);
} else if (
(event.oldState === State.BEGAN ||
event.oldState === State.UNDETERMINED) &&
event.state === State.ACTIVE
) {
handler.handlers.onStart?.(event);
lastUpdateEvent[handler.handlers.handlerTag] = event;
} else if (event.oldState !== event.state && event.state === State.END) {
if (event.oldState === State.ACTIVE) {
handler.handlers.onEnd?.(event, true);
}
handler.handlers.onFinalize?.(event, true);
lastUpdateEvent[handler.handlers.handlerTag] = undefined;
} else if (
(event.state === State.FAILED || event.state === State.CANCELLED) &&
event.oldState !== event.state
) {
if (event.oldState === State.ACTIVE) {
handler.handlers.onEnd?.(event, false);
}
handler.handlers.onFinalize?.(event, false);
gestureStateManagers.delete(event.handlerTag);
lastUpdateEvent[handler.handlers.handlerTag] = undefined;
}
} else if (isTouchEvent(event)) {
if (!gestureStateManagers.has(event.handlerTag)) {
gestureStateManagers.set(
event.handlerTag,
GestureStateManager.create(event.handlerTag)
);
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const manager = gestureStateManagers.get(event.handlerTag)!;
switch (event.eventType) {
case TouchEventType.TOUCHES_DOWN:
handler.handlers?.onTouchesDown?.(event, manager);
break;
case TouchEventType.TOUCHES_MOVE:
handler.handlers?.onTouchesMove?.(event, manager);
break;
case TouchEventType.TOUCHES_UP:
handler.handlers?.onTouchesUp?.(event, manager);
break;
case TouchEventType.TOUCHES_CANCELLED:
handler.handlers?.onTouchesCancelled?.(event, manager);
break;
}
} else {
handler.handlers.onUpdate?.(event);
if (handler.handlers.onChange && handler.handlers.changeEventCalculator) {
handler.handlers.onChange?.(
handler.handlers.changeEventCalculator?.(
event,
lastUpdateEvent[handler.handlers.handlerTag]
)
);
lastUpdateEvent[handler.handlers.handlerTag] = event;
}
}
} else {
const oldHandler = findOldGestureHandler(event.handlerTag);
if (oldHandler) {
const nativeEvent = { nativeEvent: event };
if (isStateChangeEvent(event)) {
oldHandler.onGestureStateChange(nativeEvent);
} else {
oldHandler.onGestureEvent(nativeEvent);
}
return;
}
}
}
export function startListening() {
stopListening();
gestureHandlerEventSubscription = DeviceEventEmitter.addListener(
'onGestureHandlerEvent',
onGestureHandlerEvent
);
gestureHandlerStateChangeEventSubscription = DeviceEventEmitter.addListener(
'onGestureHandlerStateChange',
onGestureHandlerEvent
);
}
export function stopListening() {
if (gestureHandlerEventSubscription) {
gestureHandlerEventSubscription.remove();
gestureHandlerEventSubscription = null;
}
if (gestureHandlerStateChangeEventSubscription) {
gestureHandlerStateChangeEventSubscription.remove();
gestureHandlerStateChangeEventSubscription = null;
}
}