Skip to content

fix: prevent crash when trigger is called after dispose#268

Merged
mfazekas merged 2 commits into
mainfrom
fix/trigger-after-dispose
May 22, 2026
Merged

fix: prevent crash when trigger is called after dispose#268
mfazekas merged 2 commits into
mainfrom
fix/trigger-after-dispose

Conversation

@mfazekas
Copy link
Copy Markdown
Collaborator

Calling trigger() from a stale closure (e.g. a Reanimated callback that fires after navigation/unmount) would crash because the native property was already disposed.

useDisposableMemo now accepts an optional liveRef parameter — set on create, cleared on dispose. useRiveTrigger uses this to safely no-op with a console.warn instead of crashing.

Reproducer: mount RiveView with trigger → unmount → call trigger from async callback.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a crash caused by calling trigger() from stale closures after a RiveView unmounts/cleans up its native trigger property, by tracking whether the underlying native property is still “live” and safely no-op’ing when it isn’t.

Changes:

  • Add optional liveRef tracking to useDisposableMemo (set on create, cleared on dispose).
  • Thread liveRef through useRiveProperty options so hooks can observe property liveness.
  • Update useRiveTrigger to call trigger() via liveRef and warn/no-op instead of crashing after disposal.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/hooks/useRiveTrigger.ts Switches trigger execution to a liveness-tracked ref and adds a warning/no-op path.
src/hooks/useRiveProperty.ts Extends property options to accept a liveRef and forwards it into disposable memoization.
src/hooks/useDisposableMemo.ts Adds liveRef support and clears/sets it alongside disposal/creation lifecycle.
Comments suppressed due to low confidence (1)

src/hooks/useDisposableMemo.ts:70

  • liveRef is only synchronized inside the deps-change/initialization branch. If a caller starts passing a liveRef after the initial render (or swaps to a different ref) while deps remain equal, the new ref will never be populated with the current value. Consider handling liveRef changes by immediately syncing liveRef.current to the current memoized value (and clearing any previous liveRef) even when deps are unchanged.
  if (
    ref.current.deps === UNINITIALIZED ||
    !depsEqual(ref.current.deps, deps)
  ) {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/hooks/useRiveTrigger.ts
Comment thread src/hooks/useDisposableMemo.ts
@mfazekas mfazekas force-pushed the fix/trigger-after-dispose branch 2 times, most recently from 6f29d17 to 6ed8d9b Compare May 22, 2026 12:08
mfazekas added 2 commits May 22, 2026 14:54
useDisposableMemo now accepts an optional liveRef that is set on create
and cleared on dispose (including unmount). useRiveTrigger uses this to
safely no-op when called from stale closures (e.g. Reanimated callbacks
that fire after navigation).
Address Copilot review: only warn when trigger() is called after the
property was disposed, not when the viewModelInstance isn't available
yet. Add liveRef tests to useDisposableMemo and new useRiveTrigger
test suite.
@mfazekas mfazekas force-pushed the fix/trigger-after-dispose branch from 6ed8d9b to 785335b Compare May 22, 2026 12:55
@mfazekas mfazekas requested a review from HayesGordon May 22, 2026 12:57
Copy link
Copy Markdown
Contributor

@HayesGordon HayesGordon left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the quick fix

@mfazekas mfazekas merged commit 13d9dc1 into main May 22, 2026
8 of 9 checks passed
@mfazekas mfazekas deleted the fix/trigger-after-dispose branch May 22, 2026 14:57
mfazekas pushed a commit that referenced this pull request May 22, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.4.9](v0.4.8...v0.4.9)
(2026-05-22)


### Bug Fixes

* prevent crash when trigger is called after dispose
([#268](#268))
([13d9dc1](13d9dc1))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
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