Repository navigation
fix(core): register touch animations once per view - #11641
Conversation
View.onLoaded calls TouchManager.addAnimations every time a view is loaded, and addAnimations added its listeners unconditionally. A view that is loaded more than once (navigating back to a page, returning from the background, a subtree that is hidden and shown again) therefore collected one more long-press recognizer, gesture observer and set of touch listeners on each load, and UIControl-backed views pushed another entry into TouchManager.touchHandlers. The listeners are now added once per view and the control target once per native view. The disposeNativeView hook is registered once and clears the control-target mark so a recreated native view is wired again.
|
View your CI Pipeline Execution ↗ for commit 8b529ec
💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗ ☁️ Nx Cloud last updated this comment at |
commit: |
| }; | ||
|
|
||
| // What TouchManager.addAnimations has already wired, so repeated calls for a view add nothing twice. | ||
| const viewsWithTouchListeners = new WeakSet<View>(); |
There was a problem hiding this comment.
wouldn t it be better to store the touchListener, controlTarget, and disposeHook on the view object instead of maintaining such a map?
There was a problem hiding this comment.
On second thought, I agree with @farfromrefug on consolidating. The three WeakSets would work fine but maybe a single per-view record would help few others things and read better?
const touchState = Symbol('touchState');
type TouchState = {
downWired?: boolean;
upWired?: boolean;
handler?: any; // TouchControlHandler, retained here since UIControl doesn't retain its target
disposeHook?: boolean;
};
The bigger win perhaps in keeping the TouchControlHandler on the view is could replace TouchManager.touchHandlers entirely. That array I originally made to keep the handler alive. It holds a strong ref to each view until disposeNativeView. With the handler on the view, its lifetime matches the view's (no cycle, since the handler only has a WeakRef back), and the dispose hook just clears state.handler.
module-level WeakMap also works if rather not put TouchManager state on View. more just consolidation i think.
one small thing along these lines: in the UIControl condition, viewsWithControlTarget is marked even when only one of down/up is configured. If touchAnimation.up is added later, its target/action never gets wired until the native view is recreated. addListeners has the same issue as a single flag. Tracking down and up separately in the record would cover that.
There was a problem hiding this comment.
Agreed, done in 8b529ec: the state is now a single record stored on the view.
There was a problem hiding this comment.
Done in 8b529ec. One symbol-keyed record per view; the TouchControlHandler lives in it, so touchHandlers is no longer populated (I left it declared and deprecated rather than removing it in a patch release — happy to delete it if you prefer). Down and up are tracked separately for both the listeners and the control target, so a direction added later is wired on the next load.
* 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.
PR Checklist
What is the current behavior?
View.onLoaded()callsTouchManager.addAnimations(this)every time a view goes from unloaded to loaded, andaddAnimationsregisters its listeners unconditionally. A view that is loaded more than once keeps everything from the previous loads and gets another full set on top:UIControl: anothergestureAttachedlistener and anotherlongPresslistener, which means anotherGesturesObserverand anotherUILongPressGestureRecognizeron the same native view;UIControl-backed views: anotherTouchControlHandlerpushed intoTouchManager.touchHandlers(thedisposeNativeViewhook only removes one entry), another target/action pair, and anothertouchDown/touchUplistener;touchlistener plustouchDown/touchUplisteners;disposeNativeViewlistener.Views are loaded again whenever a page is navigated back to, when the app returns from the background, or when a hidden subtree is shown again, so with
TouchManager.enableGlobalTapAnimations = trueevery tappable view on a long-lived page accumulates these. In one app, a page with 23 tappable views that had been navigated back to 43 times had 43 long-press observers on each of those views (987GesturesObserverinstances where 23 were expected). Besides the memory, each touch then runs the down/up animation once per stacked handler.What is the new behavior?
addAnimationskeeps one record per view, stored on the view itself, of what it has already wired:UIControltarget/action is added once per native view, and thedisposeNativeViewhook clears that part of the record so a recreated native view is wired again;disposeNativeViewhook itself is registered once per view;touchAnimationafter the first load is wired on the next one. Views that have no animation configured yet are not marked at all.The
TouchControlHandleris held in that record instead of inTouchManager.touchHandlers, so it is released together with its view and the static array no longer keeps every such view alive untildisposeNativeView.touchHandlersstays declared, marked as deprecated, and is no longer populated.With the change applied, the same page stays at one observer per tappable view across repeated navigations (57
GesturesObserverinstances before and after ten navigate-in / navigate-back cycles; measured with the first commit of this PR, before the bookkeeping moved onto the view).No tests are included. The existing suites were not run locally; they pass in CI.