Skip to content

feat(hooks): async useViewModelInstanceAsync (deprecate sync hook)#304

Draft
mfazekas wants to merge 2 commits into
feat/rive-ios-experimentalfrom
feat/use-viewmodel-instance-async
Draft

feat(hooks): async useViewModelInstanceAsync (deprecate sync hook)#304
mfazekas wants to merge 2 commits into
feat/rive-ios-experimentalfrom
feat/use-viewmodel-instance-async

Conversation

@mfazekas

@mfazekas mfazekas commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

What

Adds useViewModelInstanceAsync, an async replacement for useViewModelInstance built on the non-deprecated *Async runtime APIs (viewModelByNameAsync, defaultArtboardViewModelAsync, createInstanceByNameAsync, createDefaultInstanceAsync, createBlankInstanceAsync). The sync hook creates instances via @deprecated APIs that access the Rive runtime on the JS thread; the async variant resolves off-thread.

Why

Drops the deprecated sync creation path (the hook currently ships with eslint-disable @typescript-eslint/no-deprecated and a migration TODO), and avoids JS-thread runtime access — the same class of off-thread access behind the #297 data-binding races.

API

Returns a { instance, isLoading, error } discriminated union mirroring useRiveFile. Same overloads/params as the sync hook, including required (throws once resolved to null; undefined while loading). Lifecycle uses useState/useEffect with a cancellation flag and callDispose on deps-change/unmount; onInit runs before the instance is exposed.

Backward compatible: the sync useViewModelInstance is marked @deprecated but kept working. Example screens migrated to the async hook (dataBind={instance} unchanged).

Verify

  • yarn tsc, yarn lint (0 errors), yarn jest (65 passed incl. 16 new async tests)
  • iOS harness E2E: the useViewModelInstance hook suites pass; 9 unrelated pre-existing native failures (RiveLogger not registered, setValueAsync not implemented) are tracked separately.

mfazekas added 2 commits July 1, 2026 07:27
The sync useViewModelInstance creates instances via deprecated runtime
APIs that touch the Rive runtime on the JS thread. Add
useViewModelInstanceAsync built on the non-deprecated `*Async` APIs,
returning a { instance, isLoading, error } discriminated union (mirroring
useRiveFile), with cancellation/disposal on deps change + unmount and
onInit applied before the instance is exposed.

Keeps `required` parity (throws once resolved to null; undefined while
loading). The sync hook is marked @deprecated but left working for
backward compatibility. Migrates the example screens to the async hook.
…ty, not just the view ref

On the experimental iOS backend the ViewModel instance and its properties
resolve asynchronously a short time after the view's hybridRef is assigned.
The test read `getViewModelInstance().numberProperty('ypos')` immediately
after waiting only for the ref, so on slower CI runners the property was
occasionally still undefined, failing at `expect(ypos1).toBeDefined()`
(~1 in 4 runs). Reproduced deterministically: at ref-callback time the VMI
is null 40/40, becoming ready shortly after.

Poll with waitFor until the `ypos` property is available before sampling it,
for both the initial mount and the post-switch reconfigure.
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.

1 participant