Skip to content

feat: add setValueAsync to ViewModelImageProperty - #447

Merged
mfazekas merged 10 commits into
mainfrom
feat/image-property-set-async
Nov 18, 2026
Merged

mfazekas merged 10 commits into
mainfrom
feat/image-property-set-async

Conversation

@mfazekas

@mfazekas mfazekas commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #445.

Adds ViewModelImageProperty.setValueAsync(image), matching the setValueAsync of the other property types. It resolves when this image has been applied to the instance (it does not wait for a draw) and rejects if decoding this image fails. undefined clears. set() calls setValueAsync() and logs a rejection.

On the new backends every image write goes through one static queue per platform (a Channel on Android, an AsyncStream on iOS) with a single consumer, so each write decodes and sets before the next one starts and the last call wins, including across wrappers of the same instance. This replaces the per-instance ImagePropertySequencer on Android. The iOS new backend also accepts undefined to clear, which overlaps the iOS image part of #360.

A write still queued when the instance is disposed resolves without applying on both platforms. The new backends don't check the path, so a write to a missing path also resolves. The legacy backends set synchronously and return a resolved promise. All backends throw synchronously on a disposed image.

mfazekas added a commit that referenced this pull request Nov 16, 2026
On the new Android backend, an `OutOfMemoryError` while decoding an
image kills the app from the `Rive CmdServer` thread, and it can't be
caught there. It's reachable in normal use: on a Release build with a
192 MB Java heap limit, replacing a 7.7 MB 4096 px JPEG died at cycle 16
with JS holding no references, replacing a 12 MP JPEG while calling
`dispose()` died at cycle 26, and one 50 MP JPEG died on its own.

There are three causes:

1. Our `HybridRiveImage` kept the encoded bytes on the Java heap, with
no `memorySize` and no real `dispose()`, so Hermes rarely collected
replaced images. The bytes were reclaimable, so this is delayed
collection rather than a strict leak.
2. rive-android 11.16.0 allocates a Java `IntArray` of `2 + w*h` on the
`Rive CmdServer` thread, 64 MiB for a 4096 px image, and only catches
`Exception`. 11.16.1 removed this.
3. Before rive-android 11.16.3, a failed `NewByteArray` left the error
pending, so JS saw an ordinary decode failure and the next decode on
that thread was fatal. 11.16.3 fixes this upstream
(rive-app/rive-android#482).

rive-android 11.16.3 is on main since #457. This PR adds `memorySize`
and a `dispose()` that drops the bytes, and captures the bytes
synchronously in `set()` so disposing right after it stays safe. With
11.16.3, a decode that runs out of heap fails, the image is logged, and
the app keeps running.

With the fix, Java heap peaks at 68 MB for the 4096 px replacement run,
30–40 MB for the 12 MP `dispose()` run and 52 MB for the 50 MP image,
and all 40 cycles finish. Holding every 4096 px image, and replacing or
holding a 42 MB PNG (which died at cycle 3 before), run the heap to its
limit: decodes fail on the CmdServer thread with `Failed to allocate
NewByteArray`, and every run finishes with no fatal exception (2 of 2
runs each on a Release build of this branch). The Android harness passes
218 of 218. The new test `disposing an image frees its bytes` fails on
the unfixed build, and `AndroidImageOom` is a reproducer page for
comparing builds.

Limits: heap numbers come from an emulator limit only (192 MB growth
limit, 576 MB `heapsize`), with no physical device. The legacy backend
and iOS were not exercised. GPU memory is unknown, since the emulator
reports 0 for graphics.

This conflicts with #447, which rewrites `set()`. Whichever merges
second must keep the synchronous byte capture.
Resolves once the image is decoded and handed to the instance, rejects if decoding fails, and still resolves when a newer call supersedes it. set() shares the implementation and keeps logging failures.

The iOS new backend gains the per-path newest-wins sequencer Android already has, and accepts undefined to clear.
@mfazekas
mfazekas force-pushed the feat/image-property-set-async branch from 2828730 to 8f34425 Compare November 16, 2026 13:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Per-wrapper iOS sequencing allows stale decodes to overwrite newer clears on the same native instance, while the Android regression test can fail nondeterministically.

2 open issues
What changed in this PR

Adds ViewModelImageProperty.setAsync() so callers can await image decoding and submission to the instance, addressing the completion-signal gap in #445 without waiting for a rendered frame.

Changes:

  • Exposes the promise-based API across TypeScript and generated native bindings while keeping set() synchronous in return type.
  • Adds iOS image-write sequencing and clearing support, and shares Android’s asynchronous application logic.
  • Adds completion, supersession, clearing, and decode-failure harness cases.
File Description
src/​specs/​ViewModel.nitro.ts Defines and documents setAsync().
nitrogen/​generated/​shared/​c++/​HybridViewModelImagePropertySpec.hpp Declares the promise-returning interface.
nitrogen/​generated/​shared/​c++/​HybridViewModelImagePropertySpec.cpp Registers the new method.
nitrogen/​generated/​ios/​swift/​HybridViewModelImagePropertySpec.swift Adds the Swift protocol method.
nitrogen/​generated/​ios/​swift/​HybridViewModelImagePropertySpec_cxx.swift Bridges Swift promise results to C++.
nitrogen/​generated/​ios/​c++/​HybridViewModelImagePropertySpecSwift.hpp Forwards calls to Swift.
nitrogen/​generated/​android/​kotlin/​com/​margelo/​nitro/​rive/​HybridViewModelImagePropertySpec.kt Adds the Kotlin promise interface.
nitrogen/​generated/​android/​c++/​JHybridViewModelImagePropertySpec.hpp Declares the Android bridge method.
nitrogen/​generated/​android/​c++/​JHybridViewModelImagePropertySpec.cpp Bridges Kotlin promise completion to C++.
ios/​new/​HybridViewModelInstance.swift Caches image sequencers by property path.
ios/​new/​HybridViewModelImageProperty.swift Adds async completion, clearing, and stale-write checks.
ios/​legacy/​HybridViewModelImageProperty.swift Resolves after synchronous image assignment.
example/​__tests__/​image-property-set-async.harness.ts Adds async image-property harness coverage.
example/​__tests__/​image-property-clear.harness.ts Enables clearing checks on all backends.
example/​__tests__/​image-dispose.harness.ts Enables clearing after disposal on iOS’s new backend.
android/​src/​new/​java/​com/​margelo/​nitro/​rive/​HybridViewModelImageProperty.kt Shares async application logic and guards image writes.
android/​src/​legacy/​java/​com/​margelo/​nitro/​rive/​HybridViewModelImageProperty.kt Wraps synchronous assignment in a settled promise.

Review effort: Balanced

Comment thread ios/new/HybridViewModelInstance.swift Resolved
RiveView.getViewModelInstance() returns a new wrapper for the bound
instance, so keying the image sequencers per wrapper let a pending
decode overwrite a later clear made through the view's wrapper. Key
them by the native instance instead, on iOS and Android.
On Android the corrupt decode can fail before the newer call bumps the
generation, so only the newest call's result is deterministic.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The regression test checks promise settlement rather than final property state, so it can pass even when an older decode overwrites the clear across wrappers.

1 open issue
2 resolved since last review

Review effort: Balanced

…wrapper test

The previous cross-wrapper test set a valid image, so both promises
resolved whether or not the wrappers shared sequencing. The test now
queues a large generated PNG ahead of corrupt bytes on the shared Rive
worker, so the corrupt decode only fails after the view wrapper's clear
has superseded it. With per-wrapper sequencing that decode rejects.

It runs on the new Android backend only: elsewhere RiveImages decodes up
front, so a set can't fail and JS has nothing to observe.
Decodes already finish in call order on the shared Rive worker, so the
only race was a clear overtaking a pending decode. Each image write now
runs to completion on a single static queue per platform before the next
starts, which replaces the per-path generation sequencer and its
registry. setAsync resolves when its own image is applied and rejects if
its own decode fails, and set() logs that rejection.

The legacy Android backend now throws on a disposed image instead of
returning a rejected promise, matching the other backends.

@HayesGordon HayesGordon left a comment

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.

Looks good to me overall. I read through the diff and the rive-android / rive-ios code it relies on, and ran the harness suite on an iOS simulator (new backend in full, legacy for the image tests). All green. I also bypassed the queue locally and the cross-wrapper ordering test went red, so it does guard the ordering. I couldn't run Android here, so I'm going by CI for that.

Three comments inline. The naming one is the one I'd sort out before release.

Comment thread src/specs/ViewModel.nitro.ts Resolved
Comment thread src/specs/ViewModel.nitro.ts Resolved
Matches the setValueAsync name used by the number, string, boolean, color
and enum properties, and documents that the new backends do not check the
path.
…osed

On the new backend a write still queued or decoding when the instance is
disposed hit RiveResourceClosedException in setImage and rejected, while
iOS resolved. Such writes now resolve without applying; other errors still
reject. A harness test holds a write behind a large decode, disposes the
instance and expects both writes to resolve on every backend.
@mfazekas mfazekas changed the title feat: add setAsync to ViewModelImageProperty feat: add setValueAsync to ViewModelImageProperty Nov 18, 2026
# Conflicts:
#	android/src/new/java/com/margelo/nitro/rive/HybridViewModelImageProperty.kt
#	android/src/new/java/com/margelo/nitro/rive/HybridViewModelInstance.kt

@HayesGordon HayesGordon left a comment

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.

LGTM!

@mfazekas
mfazekas enabled auto-merge (squash) November 18, 2026 20:30
@mfazekas
mfazekas merged commit f3bde90 into main Nov 18, 2026
15 checks passed
@mfazekas
mfazekas deleted the feat/image-property-set-async branch November 18, 2026 20:47
mfazekas pushed a commit that referenced this pull request Nov 19, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.5.16](v0.5.15...v0.5.16)
(2026-11-18)


### Features

* add setValueAsync to ViewModelImageProperty
([#447](#447))
([f3bde90](f3bde90))


### Bug Fixes

* **android:** free native objects when their wrappers are garbage
collected
([#466](#466))
([2b47837](2b47837))
* keep native objects alive across Activity hide and reveal
([#449](#449))
([3d4392c](3d4392c))

---
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