-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathstateChangeHandler.ts
More file actions
70 lines (62 loc) · 2.21 KB
/
stateChangeHandler.ts
File metadata and controls
70 lines (62 loc) · 2.21 KB
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
import { ReanimatedContext } from '../../../handlers/gestures/reanimatedWrapper';
import { CALLBACK_TYPE } from '../../../handlers/gestures/gesture';
import { State } from '../../../State';
import {
GestureCallbacks,
GestureStateChangeEventWithHandlerData,
StateChangeEventWithHandlerData,
} from '../../types';
import {
flattenAndFilterEvent,
isEventForHandlerWithTag,
maybeExtractNativeEvent,
runCallback,
} from '../utils';
export function getStateChangeHandler<TBaseHandlerData, THandlerData>(
handlerTag: number,
callbacks: GestureCallbacks<TBaseHandlerData, THandlerData>,
context?: ReanimatedContext<THandlerData>
) {
return (sourceEvent: StateChangeEventWithHandlerData<THandlerData>) => {
'worklet';
const eventWithData = maybeExtractNativeEvent(
sourceEvent
) as GestureStateChangeEventWithHandlerData<THandlerData>;
const event = flattenAndFilterEvent(eventWithData);
if (!isEventForHandlerWithTag(handlerTag, eventWithData)) {
return;
}
const { state, oldState } = eventWithData;
if (oldState === State.UNDETERMINED && state === State.BEGAN) {
runCallback(CALLBACK_TYPE.BEGAN, callbacks, event);
} else if (
(oldState === State.BEGAN || oldState === State.UNDETERMINED) &&
state === State.ACTIVE
) {
// If the native recognizer skipped the BEGAN state, we still need to call the callback
if (oldState === State.UNDETERMINED) {
runCallback(CALLBACK_TYPE.BEGAN, callbacks, event);
}
runCallback(CALLBACK_TYPE.START, callbacks, event);
} else if (oldState !== state && state === State.END) {
if (oldState === State.ACTIVE) {
runCallback(CALLBACK_TYPE.END, callbacks, event, true);
}
runCallback(CALLBACK_TYPE.FINALIZE, callbacks, event, true);
if (context) {
context.lastUpdateEvent = undefined;
}
} else if (
(state === State.FAILED || state === State.CANCELLED) &&
state !== oldState
) {
if (oldState === State.ACTIVE) {
runCallback(CALLBACK_TYPE.END, callbacks, event, false);
}
runCallback(CALLBACK_TYPE.FINALIZE, callbacks, event, false);
if (context) {
context.lastUpdateEvent = undefined;
}
}
};
}