Skip to content

Commit 8b529ec

Browse files
committed
refactor(core): keep touch animation state on the view
* One record per view, stored on the view, replaces the three WeakSets. * The UIControl target is held in that record instead of TouchManager.touchHandlers, so it is released with its view and the static array no longer keeps every view alive until disposeNativeView. touchHandlers stays declared and is marked deprecated. * Down and up are tracked separately, for the listeners and for the control target, so a direction added to touchAnimation after the first load is wired on the next one.
1 parent 6288c15 commit 8b529ec

1 file changed

Lines changed: 91 additions & 65 deletions

File tree

‎packages/core/ui/gestures/touch-manager.ts‎

Lines changed: 91 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,33 @@ export type VisionHoverOptions = {
2626
shapeCornerRadius?: number;
2727
};
2828

29-
// What TouchManager.addAnimations has already wired, so repeated calls for a view add nothing twice.
30-
const viewsWithTouchListeners = new WeakSet<View>();
31-
const viewsWithControlTarget = new WeakSet<View>();
32-
const viewsWithDisposeHook = new WeakSet<View>();
29+
/**
30+
* What TouchManager.addAnimations has wired for a view. Kept on the view, so it lives exactly as long as the view does.
31+
*/
32+
const touchState = Symbol('touchState');
33+
type TouchState = {
34+
/** The down animation is hooked to the view's touchDown event. */
35+
down?: boolean;
36+
/** The up animation is hooked to the view's touchUp event. */
37+
up?: boolean;
38+
/** The gesture that raises touchDown/touchUp is attached: touch on Android, long press on iOS views that are not a UIControl. */
39+
gesture?: boolean;
40+
/** Target of the UIControl events. Held here because a UIControl does not retain its targets. */
41+
handler?: any /* TouchControlHandler */;
42+
/** The handler is registered with the current native view for down. */
43+
nativeDown?: boolean;
44+
/** The handler is registered with the current native view for up. */
45+
nativeUp?: boolean;
46+
disposeHook?: boolean;
47+
};
48+
49+
function getTouchState(view: View): TouchState {
50+
const holder = view as unknown as { [touchState]?: TouchState };
51+
if (!holder[touchState]) {
52+
holder[touchState] = {};
53+
}
54+
return holder[touchState];
55+
}
3356

3457
/**
3558
* Manage interactivity in your apps easily with TouchManager.
@@ -71,8 +94,7 @@ export class TouchManager {
7194
// }
7295
static animations: TouchAnimationOptions;
7396
/**
74-
* Native Touch handlers (iOS only) registered with the view through the TouchManager.
75-
* The TouchManager uses this internally but makes public for other versatility if needed.
97+
* @deprecated No longer populated. The native touch handler (iOS only) is kept with its view and released with it.
7698
*/
7799
static touchHandlers: Array<{ view: View; handler: any /* UIGestureRecognizer */ }>;
78100
/**
@@ -89,44 +111,46 @@ export class TouchManager {
89111
const handleDown = (view?.touchAnimation && (<TouchAnimationOptions>view?.touchAnimation).down) || (TouchManager.animations && TouchManager.animations.down);
90112
const handleUp = (view?.touchAnimation && (<TouchAnimationOptions>view?.touchAnimation).up) || (TouchManager.animations && TouchManager.animations.up);
91113

92-
// View.onLoaded calls this on every load, not only the first one. The listeners live on the
93-
// view and the control target lives on its native view, so each is added once per owner:
94-
// adding them again would stack recognizers and handlers on the same view.
95-
const addListeners = !!(handleDown || handleUp) && !viewsWithTouchListeners.has(view);
96-
if (addListeners) {
97-
viewsWithTouchListeners.add(view);
114+
// View.onLoaded calls this on every load, not only the first one, and a view can gain a
115+
// direction between two loads. Down and up are tracked separately so each is wired once:
116+
// the listeners per view, the control target per native view.
117+
const state = getTouchState(view);
118+
const wireDown = !!handleDown && !state.down;
119+
const wireUp = !!handleUp && !state.up;
120+
if (wireDown) {
121+
state.down = true;
122+
}
123+
if (wireUp) {
124+
state.up = true;
98125
}
99126

100127
if (__APPLE__) {
101128
if (view?.ios?.addTargetActionForControlEvents) {
102-
if (!viewsWithControlTarget.has(view)) {
103-
viewsWithControlTarget.add(view);
104-
// can use UIControlEvents
105-
if (!TouchManager.touchHandlers) {
106-
TouchManager.touchHandlers = [];
107-
}
108-
const handler = TouchControlHandler.initWithOwner(new WeakRef(view));
109-
TouchManager.touchHandlers.push({ view, handler });
110-
111-
if (handleDown) {
112-
(<UIControl>view.ios).addTargetActionForControlEvents(handler, GestureEvents.touchDown, UIControlEvents.TouchDown | UIControlEvents.TouchDragEnter);
113-
}
114-
if (handleUp) {
115-
(<UIControl>view.ios).addTargetActionForControlEvents(handler, GestureEvents.touchUp, UIControlEvents.TouchDragExit | UIControlEvents.TouchCancel | UIControlEvents.TouchUpInside | UIControlEvents.TouchUpOutside);
116-
}
129+
// can use UIControlEvents
130+
if (!state.handler) {
131+
state.handler = TouchControlHandler.initWithOwner(new WeakRef(view));
132+
}
133+
if (handleDown && !state.nativeDown) {
134+
state.nativeDown = true;
135+
(<UIControl>view.ios).addTargetActionForControlEvents(state.handler, GestureEvents.touchDown, UIControlEvents.TouchDown | UIControlEvents.TouchDragEnter);
136+
}
137+
if (handleUp && !state.nativeUp) {
138+
state.nativeUp = true;
139+
(<UIControl>view.ios).addTargetActionForControlEvents(state.handler, GestureEvents.touchUp, UIControlEvents.TouchDragExit | UIControlEvents.TouchCancel | UIControlEvents.TouchUpInside | UIControlEvents.TouchUpOutside);
117140
}
118141

119-
if (addListeners && handleDown) {
142+
if (wireDown) {
120143
view.on(GestureEvents.touchDown, (args) => {
121144
TouchManager.startAnimationForType(view, TouchAnimationTypes.down);
122145
});
123146
}
124-
if (addListeners && handleUp) {
147+
if (wireUp) {
125148
view.on(GestureEvents.touchUp, (args) => {
126149
TouchManager.startAnimationForType(view, TouchAnimationTypes.up);
127150
});
128151
}
129-
} else if (addListeners) {
152+
} else if ((state.down || state.up) && !state.gesture) {
153+
state.gesture = true;
130154
view.on(GestureEvents.gestureAttached, (args: GestureEventData) => {
131155
if (args.type === GestureTypes.longPress) {
132156
(<UILongPressGestureRecognizer>args.ios).minimumPressDuration = (<View>args.object)?.touchDelay || 0;
@@ -135,65 +159,67 @@ export class TouchManager {
135159
view.on(GestureTypes[GestureTypes.longPress], (args: GestureEventDataWithState) => {
136160
switch (args.state) {
137161
case GestureStateTypes.began:
138-
if (handleDown) {
162+
if (state.down) {
139163
TouchManager.startAnimationForType(<View>args.view, TouchAnimationTypes.down);
140164
}
141165
break;
142166
case GestureStateTypes.cancelled:
143167
case GestureStateTypes.ended:
144-
if (handleUp) {
168+
if (state.up) {
145169
TouchManager.startAnimationForType(<View>args.view, TouchAnimationTypes.up);
146170
}
147171
break;
148172
}
149173
});
150174
}
151-
} else if (addListeners) {
152-
view.on(GestureTypes[GestureTypes.touch], (args: TouchGestureEventData) => {
153-
switch (args.action) {
154-
case 'down':
155-
if (handleDown) {
156-
view.notify({
157-
eventName: GestureEvents.touchDown,
158-
object: view,
159-
data: args.android,
160-
});
161-
}
162-
break;
163-
case 'up':
164-
case 'cancel':
165-
if (handleUp) {
166-
view.notify({
167-
eventName: GestureEvents.touchUp,
168-
object: view,
169-
data: args.android,
170-
});
171-
}
172-
break;
173-
}
174-
});
175-
if (handleDown) {
175+
} else {
176+
if ((state.down || state.up) && !state.gesture) {
177+
state.gesture = true;
178+
view.on(GestureTypes[GestureTypes.touch], (args: TouchGestureEventData) => {
179+
switch (args.action) {
180+
case 'down':
181+
if (state.down) {
182+
view.notify({
183+
eventName: GestureEvents.touchDown,
184+
object: view,
185+
data: args.android,
186+
});
187+
}
188+
break;
189+
case 'up':
190+
case 'cancel':
191+
if (state.up) {
192+
view.notify({
193+
eventName: GestureEvents.touchUp,
194+
object: view,
195+
data: args.android,
196+
});
197+
}
198+
break;
199+
}
200+
});
201+
}
202+
if (wireDown) {
176203
view.on(GestureEvents.touchDown, (args) => {
177204
TouchManager.startAnimationForType(view, TouchAnimationTypes.down);
178205
});
179206
}
180-
if (handleUp) {
207+
if (wireUp) {
181208
view.on(GestureEvents.touchUp, (args) => {
182209
TouchManager.startAnimationForType(view, TouchAnimationTypes.up);
183210
});
184211
}
185212
}
186213

187-
if (viewsWithDisposeHook.has(view)) {
214+
if (state.disposeHook) {
188215
return;
189216
}
190-
viewsWithDisposeHook.add(view);
217+
state.disposeHook = true;
191218
view.on('disposeNativeView', (args) => {
192-
viewsWithControlTarget.delete(<View>args.object);
193-
const index = TouchManager.touchHandlers?.findIndex((handler) => handler.view === args.object);
194-
if (index > -1) {
195-
TouchManager.touchHandlers.splice(index, 1);
196-
}
219+
// The handler and its registrations belong to the native view that is going away.
220+
state.handler = undefined;
221+
state.nativeDown = false;
222+
state.nativeUp = false;
197223
TouchManager.touchAnimationDefinitions = TouchManager.touchAnimationDefinitions?.filter((d) => d.view !== args.object);
198224
});
199225
}

0 commit comments

Comments
 (0)