fix: survive fast refresh without disposing native objects#212
Merged
mfazekas merged 5 commits intofeat/rive-ios-experimentalfrom Apr 14, 2026
Merged
fix: survive fast refresh without disposing native objects#212mfazekas merged 5 commits intofeat/rive-ios-experimentalfrom
mfazekas merged 5 commits intofeat/rive-ios-experimentalfrom
Conversation
Introduce useDisposableMemo — a useMemo replacement for native-backed objects that need deterministic cleanup. Disposal happens in the render phase on deps change, and on unmount via deferred setTimeout(0) in dev (cancelled if fast refresh re-mounts) or synchronously in production. Migrates useRiveProperty, useRiveList, and useViewModelInstance to use it, fixing the "NativeState is null" crash on HMR.
When deps change, useDisposableMemo disposes the old property during render, but the useEffect cleanup for that property runs later. Wrap removeListener/removeListeners calls in try-catch since native dispose() already handles listener cleanup. Also protect render-phase cleanup in useDisposableMemo against throws.
Covers first render, deps change, unmount, rapid cycling, undefined factory, throwing cleanup, sequential changes, and Object.is semantics. Also wraps deferred setTimeout cleanup in try-catch matching the render-phase protection.
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #205. Fast refresh (HMR) re-runs all effect cleanups then all effects. Hooks that disposed native HybridObjects in effect cleanup would destroy the backing native state, but
useMemocaches still returned the same (now dead) JS references — causingNativeState is nullcrashes.Introduces
useDisposableMemo— auseMemoreplacement for disposable native resources:setTimeout(0), cancelled if fast refresh / Strict Mode re-mounts the componentMigrates
useRiveProperty,useRiveList, anduseViewModelInstance. TheprevInstanceRefdance inuseViewModelInstance(manual render-phase disposal + separateuseEffectunmount cleanup) is replaced by a singleuseDisposableMemocall that handles both.Test plan