Skip to content

Commit 593c44f

Browse files
committed
android and web refactor
1 parent 5c24151 commit 593c44f

File tree

5 files changed

+59
-93
lines changed

5 files changed

+59
-93
lines changed

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerDetectorView.kt

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
1616
private var nativeHandlers: MutableSet<Int> = mutableSetOf()
1717
private var attachedHandlers: MutableSet<Int> = mutableSetOf()
1818
private var moduleId: Int = -1
19-
private var logicChildren: HashMap<Int, LogicChild> = hashMapOf()
20-
21-
class LogicChild {
22-
var handlerTags: List<Int>? = null
23-
var attachedHandlers: MutableSet<Int> = mutableSetOf()
24-
}
19+
private var logicChildren: HashMap<Int, MutableSet<Int>> = hashMapOf()
2520

2621
data class LogicProps(val handlerTags: List<Int>, val moduleId: Int, val viewTag: Int)
2722

@@ -46,10 +41,12 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
4641
}
4742

4843
fun setLogicChildren(newLogicChildren: ReadableArray?) {
49-
val shouldKeepLogicChild = HashMap<Int, Boolean>()
44+
val logicChildrenToDelete = HashSet<Int>()
45+
5046
for (child in logicChildren) {
51-
shouldKeepLogicChild[child.key] = false
47+
logicChildrenToDelete.add(child.key)
5248
}
49+
5350
val mappedChildren = mutableListOf<LogicProps>()
5451

5552
for (i in 0 until newLogicChildren!!.size()) {
@@ -73,16 +70,24 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
7370
)
7471
}
7572

76-
for (child in logicChildren) {
77-
shouldKeepLogicChild[child.key] = false
78-
}
79-
8073
for (child in mappedChildren) {
8174
if (!logicChildren.containsKey(child.viewTag)) {
82-
logicChildren.put(child.viewTag, LogicChild())
75+
logicChildren.put(child.viewTag, mutableSetOf())
8376
}
84-
shouldKeepLogicChild[child.viewTag] = true
85-
attachHandlers(child.handlerTags, child.viewTag, true, logicChildren[child.viewTag]!!.attachedHandlers)
77+
logicChildrenToDelete.remove(child.viewTag)
78+
attachHandlers(
79+
child.handlerTags,
80+
child.viewTag,
81+
true,
82+
logicChildren[child.viewTag]!!,
83+
)
84+
}
85+
86+
for (childTag in logicChildrenToDelete) {
87+
val registry = RNGestureHandlerModule.registries[moduleId]
88+
?: throw Exception("Tried to access a non-existent registry")
89+
registry.detachHandler(childTag)
90+
logicChildren.remove(childTag)
8691
}
8792
}
8893

@@ -197,10 +202,10 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
197202
}
198203

199204
for (child in logicChildren) {
200-
for (tag in child.value.attachedHandlers) {
205+
for (tag in child.value) {
201206
registry.detachHandler(tag)
202207
}
203-
child.value.attachedHandlers.clear()
208+
child.value.clear()
204209
}
205210
}
206211

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerDetectorViewManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class RNGestureHandlerDetectorViewManager :
3737
view.setModuleId(value)
3838
}
3939

40-
override fun setLogicChildren(view: RNGestureHandlerDetectorView?, value: ReadableArray?) {
41-
view?.setLogicChildren(value)
40+
override fun setLogicChildren(view: RNGestureHandlerDetectorView, value: ReadableArray?) {
41+
view.setLogicChildren(value)
4242
}
4343

4444
override fun onDropViewInstance(view: RNGestureHandlerDetectorView) {

packages/react-native-gesture-handler/src/ActionType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const ActionType = {
44
JS_FUNCTION_OLD_API: 3,
55
JS_FUNCTION_NEW_API: 4,
66
NATIVE_DETECTOR: 5,
7-
LogicDetector: 6,
7+
LOGIC_DETECTOR: 6,
88
} as const;
99

1010
// eslint-disable-next-line @typescript-eslint/no-redeclare -- backward compatibility; it can be used as a type and as a value

packages/react-native-gesture-handler/src/v3/HostGestureDetector.web.tsx

Lines changed: 28 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ export interface LogicDetectorProps {
1919
viewRef: RefObject<Element | null>;
2020
}
2121

22-
interface LogicChild {
23-
attachedHandlerTags: Set<number>;
24-
attachedNativeHandlerTags: Set<number>;
25-
}
26-
2722
const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
2823
const { handlerTags, children } = props;
2924

@@ -32,16 +27,15 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
3227
const attachedHandlerTags = useRef<Set<number>>(new Set<number>());
3328
const attachedNativeHandlerTags = useRef<Set<number>>(new Set<number>());
3429

35-
const logicChildren = useRef<Map<number, LogicChild>>(new Map());
30+
const logicChildren = useRef<Map<number, Set<number>>>(new Map());
3631

3732
const detachHandlers = (
3833
oldHandlerTags: Set<number>,
39-
attachedHandlerTags: Set<number>,
40-
attachedNativeHandlerTags: Set<number>
34+
attachedHandlerTags: Set<number>
4135
) => {
4236
oldHandlerTags.forEach((tag) => {
4337
RNGestureHandlerModule.detachGestureHandler(tag);
44-
attachedNativeHandlerTags.delete(tag);
38+
attachedNativeHandlerTags.current.delete(tag);
4539
attachedHandlerTags.delete(tag);
4640
});
4741
};
@@ -51,16 +45,11 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
5145
propsRef: RefObject<PropsRef>,
5246
currentHandlerTags: Set<number>,
5347
attachedHandlerTags: Set<number>,
54-
attachedNativeHandlerTags: Set<number>,
5548
isLogic: boolean
5649
) => {
5750
const oldHandlerTags = attachedHandlerTags.difference(currentHandlerTags);
5851
const newHandlerTags = currentHandlerTags.difference(attachedHandlerTags);
59-
detachHandlers(
60-
oldHandlerTags,
61-
attachedHandlerTags,
62-
attachedNativeHandlerTags
63-
);
52+
detachHandlers(oldHandlerTags, attachedHandlerTags);
6453

6554
newHandlerTags.forEach((tag) => {
6655
if (
@@ -74,12 +63,12 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
7463
ActionType.NATIVE_DETECTOR,
7564
propsRef
7665
);
77-
attachedNativeHandlerTags.add(tag);
66+
attachedNativeHandlerTags.current.add(tag);
7867
} else {
7968
RNGestureHandlerModule.attachGestureHandler(
8069
tag,
8170
viewRef.current,
82-
isLogic ? ActionType.LogicDetector : ActionType.NATIVE_DETECTOR,
71+
isLogic ? ActionType.LOGIC_DETECTOR : ActionType.NATIVE_DETECTOR,
8372
propsRef
8473
);
8574
}
@@ -90,8 +79,7 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
9079
useEffect(() => {
9180
detachHandlers(
9281
attachedNativeHandlerTags.current,
93-
attachedHandlerTags.current,
94-
attachedNativeHandlerTags.current
82+
attachedHandlerTags.current
9583
);
9684
}, [children]);
9785

@@ -107,74 +95,47 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
10795
propsRef,
10896
new Set(handlerTags),
10997
attachedHandlerTags.current,
110-
attachedNativeHandlerTags.current,
11198
false
11299
);
113100
}, [handlerTags, children]);
114101

115102
useEffect(() => {
116-
const shouldKeepLogicChild: Map<number, boolean> = new Map();
103+
const logicChildrenToDelete: Set<number> = new Set();
117104

118105
for (const key of logicChildren.current.keys()) {
119-
shouldKeepLogicChild.set(key, false);
106+
logicChildrenToDelete.add(key);
120107
}
121108

122-
props.logicChildren?.forEach((child, key) => {
109+
props.logicChildren?.forEach((child) => {
123110
if (!logicChildren.current.has(child.viewTag)) {
124-
logicChildren.current.set(child.viewTag, {
125-
attachedHandlerTags: new Set(),
126-
attachedNativeHandlerTags: new Set(),
127-
});
128-
}
129-
shouldKeepLogicChild.set(key.viewTag, true);
130-
const attachedHandlerTags = logicChildren.current.get(
131-
child.viewTag
132-
)?.attachedHandlerTags;
133-
const attachedNativeHandlerTags = logicChildren.current.get(
134-
child.viewTag
135-
)?.attachedNativeHandlerTags;
136-
if (attachedHandlerTags && attachedNativeHandlerTags) {
137-
attachHandlers(
138-
child.viewRef,
139-
propsRef,
140-
new Set(child.handlerTags),
141-
attachedHandlerTags,
142-
attachedNativeHandlerTags,
143-
true
144-
);
111+
logicChildren.current.set(child.viewTag, new Set());
145112
}
113+
logicChildrenToDelete.delete(child.viewTag);
114+
attachHandlers(
115+
child.viewRef,
116+
propsRef,
117+
new Set(child.handlerTags),
118+
logicChildren.current.get(child.viewTag)!,
119+
true
120+
);
146121
});
147122

148-
shouldKeepLogicChild.forEach((value, key) => {
149-
if (value) {
150-
const attachedHandlerTags =
151-
logicChildren.current.get(key)?.attachedHandlerTags;
152-
const attachedNativeHandlerTags =
153-
logicChildren.current.get(key)?.attachedNativeHandlerTags;
154-
if (attachedHandlerTags && attachedNativeHandlerTags) {
155-
detachHandlers(
156-
attachedHandlerTags,
157-
attachedHandlerTags,
158-
attachedNativeHandlerTags
159-
);
160-
}
123+
logicChildrenToDelete.forEach((childTag) => {
124+
if (attachedHandlerTags && attachedNativeHandlerTags) {
125+
detachHandlers(
126+
logicChildren.current.get(childTag)!,
127+
logicChildren.current.get(childTag)!
128+
);
161129
}
130+
logicChildren.current.delete(childTag);
162131
});
163132
}, [props.logicChildren]);
164133

165134
useEffect(() => {
166135
return () => {
167-
detachHandlers(
168-
attachedHandlerTags.current,
169-
attachedHandlerTags.current,
170-
attachedNativeHandlerTags.current
171-
);
136+
detachHandlers(attachedHandlerTags.current, attachedHandlerTags.current);
172137
logicChildren?.current.forEach((child) => {
173-
detachHandlers(
174-
child.attachedHandlerTags,
175-
child.attachedHandlerTags,
176-
child.attachedNativeHandlerTags
177-
);
138+
detachHandlers(child, child);
178139
});
179140
};
180141
}, []);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ export default abstract class GestureHandler implements IGestureHandler {
405405
invokeNullableMethod(onGestureHandlerTouchEvent, touchEvent);
406406
} else if (
407407
onGestureHandlerLogicTouchEvent &&
408-
this.actionType === ActionType.LogicDetector
408+
this.actionType === ActionType.LOGIC_DETECTOR
409409
) {
410410
invokeNullableMethod(onGestureHandlerLogicTouchEvent, touchEvent);
411411
} else {
@@ -429,14 +429,14 @@ export default abstract class GestureHandler implements IGestureHandler {
429429

430430
const resultEvent: ResultEvent =
431431
this.actionType !== ActionType.NATIVE_DETECTOR &&
432-
this.actionType !== ActionType.LogicDetector
432+
this.actionType !== ActionType.LOGIC_DETECTOR
433433
? this.transformEventData(newState, oldState)
434434
: this.lastSentState !== newState
435435
? this.transformStateChangeEvent(newState, oldState)
436436
: this.transformUpdateEvent(newState);
437437

438438
// TODO: cleanup the logic detector types
439-
if (this.actionType === ActionType.LogicDetector) {
439+
if (this.actionType === ActionType.LOGIC_DETECTOR) {
440440
resultEvent.nativeEvent = {
441441
...resultEvent.nativeEvent,
442442
childTag: this.childTag,
@@ -450,7 +450,7 @@ export default abstract class GestureHandler implements IGestureHandler {
450450
this.lastSentState = newState;
451451
if (
452452
onGestureHandlerLogicStateChange &&
453-
this.actionType === ActionType.LogicDetector
453+
this.actionType === ActionType.LOGIC_DETECTOR
454454
) {
455455
invokeNullableMethod(onGestureHandlerLogicStateChange, resultEvent);
456456
} else {
@@ -460,7 +460,7 @@ export default abstract class GestureHandler implements IGestureHandler {
460460
if (this.state === State.ACTIVE) {
461461
if (
462462
this.actionType !== ActionType.NATIVE_DETECTOR &&
463-
this.actionType !== ActionType.LogicDetector
463+
this.actionType !== ActionType.LOGIC_DETECTOR
464464
) {
465465
(resultEvent.nativeEvent as GestureHandlerNativeEvent).oldState =
466466
undefined;
@@ -471,7 +471,7 @@ export default abstract class GestureHandler implements IGestureHandler {
471471

472472
if (
473473
onGestureHandlerLogicEvent &&
474-
this.actionType === ActionType.LogicDetector
474+
this.actionType === ActionType.LOGIC_DETECTOR
475475
) {
476476
invokeNullableMethod(onGestureHandlerLogicEvent, resultEvent);
477477
} else {

0 commit comments

Comments
 (0)