Skip to content

fix(core): register touch animations once per view - #11641

Merged
NathanWalker merged 2 commits into
mainfrom
fix/touch-animations-register-once
Nov 14, 2026
Merged

NathanWalker merged 2 commits into
mainfrom
fix/touch-animations-register-once

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

View.onLoaded() calls TouchManager.addAnimations(this) every time a view goes from unloaded to loaded, and addAnimations registers its listeners unconditionally. A view that is loaded more than once keeps everything from the previous loads and gets another full set on top:

  • on iOS, for views that are not a UIControl: another gestureAttached listener and another longPress listener, which means another GesturesObserver and another UILongPressGestureRecognizer on the same native view;
  • on iOS, for UIControl-backed views: another TouchControlHandler pushed into TouchManager.touchHandlers (the disposeNativeView hook only removes one entry), another target/action pair, and another touchDown / touchUp listener;
  • on Android: another touch listener plus touchDown / touchUp listeners;
  • on every platform: another disposeNativeView listener.

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 = true every 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 (987 GesturesObserver instances where 23 were expected). Besides the memory, each touch then runs the down/up animation once per stacked handler.

What is the new behavior?

addAnimations keeps one record per view, stored on the view itself, of what it has already wired:

  • the JS listeners are added once per view;
  • the UIControl target/action is added once per native view, and the disposeNativeView hook clears that part of the record so a recreated native view is wired again;
  • the disposeNativeView hook itself is registered once per view;
  • 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. Views that have no animation configured yet are not marked at all.

The TouchControlHandler is held in that record instead of in TouchManager.touchHandlers, so it is released together with its view and the static array no longer keeps every such view alive until disposeNativeView. touchHandlers stays 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 GesturesObserver instances 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.

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.
@nx-cloud

nx-cloud Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 8b529ec

Command Status Duration Result
nx run-many -t build -p core,webpack5,vite ✅ Succeeded 16s View ↗

💡 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 2026-11-14 00:04:44 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nativescript/core@11641
npm i https://pkg.pr.new/@nativescript/vite@11641
npm i https://pkg.pr.new/@nativescript/webpack@11641

commit: 8b529ec

NathanWalker
NathanWalker previously approved these changes Nov 13, 2026
};

// What TouchManager.addAnimations has already wired, so repeated calls for a view add nothing twice.
const viewsWithTouchListeners = new WeakSet<View>();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn t it be better to store the touchListener, controlTarget, and disposeHook on the view object instead of maintaining such a map?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, done in 8b529ec: the state is now a single record stored on the view.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@NathanWalker
NathanWalker dismissed their stale review November 13, 2026 23:56

considerations

* 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.
@NathanWalker
NathanWalker merged commit ff081de into main Nov 14, 2026
10 checks passed
@NathanWalker
NathanWalker deleted the fix/touch-animations-register-once branch November 14, 2026 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants